How to Join with select top - sql

I have a problem with the combination of multiple tables.
My SQL query:
SELECT *
FROM CRM.Bank as a
JOIN CRM.Documents as b ON a.Bank_ID = b.Documents_ID
JOIN CRM.Counterparties as c ON c.Counterparties_ID = b.Documents_ID
JOIN CRM.Items as d ON d.Document_tran_ID = b.Documents_ID
I have table CRM.Items, which has the following columns:
Item_ID Document_tran_ID Name
======= ================ ====
1 1 Advertising banner
2 1 Shipping costs
3 2 Garden tent
4 2 Additional fasteners
5 2 Shipping costs
And now I have a problem how to connect only the first items to the document (d.Document_tran_ID = b.Documents_ID ) ??
I know I should use SELECT TOP. However, I have a problem with creating the correct query
Expect the result in the form of:
Bank_ID Documents_ID Counterparties_ID Document_tran_ID Name
======= ============ ================= ================ ====
22 1 4 1 Advertising banner
23 2 20 2 Garden tent
24 3 21 3 Other
Only the first Item from the document is matched.

I think you can try to use CROSS APPLY join. In the inner query, you can apply order condition for selecting TOP row
SELECT *
FROM CRM.Bank as a
JOIN CRM.Documents as b ON a.Bank_ID = b.Documents_ID
JOIN CRM.Counterparties as c ON c.Counterparties_ID = b.Documents_ID
CROSS APPLY
(select top 1 * from CRM.Items i where i.Document_tran_ID = b.Documents_ID) as d

use sub-query for documents table
SELECT *
FROM CRM.Bank as a
JOIN ( select min(Document_tran_ID) as Documents_ID from CRM.Documents) as b ON a.Bank_ID = b.Documents_ID
JOIN CRM.Counterparties as c ON c.Counterparties_ID = b.Documents_ID
JOIN CRM.Items as d ON d.Document_tran_ID = b.Documents_ID

Related

SQL query doesnt return expected values

I have 3 tables. 1st table stores coil information, 2nd table store coil information in transport and 3rd stores reserved coils.
coils
ID
SERIAL
COLOR
MATERIAL
STATUS
1
12345
5
1
2
2
12346
4
1
3
3
12347
3
1
2
coils_in_transport
ID
SERIAL
COLOR
MATERIAL
STATUS
1
f34S5
5
1
2
2
A23GG6
4
1
3
3
ff2S147
3
1
2
reserved_coils
ID
NUMBER
QUANTITY
START
END
1
12345
25
2022-05-01
2023-05-01
3
12347
252
2022-01-01
2023-05-01
4
A23GG6
33
2022-04-01
2023-05-01
5
ff2S147
35
2022-08-01
2023-05-01
I need to write query that will get all reserved coils (reserved_coils) that has status 2 and for each coil I need to join tables for materials and colors
material
ID
NAME
1
color
ID
NAME
I wrote query but it doesn't show coils in transport that are reserved, here is what I tried
SELECT a.QUANTITY, a.START, a.END, b.name, m.name
FROM reserved_coils a
LEFT JOIN coils b ON a.number = b.serial
LEFT JOIN coils_in_transport c ON a.number = c.serial
LEFT JOIN material m ON b.material = m.id
LEFT JOIN material b ON b.material = b.id
where b.status = 2 and c.status = 2
To get all coils and colis_in_transport you need to use UNION for SELECT statements for both tables, each filtered by status value, which should be equal to 2. Then you need to do a JOIN between reserved_coils and the result of UNION to filter out reserved_coils rows.
Your query would be like this
SELECT
r.quantity,
r.start,
r.[end],
c.serial,
material.name AS material,
color.name AS color
FROM reserved_coils r
JOIN (
SELECT * FROM coils WHERE status = 2
UNION
SELECT * FROM coils_in_transport WHERE status = 2
) c ON r.number = c.serial
LEFT JOIN material ON material.id = c.material
LEFT JOIN color ON color.id = c.color
Demo
If you want left join twice on same table, this might help
SELECT a.QUANTITY, a.START, a.[END], isnull(b.serial,c.serial) serial,isnull(c1.name,c2.name) color, isnull(m.name,n.name) material
FROM reserved_coils a
LEFT JOIN coils b ON a.number = b.serial
LEFT JOIN coils_in_transport c ON a.number = c.serial
LEFT JOIN material m ON b.material = m.id
LEFT JOIN material n ON c.material = n.id
LEFT JOIN color c1 on b.color=c1.id
LEFT JOIN color c2 on c.color=c2.id
where isnull(b.status,c.status) = 2
DB<>Fiddle

SQL help i need to find the inventory remaining in my office

In sql help i have 3 tables, table one is asset table which is as follow
id
asset_code
asset_name
asset_group
asset_quantity
1
A001
demo asset
4
5
2
A002
demo asset 2
6
3
and another table is asset_allocation
id
asset_id
allocated_quantity
allocated_location
1
1
2
IT office
2
1
1
main hall
the last table is asset_liquidated which will present assets that are no longer going to be used
id
asset_id
liquidated_quantity
1
1
2
2
1
1
lets say i have 5 computers and i have allocated 3 computers and 1 is no longer going to be used so i should be remaining with 1 computer so now how do i make sql auto generate this math for me
You need to use aggregation and the join your tables -
SELECT id, asset_code, asset_name, asset_group, asset_quantity,
asset_quantity - COALESCE(AA.allocated_quantity, 0) - COALESCE(AL.liquidated_quantity, 0) available_quantity
FROM asset A
LEFT JOIN (SELECT asset_id, SUM(allocated_quantity) allocated_quantity
FROM asset_allocation
GROUP BY asset_id) AA ON A.id = AA.asset_id
LEFT JOIN (SELECT asset_id, SUM(liquidated_quantity) liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id) AL ON A.id = AL.asset_id
This query will give you -1 as available_quantity for asset_id 1 as you have only 5 available, 3 of them are allotted and 3 are liquidated as per your sample data.
Please see if this helps
SELECT
asset_quantity AS Total_Assets
,ISNULL(allocated_quantity, 0) allocated_quantity
,ISNULL(liquidated_quantity, 0) liquidated_quantity
FROM asset
LEFT OUTER JOIN (
SELECT
asset_id, SUM(allocated_quantity) AS allocated_quantity
FROM asset_allocation
GROUP BY asset_id
) asset_allocation2
ON asset_allocation2.asset_id = asset.id
LEFT OUTER JOIN (
SELECT
asset_id, SUM(liquidated_quantity) AS liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id
) asset_liquidated 2
ON asset_liquidated 2.asset_id = asset.id

Most efficient way to SELECT and JOIN these tables

I have 4 tables:
Table1(Groups):
id_group name_group
1 abc
2 def
3 ghi
Table2(Color):
id_color name_color
1 blue
2 red
3 green
Table3(Variety):
id_variety id_color id_group name_variety
1 1 1 light
2 3 1 dark
3 6 2 dark
Table4(Tone):
id_tone id_color id_group name_tone
1 1 1 ocean
2 5 1 sea
3 9 3 clay
Given an id_group like 1 I want to make a selection resulting something like this:
id_group id_color name_variety name_tone
1 1 light ocean
1 3 dark NULL
1 5 NULL sea
I was able to solve the problem with an union and changing the order of the JOINS but I think that there must be another solution
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, TONE.name_tone
FROM
groups GRO
LEFT OUTER JOIN
variety VARI ON (VARI.id_group = GRO.id_group)
LEFT OUTER JOIN
tone TONE ON (TONE.id_group = GRO.id_group)
AND (TONE.id_color = VARI.id_color)
LEFT OUTER JOIN
color COL ON (COL.id_color = VARI.id_color)
OR (COL.id_color = TONE.id_color)
WHERE
GRO.id_group = 1
UNION
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, TONE.name_tone
FROM
groups GRO
LEFT OUTER JOIN
tone TONE ON (TONE.id_group = GRO.id_group)
LEFT OUTER JOIN
variety VARI ON (VARI.id_group = GRO.id_group)
AND (VARI .id_color = TONE.id_color)
LEFT OUTER JOIN
color COL ON (COL.id_color = VARI.id_color)
OR (COL.id_color = TONE.id_color)
WHERE
GRO.id_group = 1
I think what you want is something more like:
SELECT
id_group,
id_color,
MAX(max_variety),
MAX(max_tone)
FROM
(SELECT
v.id_group,
v.id_color,
MAX(name_variety) AS max_variety,
MAX(name_tone) AS max_tone
FROM
variety V INNER JOIN
color c ON
V.id_color = c.id_color LEFT OUTER JOIN
tone t ON
t.id_group = v.id_group AND
t.id_color = V.id_color
WHERE
v.id_group = 1
GROUP BY
v.id_group,
v.id_color
UNION ALL
SELECT
t.id_group,
t.id_color,
MAX(name_variety) AS max_variety,
MAX(name_tone) AS max_tone
FROM
tone t INNER JOIN
color c ON
t.id_color = c.id_color LEFT OUTER JOIN
variety V ON
v.id_group = t.id_group AND
v.id_color = t.id_color
WHERE
t.id_group = 1
GROUP BY
t.id_group,
t.id_color) u
GROUP BY
id_group,
id_color
instead of using union you could use full join , as full join return all result from first table and also all results from the second tables.
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, Tone.name_tone from Variety VARI
join Color COL on col.id_color=VARI col.id
join Groups GRO on GRO.id_group=VARI.id_group
full join Tone on Tone.id_group=GRO.id_group
where GRO.id_group = 1

how to prevent duplicates with inner join query (Postgres)

I am trying to understand how to create a query to filter out some results based on an inner join.
Consider the following data:
formulation_batch
-----
id project_id name
1 1 F1.1
2 1 F1.2
3 1 F1.3
4 1 F1.all
formulation_batch_component
-----
id formulation_batch_id component_id
1 1 1
2 2 2
3 3 3
4 4 1
5 4 2
6 4 3
7 4 4
I would like to select all formulation_batch records with a project_id of 1, and has a formulation_batch_component with a component_id of 1 or 2. So I run the following query:
SELECT formulation_batch.*
FROM formulation_batch
INNER JOIN formulation_batch_component
ON formulation_batch.id = formulation_batch_component.formulation_batch_id
WHERE formulation_batch.project_id = 1
AND ((formulation_batch_component.component_id = 2
OR formulation_batch_component.component_id = 1 ))
However, this returns a duplicate entry:
1;"F1.1"
2;"F1.2"
4;"F1.all"
4;"F1.all"
Is there a way to modify this query so that I only get back the unique formulation_batch records which match the criteria?
EG:
1;"F1.1"
2;"F1.2"
4;"F1.all"
Thanks for your time!
In this case it is possible to apply the distinct before the join possibly making it more performant:
select fb.*
from
formulation_batch fb
inner join
(
select distinct formulationbatch_id
from formulation_batch_component
where component_id in (1, 2)
) fbc on fb.id = fbc.formulationbatch_id
where fb.project_id = 1
Notice how to use alias for the table names to make the query clearer. Also then in operator is very handy. The use of double quotes with those identifiers is not necessary.
One way would be to use distinct:
SELECT distinct "formulation_batch".*
FROM "formulation_batch"
INNER JOIN "formulation_batch_component"
ON "formulation_batch"."id" = "formulation_batch_component"."formulationBatch_id"
WHERE "formulation_batch"."project_id" = 1
AND (("formulation_batch_component"."component_id" = 2
OR "formulation_batch_component"."component_id" = 1 ))
I know the question asks how to prevent duplicates with inner join but could use an IN clause in the predicate.
SELECT "formulation_batch".*
FROM "formulation_batch" fb
ON "formulation_batch"."id" = "formulation_batch_component"."formulationBatch_id"
WHERE "formulation_batch"."project_id" = 1
AND fb.id IN (SELECT "formulation_batch"."id"
FROM formulation_batch_component
WHERE (("formulation_batch_component"."component_id" = 2
OR "formulation_batch_component"."component_id" = 1 ))

sql query for 3 tables

i have 3 tables (A,B,C)
Table A -
ID Name
1 Sam
2 Manuel
3 Jane
Table B
ID Tab_A_ID Name
1 1 Meer
2 1 Kutti
3 2 Mikaro
Table C
ID Tab_B_ID Price
1 1 255.11
2 1 30.52
3 3 125.22
I need a query that shall pick up the top price for TableA-Name from TableC. So only 1 top price for 1 nae record.
e.g.-
Sam - 255.11
Manuel - 125.22
How can i get this?
To get the max price per entry in A:
SELECT a.Name,
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, a.name
To get the max price per entry A per entry B:
SELECT a.Name,
b.Name
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, b.id, a.name, b.name
Note that entries in A without corresponding entires in B or entries in B without corresponding entries in C will not appear in the result. Use LEFT JOIN if you want to include these in the result.