All products bought by a particular user - sql

I have two tables:
x_products
id name image price
-- ------- ------ -----
1 name1 path 10
2 name2 path 8
3 name3 NULL 7
4 name4 path 10
5 name5 path 5
x_user_products
id userId productId
-- ------ ---------
1 100 1
2 100 2
3 105 1
4 105 3
5 100 5
6 102 2
how to SELECT all product of a user (for example user 100) in x_products?
the result should be like this:
id name image price
-- ------- ------ -----
1 name1 path 10
2 name2 path 8
5 name5 path 5

You are looking for the INNER JOIN keyword which selects records that have matching values in both tables.
The code:
SELECT xp.*
FROM x_products xp
INNER JOIN x_user_products xup ON xp.id = xup.productId
WHERE xup.userId = 100

You appears to want :
select xp.*
from x_products xp
where exists (select 1
from x_user_products xup
where xup.productId = xp.id and xup.userId = 100);

Related

postgresql how to check by two columns from join

There are some points and point_types with mark actual:
points
------
id point_type
------
1 1
2 1
3 2
point_types
------
id actual
------
1 true
2 false
Also there are directions:
directions
------
id point_from point_to
------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
7 3 1
8 3 2
9 3 3
I need only directions with actual point_from and point_to:
id point_from point_to
------
1 1 1
2 1 2
4 2 1
5 2 2
Trying with:
SELECT d.id, d.point_from, d.point_to
FROM directions d
JOIN points p ON (d.point_from = p.id OR d.point_to = p.id)
JOIN point_types pt ON pt.id = p.TYPE AND pt.actual = TRUE
GROUP BY 1,2,3
but getting directions with actual point_from or actual point_to :
id point_from point_to
------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
7 3 1
8 3 2
Use a CTE that returns all the ids of the not actual points and then with NOT EXISTS get only the directions that do not contain any of them:
WITH cte AS (
SELECT p.id
FROM points p INNER JOIN point_types pt
ON pt.id = p.point_type
WHERE pt.actual = false
)
SELECT d.*
FROM directions d
WHERE NOT EXISTS (SELECT * FROM cte c WHERE c.id IN (d.point_from, d.point_to));
See the demo.

Get TOP 1 row from multiple tables based on date

I have five tables in a SQL Server database. These tables are listed below and I want to select Data from these tables according to date. I tried searching but could not find solution for multiple tables. Please help
TABLE1
id PId CId
----------- ---------- ------------
1 P001 1
2 P002 2
3 P003 4
4 P004 5
TABLE2
id CId CNo ConId
----------- ---------- ------------ ----------
1 1 1 C123
2 1 2 PA444
3 1 3 PA456
4 2 1 AUX2398
5 2 2 AUX2345
6 4 1 PA123
7 5 1 C234
TABLE3
id CId CNo Label Date
----------- ---------- ------------ ---------- ----------
1 1 1 A 1/1/2000
2 1 2 A 15/10/2020
3 1 3 A 20/10/2020
4 2 1 A 15/10/2020
5 2 2 A 20/10/2020
6 4 1 A 20/10/2020
7 5 1 A 27/10/2020
TABLE4
id CId CNo Label Date
----------- ---------- ------------ ---------- ----------
1 1 1 B 20/10/2020
2 1 2 B 27/10/2020
3 1 3 B 22/10/2020
4 2 1 B 22/10/2020
5 2 2 B 26/10/2020
6 4 1 B 22/10/2020
7 5 1 B 30/10/2020
TABLE5
id CId CNo Label Date
----------- ---------- ------------ ---------- ----------
1 1 1 C 26/10/2020
2 1 2 C 1/1/2000
3 1 3 C 23/10/2020
4 2 1 C 25/10/2020
5 2 2 C 30/10/2020
6 4 1 C 25/10/2020
7 5 1 C 1/1/2000
I want to select Label and Date from Table 3, 4 and 5 where Date is >1/1/2000 and < than and close to 24/10/2020 and grouped according to PId, CId, ConId and CNo.
Desired result:
PId CId ConId CNo Label Date
-------- ---------- ---------- -------- --------- ----------
P001 1 C123 1 B 20/10/2020
P001 1 PA444 2 A 15/10/2020
P001 1 PA456 3 C 23/10/2020
P002 2 AUX2398 1 B 22/10/2020
P002 2 AUX2345 2 A 20/10/2020
P003 4 PA123 1 B 22/10/2020
P004 5 C234 1 - -
Any help will be appreciated. Thank you.
You can achieve this with a couple of CTE's; the first forms a UNION of TABLE3, TABLE4 and TABLE5; the second generates a row number based on the Date descending for each partition of PId, CId, ConId and CNo. We then select all rows from the second CTE where the row number is 1:
WITH CTE AS (
SELECT * FROM Table3 WHERE date > '2000-01-01'
UNION ALL
SELECT * FROM Table4 WHERE date > '2000-01-01'
UNION ALL
SELECT * FROM Table5 WHERE date > '2000-01-01'
),
CTE2 AS (
SELECT t1.PId, t1.CId, t2.ConId, t2.CNo, CTE.Label, CTE.Date,
ROW_NUMBER() OVER (PARTITION BY t1.PId, t1.CId, t2.ConId, t2.CNo ORDER BY CTE.Date DESC) AS rn
FROM TABLE1 t1
JOIN TABLE2 t2 ON t2.CId = t1.CId
LEFT JOIN CTE ON CTE.Cid = t2.CId AND CTE.CNo = t2.CNo AND CTE.Date < '2020-10-24'
)
SELECT PId, CId, ConId, CNo, Label, Date
FROM CTE2
WHERE rn = 1
ORDER BY PId, CId, CNo
Output:
PId CId ConId CNo Label Date
P001 1 C123 1 B 2020-10-20
P001 1 PA444 2 A 2020-10-15
P001 1 PA456 3 C 2020-10-23
P002 2 AUX2398 1 B 2020-10-22
P002 2 AUX2345 2 A 2020-10-20
P003 4 PA123 1 B 2020-10-22
P004 5 C234 1 - -
Demo on dbfiddle

Update value in a table in Oracle based upon another table's values

I have two tables and I wish to update a column in the first table (invn_sbs) based upon the results from another table (invn_sbs_qty).
The tables and columns are shown below
Table: invn_sbs
item_sid sbs_no active
-------- ------ ------
12345 6 0
23456 6 0
Table: invn_sbs_qty
item sid sbs_no store_no qty
-------- ------ -------- ---
12345 6 1 5
23456 6 10
What I wish to achieve is to update active = 1 in invn_sbs table
only if qty <> 0 and sbs_no = 6 and store_no = 1 in table invn_sbs_qty.
Therefore only item 12345 would be active = 1 after running the update.
Use the following :
ITEM_SID SBS_NO ACTIVE
-------- ------ ------
12345 6 0
23456 6 0
update invn_sbs s
set active = ( select sign(count(1))
from invn_sbs_qty q
where q.item_sid = s.item_sid
and qty <> 0
and sbs_no = 6
and store_no = 1);
-- results will become :
select * from invn_sbs;
ITEM_SID SBS_NO ACTIVE
-------- ------ ------
12345 6 1
23456 6 0

Convert delimited strings to parent-child type rows in SQL Server 2008 R2

I have the following table data:
SELECT * from Autenticacao_Permissoes;
PermissionId PermissionName RoleId
------------ -------------- ---------
1 Pages.Clientes.Editar.Test 3
2 Pages.Sinistros.Editar 3
3 Pages.Hack.Ver.Editar.Comer.Maca 3
4 Pages.Sinistros.VerFolder3 3
5 Pages.Hack.Ver NULL
There is no limit to the permissions' depth and I would like to have a temporary table with the following format:
SiteMapId Title RoleId ParentId
--------- ------------------------ ------ --------
1 Pages NULL NULL
2 Clientes NULL 1
3 Sinistros NULL 1
4 Hack NULL 1
5 Editar NULL 2
6 Test 3 5
7 Editar 3 3
...
The idea is to use it according to the ASP.NET sitemap data structure.

Retrieve data from Table

I have a table as follows,
Id Form_Id Form_Layout_Txt
-------------- ----------- -----------------
2 1 aa
3 1 bb
5 2 dddd
6 2 eeeee
7 3 fffff
8 3 gggg
i need to retreive the rows which has same Form_id as follows,
2 1 aa
3 1 bb
How to write a query for the above data?
Thanks
Here is what you need at most basic level, unless there's a hidden immunity to find out more :)
SELECT * FROM YOUR TABLE
WHERE FORM_ID = 1
Select *
from nameoftable
where Form_Id = 1;