I have a table some_table:
id | some_integer
---|-------------
1 | ?
2 | ?
3 | ?
How do I (and can it be done in one query) update some_table to:
Where id is 1, increase some_integer by 1
Where id is 2, decrease some_integer by 2
Where id is 3, increase some_integer by 3
To make some_table like:
id | some_integer
---|-------------
1 | ? + 1
2 | ? - 2
3 | ? + 3
Thanks!
UPDATE some_table
SET some_integer = some_integer + (CASE id WHEN 1 THEN 1 WHEN 2 THEN -2 WHEN 3 THEN 3 END)
WHERE id IN (1,2,3);
Use a CASE expression:
UPDATE yourTable
SET some_integer = CASE WHEN id = 1 THEN some_integer + 1
WHEN id = 2 THEN some_integer + 2
WHEN id = 3 THEN some_integer + 3 END
WHERE id IN (1, 2, 3);
Related
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.
I have a table like this:
user_id | subscription_id
-------------------------
1 | 1
1 | 2
2 | 3
2 | 4
3 | 1
3 | 2
4 | 3
5 | 3
What I want to do is count how many users have similar subscriptions:
user_id | same_subscriptions
----------------------------
1 | 1
2 | 0
3 | 1
4 | 1
5 | 1
Is this even possible? How can I achieve this...
Best I managed to do is get a table like this with group_concat:
user_id | subscriptions
-----------------------
1 | 1,2
2 | 3,4
3 | 1,2
4 | 3
5 | 3
This is how I achieved it:
SELECT A.user_id, group_concat(B.subscription_id)
FROM Subscriptions A LEFT JOIN Subscriptions B ON
A.user_id=B.user_id GROUP BY A.user_id;
The aggregate function GROUP_CONCAT() does not help in this case because in SQLite it does not support an ORDER BY clause, so that a safe comparison can be done.
But you can use GROUP_CONCAT() window function instead:
SELECT user_id,
COUNT(*) OVER (PARTITION BY subs) - 1 same_subscriptions
FROM (
SELECT user_id,
GROUP_CONCAT(subscription_id) OVER (PARTITION BY user_id ORDER BY subscription_id) subs,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY subscription_id DESC) rn
FROM Subscriptions
)
WHERE rn = 1
ORDER BY user_id
See the demo.
Results:
> user_id | same_subscriptions
> ------: | -----------------:
> 1 | 1
> 2 | 0
> 3 | 1
> 4 | 1
> 5 | 1
I have a table which looks like this:
id timestamp value1 value2
1 09:12:37 1 1
1 09:12:42 1 2
1 09:12:41 1 3
1 10:52:16 2 4
1 10:52:18 2 5
2 09:33:12 3 1
2 09:33:15 3 2
2 09:33:13 3 3
I need to group by id and value1. For each group i want to have the row with the highest timestamp.
The result for the table above would look like this:
id timestamp value1 value2
1 09:12:42 1 2
2 09:33:15 3 2
I know there is the summarize operator which would give me this:
mytable
| project id, timestamp, value1, value2
| summarize max(timestamp) by id, value1
Result:
id timestamp value1
1 09:12:42 1
2 09:33:15 3
But i was not able to get value2 for this rows too.
Thanks in advance
If i understand your question correctly, you should be able to use summarize arg_max():
doc: https://learn.microsoft.com/en-us/azure/kusto/query/arg-max-aggfunction
datatable(id:long, timestamp:datetime, value1:long, value2:long)
[
1, datetime(2019-03-20 09:12:37), 1, 1,
1, datetime(2019-03-20 09:12:42), 1, 2,
1, datetime(2019-03-20 09:12:41), 1, 3,
1, datetime(2019-03-20 10:52:16), 2, 4,
1, datetime(2019-03-20 10:52:18), 2, 5, // this has the latest timestamp for id == 1
2, datetime(2019-03-20 09:33:12), 3, 1,
2, datetime(2019-03-20 09:33:15), 3, 2, // this has the latest timestamp for id == 2
2, datetime(2019-03-20 09:33:13), 3, 3,
]
| summarize arg_max(timestamp, *) by id
This will result with:
| id | timestamp | value1 | value2 |
|----|-----------------------------|--------|--------|
| 2 | 2019-03-20 09:33:15.0000000 | 3 | 2 |
| 1 | 2019-03-20 10:52:18.0000000 | 2 | 5 |
I found a solution to my problem, but there might be a better one.
mytable
| project id, timestamp, value1, value2
| order by timestamp desc
| summarize max(timestamp), makelist(value2) by id, value1
Results in:
id timestamp value1 list_value2
1 09:12:42 1 ["2", "3", "1"]
2 09:33:15 3 ["2", "3", "1"]
Now you can extend the query by adding
| project max_timestamp, id, value1, list_value2[0]
to get the first element from that list. Replace '0' by any number between 0 and length(list_value2)-1 to access the other values.
One more advice:
The timestamp i use is the one that is generated by ApplicationInsights. In our code we call TrackTrace to log some data. If you order the rows by this timestamp, the resulting list of rows is not garanteed to be in the same order in which the data was produced in code.
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
SELECT * FROM MyTable
a | b
---+----
1 | 2
2 | 10
2 | 5
3 | 10
I want each number to only appear once in each column, i.e. the result should be:
a | b
---+----
1 | 2
2 | 10
Is this possible?
I suggest to add a column with ID. Then do this:
select a, b from mytable m1 where
a not in (select m2.a from mytable m2 where m2.id < m1.id)
and
b not in (select m3.b from mytable m3 where m3.id < m1.id)
If you don't want to decide by the order of IDs, you can add another column (like a sequence).