how do i update mulitple rows with dynamic data? - sqlite

I am using SQLite to update mulipte rows, but it does not work.
item table to keep the item data
item_ID Qty
- -
1 10
2 10
3 10
user_basket table to keep users' basket data
user_ID item_ID Bask_Qty
- - -
1 1 5
1 2 1
2 1 1
I used command like:
UPDATE item
SET Qty =
(SELECT Qty-Bask_Qty FROM user_basket
INNER JOIN item ON item.item_ID = user_basket.item_ID)
WHERE item_ID IN (SELECT item_ID FROM user_basket WHERE user_ID = 1);
After the command, I should expect item table be like that:
item_ID Qty
- -
1 5
2 9
3 10
but instead, I got:
item_ID Qty
- -
1 5
2 5
3 10
apparently, it used the same value to update all the rows.

Use a correlated subquery:
UPDATE item AS i
SET Qty = i.Qty - COALESCE(
(SELECT SUM(b.Bask_Qty) FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID),
0
)
See the demo.
Or, with a WHERE clause to avoid unnecessary updates:
UPDATE item AS i
SET Qty = i.Qty - (SELECT SUM(b.Bask_Qty) FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID)
WHERE EXISTS (SELECT 1 FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID)
See the demo.
If your version of SQLite is 3.33.0+ you can use UPDATE...FROM syntax:
UPDATE item AS i
SET Qty = i.Qty - b.Bask_Qty
FROM (SELECT item_ID, SUM(Bask_Qty) Bask_Qty FROM user_basket WHERE user_id = 1 GROUP BY item_ID) AS b
WHERE b.item_id = i.item_id
In all of the above queries I used SUM(Bask_Qty) to return the quantity of each item from the table user_basket, just in case there are more than 1 rows for each item.
If you are sure that there may be only 1 row for each item then replace it with just Bask_Qty.

Related

Mariadb Increment counter with ORDER BY

I Have table with description
Table Name : mt_user
column : id , name
Records
id
name
1
Tom
10
Carren
30
Jessy
Then I Query:
select r.id,
#rownum := #rownum + 1 as rownum
from mt_user r
cross join
(SELECT
#rownum := 0
) params
order by r.id desc;
the result is :
id
rownum
30
3
10
2
1
1
But if the query :
select r.id,
#rownum := #rownum + 1 as rownum
from mt_user r
cross join
(SELECT
#rownum := 0
) params
order by r.id asc;
the result is:
id
rownum
1
1
10
2
30
3
the question is, how can I achieved the result like this
id
rownum
1
1
10
2
30
3
but with order by r.id desc
because I trying the query in mariadb version 10.5.9, it have no problem.
it act strange on mariadb version 10.6.10 or 10.9.2

Updating a column based on conditions

I have a table like:
ID ID2 Name
1 1
2 1
3 2
4 2
5 2
6 3
7 3
8 3
I want to Update the Name column with Values like Name1, name 2 and so on.
This will be based on condition if there are two similar values in ID2 column, for example, the first two rows, then the Name column to be updated with values Name1 and Name2 respectively.Next rows would be Name1, Name2, Name3 respectively and so on. Can someone help me with the logic for this?
ROW_NUMBER with a partition on the ID2 column, ordered by the ID column, should generate the sequences you want. Try this update query:
UPDATE yourTable t1
SET Name = (SELECT 'Name' || t.rn FROM
(
SELECT t2.ID, t2.ID2,
ROW_NUMBER() OVER (PARTITION BY t2.ID2 ORDER BY t2.ID) rn
FROM yourTable t2
WHERE t1.ID = t2.ID AND t1.ID2 = t2.ID2
) t)

Find empty positions in a grid

In a Contents table, items are stored in X,Y coordinates:
Contents
-------
id
parent_id
pos_x
pos_y
Assume the container size is 3 by 3. I'd like to find which positions in a given container are free. So far I've generated a 2D matrix:
SELECT *
FROM
(SELECT rownum X FROM dual CONNECT BY LEVEL <= 3 ) xaxis
INNER JOIN
(SELECT rownum Y FROM dual CONNECT BY LEVEL <=3 ORDER BY 1) yaxis
ON xaxis.X <> yaxis.Y OR xaxis.X = yaxis.Y
Then I attempt to JOIN the queries together, excluding X,Y positions present in Contents:
SELECT X, Y
FROM
(SELECT rownum X FROM dual CONNECT BY LEVEL <= 3 ) xaxis
INNER JOIN
(SELECT rownum Y FROM dual CONNECT BY LEVEL <=3 ORDER BY 1) yaxis
ON xaxis.X <> yaxis.Y OR xaxis.X = yaxis.Y
INNER JOIN (
SELECT pos_x, pos_y FROM Contents WHERE parent_id = ?) items
ON items.posx <> xaxis.X AND items.posy <> yaxis.Y;
This doesn't treat each pair as unique, and excludes values from all rows if a position is occupied. For example, assuming that (2, 2) is occupied, the above returns:
X Y
-----
1 1
1 3
3 1
3 3
Essentially I'm trying to get the difference of the two sets. Any help appreciated.
I figured out the answer right before I posted the question, so I thought I'd post it and answer it at the same time. Stating the problem as get the difference of the two sets set me in the right direction.
The answer is the MINUS operator. Replace the final JOIN with MINUS and you get the intended results:
select X, Y
from
(select rownum X from dual CONNECT BY LEVEL <= 3 ) xaxis
inner join
(select rownum Y from dual CONNECT BY LEVEL <=3 order by 1) yaxis
on xaxis.X <> yaxis.Y OR xaxis.X = yaxis.Y
MINUS
select pos_x, pos_y FROM Contents WHERE parent_id = ?;
which returns the intended result (note the lack of (2, 2)):
X Y
-----
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Today was a good day
You could do this with an outer join rather than a minus (although you'd have to test both to find out which is more performant for your data!).
If you are only doing it for a single parent_id at a time, you would do:
WITH CONTENTS AS (SELECT 1 parent_id, 2 pos_x, 2 pos_y FROM dual UNION ALL
SELECT 2 parent_id, 2 pos_x, 1 pos_y FROM dual)
SELECT xaxis.x,
yaxis.y
FROM ((SELECT LEVEL x FROM dual CONNECT BY LEVEL <= 3) xaxis
CROSS JOIN (SELECT LEVEL y FROM dual CONNECT BY LEVEL <= 3) yaxis)
LEFT OUTER JOIN CONTENTS c ON c.pos_x = xaxis.x AND c.pos_y = yaxis.y AND c.parent_id = 1
WHERE c.parent_id IS NULL
ORDER BY x, y;
X Y
---------- ----------
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Alternatively, if you want to run it for all parent_ids, you could use a partitioned outer join like so:
WITH CONTENTS AS (SELECT 1 parent_id, 2 pos_x, 2 pos_y FROM dual UNION ALL
SELECT 2 parent_id, 2 pos_x, 1 pos_y FROM dual)
SELECT c.parent_id,
xaxis.x,
yaxis.y
FROM ((SELECT LEVEL x FROM dual CONNECT BY LEVEL <= 3) xaxis
CROSS JOIN (SELECT LEVEL y FROM dual CONNECT BY LEVEL <= 3) yaxis)
LEFT OUTER JOIN CONTENTS c PARTITION BY (c.parent_id) ON c.pos_x = xaxis.x AND c.pos_y = yaxis.y
WHERE c.pos_x IS NULL
AND c.pos_y IS NULL
ORDER BY c.parent_id,
xaxis.x,
yaxis.y;
PARENT_ID X Y
---------- ---------- ----------
1 1 1
1 1 2
1 1 3
1 2 1
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3

SQLite UPDATE Statement

SQLite UPDATE Statement
I have two tables:
Table1
ID Num
1
2
3
4
5
Table2
ID
1
1
2
2
2
3
3
4
4
4
5
I need to UPDATE the Num field in Table1 with occurences of the ID field in Table2 i.e. based on previous:
Table1
ID Num
1 2
2 3
3 2
4 3
5 1
If i run this SQLite statement:
SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID;
i have the correct table but when i try to UPDATE with that statement:
UPDATE Table1
SET Num=(SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID);
i have nonsense output.Any ideas?
You must use a correlated subquery to correlate the value returned by the subquery with the current row in the outer query.
This means that you must not use Table1 again in the subquery, but instead refer to the outer table (with the actual name; the UPDATEd table does not support an alias):
UPDATE Table1
SET Num = (SELECT COUNT(t2.ID)
FROM Table2 t2
WHERE Table1.ID=t2.ID
GROUP BY t2.ID);

i have one Issue in My Query not get Group Record

hi i have one issue in get query like 3 column
ID Grp Name
1 10 aa
2 11 bb
3 11 cc
4 11 dd
5 12 ee
6 12 ff
i want ans is
ID Grp Name
1 10 aa
2 11 bb
5 12 ee
so what can id Do ?
In sql server
Try this !
select * from
(
select *,rn=row_number()over(partition by Grp order by ID) from table
)x
where x.rn=1
For SQL Server:
WITH CTE AS (SELECT *,RN=ROW_NUMBER() OVER(PARTITION BY Grp ORDER BY ID)
FROM TableName)
SELECT ID,Grp,Name
FROM CTE
WHERE RN=1
Example in SQL Fiddle.
For MySQL:
SELECT ID,Grp,Name
FROM
(SELECT A.ID, A.Grp, A.Name, count(*) as row_number FROM TableName A
JOIN TableName B ON A.Grp = B.Grp AND A.ID >= B.ID
GROUP BY A.Grp, A.ID,A.Name) T
WHERE row_number=1
Example in SQL Fiddle.

Resources