TABLE Laptop_Shift_Departments
id Laptop_ID Curr_department Pre_Department
-----|----------|-----------------|----------------
9 71 4 3
10 68 4 3
11 71 5 4
12 68 5 4
User only search PO Number and against this PO_Num there are 2 laptops (Laptop_ID)
Above is my table now i want to get MAX value Against this laptop_ID ID
I have tried this:
SELECT LD.ID AS Laptop_ID,ld.GulfITBarcode,po.ID as PO_ID,PO.PO_Number as PO_Number,D.Department,
D.ID as Crr_DepID1,d2.Department,D2.ID as Pre_DepID2
FROM PO_PURCHASEORDER PO
INNER JOIN PO_Laptop_Master LM
ON LM.PO_ID = PO.ID
INNER JOIN PO_LaptopDetail LD
ON LD.LapTop_Master_ID = LM.ID
INNER JOIN Laptop_Shift_Departments DP
ON DP.Laptop_Detail_ID = LD.ID
inner join Laptop_Departments d
on d.ID = DP.Current_Dep_ID
inner join Laptop_Departments d2
on d2.ID = DP.Previous_Dep_ID
WHERE PO_NUMBER = '5258'
AND
LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh)
But it is not working
MY output should be like this:
id Laptop_ID Curr_department Pre_Department
-----|----------|-----------------|----------------
11 71 5 4
12 68 5 4
You'll want to use the 'Group By' function at the end of the fields you want to group by and the 'Max' function in the Select on the columns you want the 'Max' (or 'Min' or 'Avg'). Should look something like this:
SELECT
LD.ID AS Laptop_ID
,ld.GulfITBarcode
,po.ID as PO_ID
,PO.PO_Number as PO_Number
,D.Department
,MAX(D.ID) as Crr_DepID1
,d2.Department
,MAX(D2.ID) as Pre_DepID2
FROM PO_PURCHASEORDER PO
INNER JOIN PO_Laptop_Master LM
ON LM.PO_ID = PO.ID
INNER JOIN PO_LaptopDetail LD
ON LD.LapTop_Master_ID = LM.ID
INNER JOIN Laptop_Shift_Departments DP
ON DP.Laptop_Detail_ID = LD.ID
inner join Laptop_Departments d
on d.ID = DP.Current_Dep_ID
inner join Laptop_Departments d2
on d2.ID = DP.Previous_Dep_ID
WHERE PO_NUMBER = '5258'
AND LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh)
AND DP.Current_Dep_ID = 5
GROUP BY
LD.ID
,ld.GulfITBarcode
,po.ID
,PO.PO_Number
,D.Department
,d2.Department
(Having not seen the table structure I'm not sure as to the rest of the query. Hope this helps.)
Related
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
one contain multiple columns and test two tables have one columns.
Table: Client
Name age Benefit Id code value
Tom 33 AA 0A 1 12
Tom 33 AB 0C 1 13
Tom 33 AA 0C 5 11
Sam 31 CC 0B 3 10
Rik 28 EE 0D 5 9
Table2: Sell
Code1
1
4
Table3: tip
Code2
5
6
I want output as Name,Age, Benefit, Id and Code, which code present in both table "sell" and "tip".
Name Age Benefit Id code Approved
Tom 33 AA 0A 1 7
Tom 33 AA 0C 5 11
Code I have written as
------Break code-----------
Create table #temp1 as
select c.* from Client c inner join Sell s on c.code1 = s.code
where Benifit= AA
Create table #temp2 as
select c.* from Client c inner join Sell s on c.code2 = s.code
where Benifit= AA
Create table #combine as
select ss.* ,
,case when ss.value > 5 then ss.value- 5 Else 0 approved
from #temp1 ss inner join #temp2 ff
On ss.Name= ff.Name
AND ss.Age= ff.Age
where Benifit= 'AA'
Group by ss.Name Age
------------------------------------------------
----Since 3 table are created with repeated logic, I have put Above code is put into one code, even thought it is not optimized ----------------
select ss.Name, ss.age
,case when ss.value > 5 then ss.value- 5 Else 0 approved
from
(select * from Client c inner join Sell s on c.code1 = s.code
where Benifit= AA) ss
inner join
(select * from Client cc inner join Tip t on cc.code2 = t.code
where Benifit= AA) ff
On ss.Name= ff.Name
AND ss.Age= ff.Age
Group by Name Age
So, I have two problem:
I want Benefit, Id, code and value beside Name and Age, since current query not allow to do that as 'Group By' is used
I am using two select multiple inner join as below
a. (select * from Client c inner join Sell s on c.code = s.code1
where Benifit= AA) ss
b. (select * from Client cc inner join Tip t on cc.code2 = t.code
where Benifit= AA) ff
but I don't want my code to repeatedly use the same code as above is repeated.
A new column Approved is created by subtracting 5 from Value column
Note: The output will Inner join based on "Name" and "Age" between
Inner join of Client & Sell
Inner join between Client & Tip
Please suggest.
Note I want Name, Age having code in both 'Sell' and 'Tip' where Benifit= AA Although output will be from "Sell".
I think you want a query like this -
SELECT C.Name, C.Age, C.Benefit, C.Id, C.Code, (C.Value - 5) Approved
FROM Client C
INNER JOIN Sell S ON C.Code = S.Code1
INNER JOIN Tip T ON S.Code1 = T.Code2
WHERE C.Benefit = 'AA'
Hi every one.
I have a DB shown in the following picture.
enter image description here
Every season, every personnel could have one or more posts and every post is in a specific unit. My question is how to calculate weighted AVG for every personnel in each related posts (note that we have two types of criteria where ct.ID = 1 OR 2). I did something as below
select r.EvaluaTEDID,r.PostID, Avg(r.FirstScore*iif(ct.id=1,p.COMCrtWeight/100,0))+
Avg(r.FirstScore*iif(ct.id=2,p.PROCrtWeight/100,0)) as 'AVG'
from results r
inner join post p on r.PostID = p.ID
inner join job j on p.JobID = j.ID
inner join subcrt sc on r.SubCrtID = sc.ID
inner join Crt c on sc.CrtID = c.ID
inner join CrtType ct on c.CrtTypeID = ct.ID
where r.SeasonID = 4 and j.UnitID = 3068
group by r.EvaluaTEDID, r.PostID
but MSSQLS showed 46 which is the weighted average of all records for personnel with ID=8. The desired answer should be 87 (36+51)
COMCrtWeight=40,PROCrtWeight=60
ct.ID for SubCID=6 is 2 and ct.ID for SubCID=1 & 4 is 1
seasonID - EvaluaTEDID - SubCID - PostID - Score
4 ------------ 8 ------------------ 6 ----------- 26 ------ 90
4 ------------ 8 ------------------ 1 ----------- 26 ------ 90
4 ------------ 8 ------------------ 4 ----------- 26 ------ 90
I also did something like this:
select EvaluaTEDID, sum(WeightedSum) from
(select r.EvaluaTEDID,r.PostID,
avg(r.FirstScore*iif(ct.id=1,p.COMCrtWeight/100,p.PROCrtWeight/100))
as WeightedSum from results r
inner join post p on r.PostID = p.ID
inner join job j on p.JobID = j.ID
inner join subcrt sc on r.SubCrtID = sc.ID
inner join Crt c on sc.CrtID = c.ID
inner join CrtType ct on c.CrtTypeID = ct.ID
where r.SeasonID = 4 and j.UnitID = 3068
group by ct.id, r.EvaluaTEDID, r.PostID)
group by EvaluaTEDID
Buy I got syntax error.
Could you please help.
Thanks in advance.
I have a SQL query in which I am using INNER JOIN but I have to calculate the string values ERROR, OK and their total from column d.sd_figure.
This is my SQL query
SELECT
count(d.sd_figure = 'ERROR') AS error,
count(d.sd_figure = 'OK') AS OK count,
count(*) AS total,
m.contr_num, bu.buyer_name,
m.S_remarks, cr.contr_name, br.brand_name,
m.S_date, c.colour_name, s.size_name,
mn.manu_name
FROM
Scanning_M m
INNER JOIN
colour c ON m.color_id = c.colour_id
INNER JOIN
Buyer bu ON m.buyer_id = bu.Byer_ID
INNER JOIN
Scanning_D d ON m.S_id = d.S_id
INNER JOIN
Brand br ON m.Brand_id = br.Brand_id
INNER JOIN
Contract cr ON m.Contr_num = cr.Contr_id
INNER JOIN
Size s ON m.Size_id = s.Size_id
INNER JOIN
Manufacturer mn ON m.Manu_id = mn.manu_id
WHERE
m.S_date BETWEEN '2016-01-13' AND '2016-01-13'
I also want to add count of 'error' , 'OK' and 'Total' too error is in this line
SELECT
count(d.sd_figure = 'ERROR') AS error,
count(d.sd_figure = 'OK') as OK count,
count(*) as total ,
I tried many solution butt returned group by not found etc
SAMPLE DATA DEMANDED FOR d.sd_figure
S_id sd_res sd_figure sd_datetime sd_id
4 456456 ERROR 2016-01-09 03:11:07.000 1
4 456456 ERROR 2016-01-09 03:11:07.000 2
4 456456 ERROR 2016-01-09 03:11:07.000 3
6 123 ERROR 2016-01-09 10:54:47.000 22
6 123 ERROR 2016-01-09 10:54:47.000 23
6 123 ERROR 2016-01-09 10:54:47.000 24
6 123 ERROR 2016-01-09 10:54:48.000 25
6 123 ERROR 2016-01-09 10:54:48.000 26
Untested, but should be something like this:
SELECT sum(CASE WHEN d.sd_figure = 'ERROR' THEN 1 ELSE 0 END) AS error
,sum(CASE WHEN d.sd_figure = 'OK' THEN 1 ELSE 0 END) AS [OK count]
,count(*) AS total
,m.contr_num
,bu.buyer_name
,m.S_remarks
,cr.contr_name
,br.brand_name
,m.S_date
,c.colour_name
,s.size_name
,mn.manu_name
FROM Scanning_M m
INNER JOIN colour c ON m.color_id = c.colour_id
INNER JOIN Buyer bu ON m.buyer_id = bu.Byer_ID
INNER JOIN Scanning_D d ON m.S_id = d.S_id
INNER JOIN Brand br ON m.Brand_id = br.Brand_id
INNER JOIN Contract cr ON m.Contr_num = cr.Contr_id
INNER JOIN Size s ON m.Size_id = s.Size_id
INNER JOIN Manufacturer mn ON m.Manu_id = mn.manu_id
WHERE m.S_date BETWEEN '2016-01-13' AND '2016-01-13'
GROUP BY m.contr_num
,bu.buyer_name
,m.S_remarks
,cr.contr_name
,br.brand_name
,m.S_date
,c.colour_name
,s.size_name
,mn.manu_name
Here is my query:
SELECT Count(*) AS CountOfJoker, tblPrediction.GameID
FROM (tblUser INNER JOIN (tblPool INNER JOIN tblUserPool ON tblPool.PoolID = tblUserPool.PoolID) ON tblUser.UserID = tblUserPool.UserID) INNER JOIN (tblResult INNER JOIN tblPrediction ON tblResult.GameID = tblPrediction.GameID) ON tblUser.UserID = tblPrediction.UserID
GROUP BY tblPrediction.GameID, tblPrediction.Joker, tblResult.GroupName, tblPool.PoolID
HAVING (((tblPrediction.Joker)=True) AND ((tblResult.GroupName)='A') AND ((tblPool.PoolID)=314));
Here is the output:
CountOfJoker GameID
2 1
2 2
2 9
1 10
1 17
3 18
I would like the MAX of CountOfJoker with the following output:
CountOfJoker GameID
3 18
Thanks
Drew
You could use select top and order by ... desc
SELECT TOP 1 Count(*) AS CountOfJoker, tblPrediction.GameID
FROM (tblUser INNER JOIN (tblPool INNER JOIN tblUserPool ON tblPool.PoolID = tblUserPool.PoolID) ON tblUser.UserID = tblUserPool.UserID) INNER JOIN (tblResult INNER JOIN tblPrediction ON tblResult.GameID = tblPrediction.GameID) ON tblUser.UserID = tblPrediction.UserID
GROUP BY tblPrediction.GameID, tblPrediction.Joker, tblResult.GroupName, tblPool.PoolID
HAVING (((tblPrediction.Joker)=True) AND ((tblResult.GroupName)='A') AND ((tblPool.PoolID)=314));
ORDER BY Count(*) DESC