How to write insert query for SQLite with selected values - sqlite

I am using Ionic 2 and SQLite trying to insert a comment in the table and point_id to be equal to second highest point_id of particular match. So I wrote following query, but Ionic 2 is throwing me
sqlite3_prepare_v2 failure: no such column: match_id
INSERT INTO comments (point_id, match_id, comment, created_at)
VALUES((
SELECT name.point_id
FROM (
SELECT point_id from undo
WHERE match_id = ?
ORDER BY point_id DESC
LIMIT 2) AS name
WHERE match_id = ?
ORDER BY point_id LIMIT 1), ?, ?, ?)
My whole ionic 2 code is
NativeStorage.getItem('matchID').then(matchID => {
let query = 'INSERT INTO comments (point_id, match_id, comment, created_at) '+
'VALUES((SELECT name.point_id FROM (SELECT point_id from undo WHERE match_id = ? ORDER BY point_id DESC LIMIT 2) AS name WHERE match_id = ? ORDER BY point_id LIMIT 1), ?, ?, ?)';
this.db.executeSql(query, [matchID.value, matchID.value, matchID.value, data.comment, new Date()]).then(data_comment => {
let toast = this.toastCtrl.create({
message: 'Comment was added successfully',
duration: 2000,
position: 'top'
});
toast.present();
console.log("comment entered", data_comment);
}, err => console.error("ERROR enter comment in match tracker ", err))
})
Thank you very much.

I changed my SQLite query and now it works fine. If somebody else needs this here it is:
INSERT INTO comments (point_id, match_id, comment, created_at)
VALUES((
SELECT point_id from undo
WHERE match_id = ?
ORDER BY point_id DESC
LIMIT 1 OFFSET 1), ?, ?, ?)

Related

Crafting a Subquery-able UNION ALL based on the results of a query

Data
I have a couple of tables like so:
CREATE TABLE cycles (
`cycle` varchar(6) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`cycle_type` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`start` date DEFAULT NULL,
`end` date DEFAULT NULL
);
CREATE TABLE rsvn (
`str` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL
);
INSERT INTO `cycles` (`cycle`, `cycle_type`, `start`, `end`) values
('202013', 'a', '2021-01-04', '2021-01-31'),
('202013', 'b', '2021-01-04', '2021-01-31'),
('202101', 'a', '2021-01-04', '2021-01-31'),
('202101', 'b', '2021-01-04', '2021-01-31'),
('202102', 'a', '2021-02-01', '2021-02-28'),
('202102', 'b', '2021-02-01', '2021-02-28'),
('202103', 'a', '2021-03-01', '2021-03-28'),
('202103', 'b', '2021-03-01', '2021-03-28');
INSERT INTO `rsvn` (str, start_date, end_date) values
('STR01367', '2020-12-07', '2020-06-21'),
('STR00759', '2020-12-07', '2021-04-25'),
('STR01367', '2021-01-04', '2021-09-12'),
('STR01367', '2021-06-21', '2022-02-27');
Desired Results
For any given cycle, I want to count the number of occurrences of str across cycles. So between cycle 2108 - 2108 (one cycle), I see:
str
count
STR01367
1
STR00759
1
And from between 2108 - 2109 (two cycles) I see:
str
count
STR01367
2
STR00759
1
What I've tried
I'm trying to figure out how to dynamically obtain those results. I don't see any options outside a UNION ALL query (one query for each cycles), so I tried writing a PROCEDURE. However, that didn't work because I want to do post-processing on the query results, and I don't believe you can use the results of a PROCEDURE in a CTE or subquery.
My PROCEDURE (works, can't include results in a subquery like SELECT * FROM call count_cycles (?)):
CREATE PROCEDURE `count_cycles`(start_cycle CHAR(6), end_cycle CHAR(6))
BEGIN
SET #cycles := (
SELECT CONCAT('WITH installed_cycles_count AS (',
GROUP_CONCAT(
CONCAT('
SELECT rsvn.str, 1 AS installed_cycles
FROM rsvn
WHERE "', `cy`.`start`, '" BETWEEN rsvn.start_date AND COALESCE(rsvn.end_date, "9999-01-01")
OR "', `cy`.`end`, '" BETWEEN rsvn.start_date AND COALESCE(rsvn.end_date, "9999-01-01")
GROUP BY rsvn.str
'
)
SEPARATOR ' UNION ALL '
),
')
SELECT
store.chain AS "Chain"
,store.division AS "Division"
,dividers_store AS "Store"
,SUM(installed_cycles) AS "Installed Cycles"
FROM installed_cycles_count r
LEFT JOIN store ON store.name = r.dividers_store
GROUP BY dividers_store
ORDER BY chain, division, dividers_store, installed_cycles'
)
FROM cycles `cy`
WHERE `cy`.`cycle_type` = 'Ad Cycle'
AND `cy`.`cycle` >= CONCAT('20', RIGHT(start_cycle, 4))
AND `cy`.`cycle` <= CONCAT('20', RIGHT(end_cycle, 4))
GROUP BY `cy`.`cycle_type`
);
EXECUTE IMMEDIATE #cycles;
END
Alternatively, I attempted to use a recursive query to obtain my results by incrementing my cycle. This gave me the cycles I wanted:
WITH RECURSIVE xyz AS (
SELECT cy.`cycle`, cy.`start`, cy.`end`
FROM cycles cy
WHERE cycle_type = 'Ad Cycle'
AND `cycle` = '202101'
UNION ALL
SELECT cy.`cycle`, cy.`start`, cy.`end`
FROM xyz
JOIN cycles cy
ON cy.`cycle` = increment_cycle(xyz.`cycle`, 1)
AND cy.`cycle_type` = 'Ad Cycle'
WHERE cy.`cycle` <= '202110'
)
SELECT * FROM xyz;
But I can't get it working when I add in the reservations table:
infinite loop?
WITH RECURSIVE xyz AS (
SELECT cy.`cycle`, 'dr.dividers_store', 1 AS installed_cycles
FROM cycles cy
LEFT JOIN rsvn dr
ON cy.`start` BETWEEN dr.start_date AND COALESCE(dr.end_date, "9999-01-01")
OR cy.`end` BETWEEN dr.start_date AND COALESCE(dr.end_date, "9999-01-01")
WHERE cy.`cycle_type` = 'Ad Cycle'
AND cy.`cycle` = '202101'
UNION ALL
SELECT cy.`cycle`, 'dr.dividers_store', 1 AS installed_cycles
FROM xyz
JOIN cycles cy
ON cy.`cycle` = increment_cycle(xyz.`cycle`, 1)
AND cy.`cycle_type` = 'Ad Cycle'
LEFT JOIN rsvn dr
ON cy.`start` BETWEEN dr.start_date AND COALESCE(dr.end_date, "9999-01-01")
OR cy.`end` BETWEEN dr.start_date AND COALESCE(dr.end_date, "9999-01-01")
WHERE cy.`cycle` <= '202102'
)
SELECT * FROM xyz
What options do I have to get the results I need, in such a way that I can use them in a CTE or subquery?
The results I am looking for are easily obtained via a two-stage grouping. Something like this:
WITH sbc AS (
SELECT cy.`cycle`, dr.str, 1 AS 'count'
FROM cycles cy
LEFT JOIN rsvn dr
ON cy.`start` BETWEEN dr.start_date AND dr.end_date
OR cy.`end` BETWEEN dr.start_date AND dr.end_date
WHERE cy.`cycle_type` = 'Ad Cycle'
AND cy.`cycle` BETWEEN '202201' AND '202205'
GROUP BY cy.`cycle`, dr.str
ORDER BY dr.str, cy.`cycle`
)
SELECT `cycle`, str, SUM(`count`) as `count`
FROM sbc
GROUP BY str
The CTE produces one result per rsvn per cycle. Afterwards all that is needed is to group by store and count the number of occurrences.
Besides being simpler, I suspect that this query is faster than the union concept I was stuck on when I asked the question, since among other things the server does not need to perform a union on multiple grouping queries. However, I do not understand how MariaDB optimizes such queries, and while I am curious I don't have the time to run benchmarks to find out.

Querying a many to many relationship with filter and pagination in SQLite

I am trying to paginate over a list of blog posts and filter those based on a list of tags they might have in an SQLite database.
Posts and Tags have a n-to-n relationship so I created a PostTag relation table.
CREATE TABLE "Post" (
"Id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"Title" TEXT
);
CREATE TABLE "Tag" (
"Id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"Label" TEXT
);
CREATE TABLE "PostTag" (
"Id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"PostId" INTEGER,
"TagId" INTEGER,
FOREIGN KEY("PostId") REFERENCES "Post"("Id"),
FOREIGN KEY("TagId") REFERENCES "Tag"("Id")
);
Given the following data
INSERT INTO Post (Title) VALUES ('Post title 1'), ('Post title 2'), ('Post title 3');
INSERT INTO Tag (Label) VALUES ('news'), ('funny'), ('review');
INSERT INTO PostTag (PostId, TagId) VALUES (1, 1), (1, 2), (2, 3), (3, 2), (3, 3);
I am trying to select 10 posts that have both the tags 'news' and 'funny' so I would like only 'Post title 1' to be returned (edit for clarification : I need post 1 to be returned twice here, once with the 'news' tag, and once with the 'funny' tag).
I am using DENSE_RANK to actually have 10 different posts in the results even though the join could return more than 10 rows.
The issue I have is how to manage the 'AND' operator on tags values, i.e. not returning posts that have only one of the tags. So here I would not want post 3 to be returned because it only has the 'funny' tag, not the 'news' tag.
Here is my best query so far (updated below), which will return posts having 'news' or 'funny' which is not what I want :
SELECT * FROM (
SELECT p.*, t.*, DENSE_RANK() OVER(order by p.id desc) rnk
FROM Post p
JOIN PostTag pt ON p.Id = pt.PostId
JOIN Tag t ON pt.TagId = t.Id AND t.Label IN ('news', 'funny')
ORDER BY p.id desc
) ranked
WHERE rnk <= 10
Please note that I am deduplicating and regrouping the results by posts afterwards using dapper, so having each post appearing several times is not real concern (please read update below for more details).
UPDATE:
The query must return a matching post as many times as the number of its associated tags (even though those tags may not be in the queried tags), something like :
Id Title Id:2 Label rnk
1 'Post Title 1' 1 'news' 1
1 'Post Title 1' 2 'funny' 1
If later on, someone adds a tag to post 1 like so :
INSERT INTO Tag (Label) VALUES ('tech'); -- id is 4
INSERT INTO PostTag (PostId, TagId) VALUES (1, 4);
The result of the query should be
Id Title Id:2 Label rnk
1 'Post Title 1' 1 'news' 1
1 'Post Title 1' 2 'funny' 1
1 'Post Title 1' 4 'tech' 1
So I can show the matching post with all its tags, even though the tag was not in the query.
I finally have something working, but it is horribly nested and I am sincerely wondering why this problem is ending up being so convoluted. Isn't there a way to count on ranks directly ?
select * from (
select *, dense_rank() over(order by p.id desc) rnk
from Post p
join PostTag pt on p.Id = pt.PostId
join Tag t on pt.TagId = t.Id
and postId in (
select postId from (
select dense_rank() over(order by pt2.PostId) rnk2,
from PostTag pt2
join Tag t2 on pt2.TagId = t2.Id
where t2.Label in ('news', 'funny')
)
group by rnk2
having count(rnk2) == 2 -- 2 being the number of tags requested
) order by p.id desc
)
ranked where rnk <= 10
Just to give you one idea.
select * from (
select p.*, t.*, dense_rank() over(order by p.id desc) rnk
from Post p
join PostTag on p.Id = PostId
join Tag t on TagId = t.Id
and postid in (select postid
from posttag join tag t on tagid = t.id
where label in ('news', 'funny')
group by postid having count(distinct tagid) > 1)
order by p.id desc
) ranked
where rnk <= 10;
You can do it with a simple group by post, like this:
select p.id, p.title
from posttag pt
inner join post p on p.id = pt.postid
inner join tag t on t.id = pt.tagid
where t.label in ('news', 'funny')
group by p.id, p.title
having count(distinct t.id) = 2
order by p.id limit 10
See the demo.

Update multiple rows from select statement

I am trying to assign 'A' to [Student Details].group based on this SELECT statement.
SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
I can't figure out how to use a SELECT statement in an UPDATE statement.
Can someone please explain how to accomplish this?
I am using ASP .NET and MsSQL Server if it makes a difference.
Thanks
I'm assuming you want to update these records and then return them :
SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
INTO #temp
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
update [Student Details] set group='A' where [person id] in(select [person id] from #temp)
select * from #temp
I'm also assuming person id is the PK of student details
Try this using CTE (Common Table Expression):
;WITH CTE AS
(
SELECT TOP 10 PERCENT [Group]
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
)
UPDATE CTE SET [Group] = 'A'
Is this you want?
Update top (10) Percent [Student Details] set [group] = 'A'
where [Location Cd] = 'PAR' AND [group] is null

Copy a subset of column data from one table to another

I have two tables with identical schema. Let's name them TestTable and TestTableTemp. I need to copy just two columns from TestTableTemp to TestTable without disrupting other data. The rows in TestTable are a subset of those in TestTableTemp. Let's say the columns that I need to copy are named Column1 and Column2 and that they have identical primary keys reference by column primaryKey.
In mysql I believe this could be done as such or something similar:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Sqlite does not allow for multiple tables to be defined on the update statement as can been seen in their reference data here: http://www.sqlite.org/lang_update.html
The best I could come up with is such:
UPDATE TestTable SET
Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey),
Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey)
WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey"
This gives me a syntax error near "." I am guessing this is because I cannot reference TestTable in the scalar expressions.
Can anyone point me in the right direction? Any help is much appreciated.
EDIT:
I cleaned up the second query a bit. It seems to just set the Column1 and Column2 to the first row from that column from TestTableTemp.
Your original query for comparison:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1
, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Here is the working query (I just slightly changed your version):
http://sqlfiddle.com/#!5/f3a19/9
UPDATE TestTable
SET
Column1 = ( SELECT TestTableTemp.Column1
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
,Column2 = ( SELECT TestTableTemp.Column2
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
WHERE EXISTS( SELECT NULL
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
;

SQL Select fields with a value of 'Y' and order by date descending, then select all others and order by another field ascending

I am generating an SQL query:
SELECT * FROM ToDoList
WHERE ws_status <> 'Completed'
AND (user_id= 'TESTUSR' OR ww_cover='TESTUSR'
OR (ws_status = 'Orphan' AND wwt_workgroupid IN (108)))
**ORDER BY psc_alt_code ASC**
And I need to list all results with wi_urgent set to 'Y' and order them by date Desc *first and then list all other results ordered by psc_alt_code descending* so I thought something like this would suffice:
ORDER BY (wi_urgent = 'Y') DESC, psc_alt_code ASC
I am getting SqlClient.SqlException: Incorrect syntax near '=' error when trying to run that query. Please note that I am querying an SQL View if that makes a difference?
You can use a case expression in the order by
SELECT * FROM ToDoList
WHERE ws_status <> 'Completed'
AND (user_id= 'TESTUSR' OR ww_cover='TESTUSR'
OR (ws_status = 'Orphan' AND wwt_workgroupid IN (108)))
ORDER BY CASE WHEN wi_urgent = 'Y' THEN 0 ELSE 1 END ASC
,psc_alt_code
I don't think you can do wi_urgent = 'Y' in an ORDER BY.
Since you're looking for all results with wi_urgent, try adding it to the WHERE clause:
SELECT * FROM ToDoList
WHERE ws_status <> 'Completed'
AND (user_id= 'TESTUSR' OR ww_cover='TESTUSR'
OR (ws_status = 'Orphan' AND wwt_workgroupid IN (108)))
AND wi_urgent = 'Y'
ORDER BY wi_urgent DESC,
psc_alt_code ASC

Resources