I have 2 Queries as below:
1st Query
Expr1
SumOfBRK
08/03/2021
32499
09/03/2021
2687
28/02/2021
355
SELECT Format([TBL_BRK_DATA_OUTPUT].[DATE],"Short Date") AS Expr1, Sum(TBL_BRK_DATA_OUTPUT.QUANTITY) AS SumOfBRK FROM TBL_BRK_DATA_OUTPUT GROUP BY Format([TBL_BRK_DATA_OUTPUT].[DATE],"Short Date");
2nd Query
Expr1
SumOfSTD
08/03/2021
6178
09/03/2021
4135
28/02/2021
2845
SELECT Format([TBL_STD_DATA_OUTPUT].[DATE],"Short Date") AS Expr1, Sum(TBL_STD_DATA_OUTPUT.QUANTITY) AS SumOfSTD FROM TBL_STD_DATA_OUTPUT GROUP BY Format([TBL_STD_DATA_OUTPUT].[DATE],"Short Date");
I expect the result as follows:
Expr1
SumOfSTD
SumOfBRK
08/03/2021
6178
32499
09/03/2021
4135
2687
28/02/2021
2845
355
Anyone can help me ? I tried to use the union to combine them but the result was not what I expected
Thank you.
Bosco
It sounds like you want a join here, rather than a union:
SELECT t1.Expr1, t2.SumOfSTD, t1.SumOfBRK
FROM
(
SELECT FORMAT([TBL_BRK_DATA_OUTPUT].[DATE], "Short Date") AS Expr1,
SUM(TBL_BRK_DATA_OUTPUT.QUANTITY) AS SumOfBRK
FROM TBL_BRK_DATA_OUTPUT
GROUP BY FORMAT([TBL_BRK_DATA_OUTPUT].[DATE], "Short Date")
) t1
INNER JOIN
(
SELECT FORMAT([TBL_STD_DATA_OUTPUT].[DATE], "Short Date") AS Expr1,
SUM(TBL_STD_DATA_OUTPUT.QUANTITY) AS SumOfSTD
FROM TBL_STD_DATA_OUTPUT
GROUP BY FORMAT[TBL_STD_DATA_OUTPUT].[DATE], "Short Date")
) t2
ON t2.Expr1 = t1.Expr1;
Related
SELECT mark.student_id,
.
.
.
MAX(mark.SAVE_TIME) AS SAVE_TIME,
(SELECT tamil FROM mark WHERE SAVE_TIME = (MAX(mark.SAVE_TIME))) AS tamilmark,
(SELECT english FROM mark WHERE SAVE_TIME = (MAX(mark.SAVE_TIME))) AS englishmark
FROM
(.......
)
Above query will return the my expected data. but the problem is two times I'm fetching the data from same table. How can i achieve with single select statement?
Here is one way that could be rewritten:
SELECT MySubTable.*, mark.tamil, mark.english
(
SELECT student_id,
MAX(mark.SAVE_TIME) AS SAVE_TIME
FROM Table
GROUP BY student_id
) MySubTable
INNER JOIN mark
ON mark.SAVE_TIME = MySubTable.SAVE_TIME
You can use one select statement to get all values, but you also will get NULLs (in my example I gave them 'n/a' value). To get rid of them you will have to select this subquery:
SELECT *
FROM (
SELECT DISTINCT mark.student_id,
MAX(MARK.SAVE_TIME) OVER() AS SAVE_TIME,
CASE WHEN SAVE_TIME = (MAX(mark.SAVE_TIME) OVER()) THEN tamil ELSE 'n/a' END AS tamilmark,
CASE WHEN SAVE_TIME = (MAX(mark.SAVE_TIME) OVER()) THEN english ELSE 'n/a' END AS englishmark
FROM MARK
) AS t
WHERE tamilmark != 'n/a'
SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
INTO pat_primary_provider
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM;
SELECT ppp.*
INTO ppp1
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM pat_primary_provider GROUP BY PAT_KEY) AS x
INNER JOIN pat_primary_provider AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;
I need to get the results of ppp1 only using one query (no INTO statements) in SQL Server. Please help.
Simply put the first query into a CTE (without the INTO clause). Then select from that.
;WITH pat_primary_provider AS
(
-- The first query goes here
)
-- The second query goes here
But something like below might also return the PAT_KEY's with the maximum PROV_NPI:
SELECT TOP 1 WITH TIES
PAT_KEY,
MAX(PROV_NPI) AS [Max_Provider_ID],
CONCAT(LAST_NM,' ',FIRST_NM) AS [Patient_Provider_Full_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP = 1
AND PAT_KEY IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY PAT_KEY, LAST_NM, FIRST_NM
ORDER BY row_number() over (order by MAX(PROV_NPI) desc);
Whats wrong with just inserting the first query as subqueries into the second?
SELECT ppp.*
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) GROUP BY PAT_KEY) AS x
INNER JOIN (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;
I have a huge query that creates a cte of warehouses and uses this to create 3 more cte's with the same column names.
below is my T-Sql query:
With WhList AS (SELECT Company, Warehouse FROM
CCWMS.dbo.WMSWarehouse_Control),
CoB AS (SELECT 'B' AS Company, B_IMB.Warehouse, B_IMB.StockCode, B_IMB.Bin, B_IMB.QtyOnHand1 AS QtyOnHand, B_IMB.SoQtyToShip, B_IMB.LastReceiptDate,B_IMB.OnHold FROM SysproCompanyB.dbo.InvMultBin AS B_IMB RIGHT JOIN WhList As WL ON B_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0),
CoX AS (SELECT 'X' AS Company, X_IMB.Warehouse, X_IMB.StockCode, X_IMB.Bin, X_IMB.QtyOnHand1 AS QtyOnHand, X_IMB.SoQtyToShip, X_IMB.LastReceiptDate,X_IMB.OnHold FROM SysproCompanyX.dbo.InvMultBin AS X_IMB RIGHT JOIN WhList As WL ON X_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0),
CoH AS (SELECT 'H' AS Company, H_IMB.Warehouse, H_IMB.StockCode, H_IMB.Bin, H_IMB.QtyOnHand1 AS QtyOnHand, H_IMB.SoQtyToShip, H_IMB.LastReceiptDate,H_IMB.OnHold FROM SysproCompanyH.dbo.InvMultBin AS H_IMB RIGHT JOIN WhList As WL ON H_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0)
I need to compile these three cte's into one result set.
Any way I might be able to achieve this. I have thought about PIVOT but cant seem to wrap my head around how to do this.
I think you are looking for union all of all the results... What do you mean by combining the results?
Are you looking something like below?
SELECT Company, Warehouse FROM (
SELECT 'B' AS Company, B_IMB.Warehouse, B_IMB.StockCode, B_IMB.Bin, B_IMB.QtyOnHand1 AS QtyOnHand, B_IMB.SoQtyToShip, B_IMB.LastReceiptDate,B_IMB.OnHold FROM SysproCompanyB.dbo.InvMultBin AS B_IMB RIGHT JOIN WhList As WL ON B_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0
UNION ALL
SELECT 'X' AS Company, X_IMB.Warehouse, X_IMB.StockCode, X_IMB.Bin, X_IMB.QtyOnHand1 AS QtyOnHand, X_IMB.SoQtyToShip, X_IMB.LastReceiptDate,X_IMB.OnHold FROM SysproCompanyX.dbo.InvMultBin AS X_IMB RIGHT JOIN WhList As WL ON X_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0
UNION ALL
SELECT 'H' AS Company, H_IMB.Warehouse, H_IMB.StockCode, H_IMB.Bin, H_IMB.QtyOnHand1 AS QtyOnHand, H_IMB.SoQtyToShip, H_IMB.LastReceiptDate,H_IMB.OnHold FROM SysproCompanyH.dbo.InvMultBin AS H_IMB RIGHT JOIN WhList As WL ON H_IMB.Warehouse=WL.Warehouse WHERE QtyOnHand1 <> 0
) A
I solved it. I cant believe I didn't think of this. Answer as follows:
--CTE's end here.
SELECT * FROM CoB
UNION
SELECT * FROM CoX
UNION
SELECT * FROM CoH
Thanks though
I have 2 tables item and memo. In item, itemId is the PK and the itid is the FK. In memo the memID is the PK.
I created a view:
SELECT
dbo.memo.memID, dbo.memo.fullname, dbo.memo.company,
dbo.memo.department, dbo.memo.MRnum, dbo.memo.date,
dbo.memo.returndate, dbo.memo.remarks,
dbo.memo.issuedby, dbo.memo.picture, dbo.item.itemID AS Expr1,
dbo.item.Itemnumber, dbo.item.description, dbo.item.qty,
dbo.item.unitofmeasure, dbo.item.itid
FROM
dbo.memo
INNER JOIN
dbo.item ON dbo.memo.memID = dbo.item.itid
WHERE
(dbo.memo.department = N'tsd')
and output is this
this is the output
I just want 1 output of the fullname, company, department, MRnum, date , remarks, issuedby every multiple rows of item
enter image description here
Hope you understand . thank you
You can use DISTINCT in your query.
See below
SELECT DISTINCT dbo.memo.memID, dbo.memo.fullname, dbo.memo.company, dbo.memo.department, dbo.memo.MRnum, dbo.memo.date, dbo.memo.returndate, dbo.memo.remarks, dbo.memo.issuedby, dbo.memo.picture, dbo.item.itemID AS Expr1, dbo.item.Itemnumber, dbo.item.description, dbo.item.qty, dbo.item.unitofmeasure, dbo.item.itid
FROM dbo.memo
INNER JOIN dbo.item ON dbo.memo.memID = dbo.item.itid
WHERE (dbo.memo.department = N'tsd')
i just want , 1 output of the fullname, company, department, MRnum,
date , remarks, issuedby with multiple rows of item
You could do this, use group by on required columns and take top 1
SELECT TOP 1
dbo.memo.fullname ,
dbo.memo.company ,
dbo.memo.department ,
dbo.memo.MRnum ,
dbo.memo.date ,
dbo.memo.remarks ,
dbo.memo.issuedby
FROM dbo.memo
INNER JOIN dbo.item
ON dbo.memo.memID = dbo.item.itid
WHERE ( dbo.memo.department = N'tsd' )
GROUP BY dbo.memo.fullname ,
dbo.memo.company ,
dbo.memo.department ,
dbo.memo.MRnum ,
dbo.memo.date ,
dbo.memo.remarks ,
dbo.memo.issuedby
Also you can use TOP like below if you are certain you get all duplicate records.
SELECT TOP 1 dbo.memo.memID, dbo.memo.fullname, dbo.memo.company, dbo.memo.department, dbo.memo.MRnum, dbo.memo.date, dbo.memo.returndate, dbo.memo.remarks, dbo.memo.issuedby, dbo.memo.picture, dbo.item.itemID AS Expr1, dbo.item.Itemnumber, dbo.item.description, dbo.item.qty, dbo.item.unitofmeasure, dbo.item.itid
FROM dbo.memo
INNER JOIN dbo.item ON dbo.memo.memID = dbo.item.itid
WHERE (dbo.memo.department = N'tsd')
I have to calculate the sum of total count value for each distinct arrival_time (column in database table) corresponding to column system_name. My sql query is:
SELECT system_name, COUNT(distinct arrival_time) AS c
FROM i2alarmlog
WHERE Ack_status = 0
AND Direction='CAME'
AND system_name in('I2-tciu database',
'i2-vcs logging',
'Indus1 Vacuum',
'Indus2 TCIU',
'Indus2 Vacuum',
'Septum_SIP2',
'TL3 Vacuum')
GROUP BY system_name
UNION ALL
SELECT 'sum' system_name,
Count(distinct arrival_time)
FROM i2alarmlog
WHERE Ack_status=0
AND Direction='CAME'
AND system_name in( 'I2-tciu database'
,'i2-vcs logging',
'Indus1 Vacuum',
'Indus2 Vacuum',
'Septum_SIP2',
'TL3 Vacuum')
When I run this sql query then sum is shown as 1841 but actually its 1845.
i2-vcs logging 2
I2-tciu database 2
Indus1 Vacuum 19
Indus2 TCIU 120
Indus2 Vacuum 1691
Septum_SIP2 8
TL3 Vacuum 3
sum 1841
In your second query, the distinct part is applied to all data. That means, if you have the same arrival time for 2 different products, it will only be counted once, and not once for each product. That would explain the difference between your expected and actual results.
What you could do instead is this:
;with cte as
(SELECT system_name, COUNT(distinct arrival_time) AS c
FROM i2alarmlog
WHERE Ack_status = 0
AND Direction='CAME'
AND system_name in('I2-tciu database','i2-vcs logging','Indus1 Vacuum','Indus2 TCIU','Indus2 Vacuum','Septum_SIP2','TL3 Vacuum')
GROUP BY system_name )
select system_name, c
from cte
union
select 'sum', sum(c) as c
from cte
Demo
One solution is by using an inline view and calculate the total sum like so:
SELECT system_name, COUNT(distinct arrival_time) AS c
FROM i2alarmlog
WHERE Ack_status = 0
AND Direction='CAME'
AND system_name in('I2-tciu database','i2-vcs logging','Indus1 Vacuum','Indus2 TCIU','Indus2 Vacuum','Septum_SIP2','TL3 Vacuum')
GROUP BY system_name
UNION ALL
select 'TotalSum' as TotalSum, sum(s.c) as TotalValue
from
(
SELECT system_name, COUNT(distinct arrival_time) AS c
FROM i2alarmlog
WHERE Ack_status = 0
AND Direction='CAME'
AND system_name in('I2-tciu database','i2-vcs logging','Indus1 Vacuum','Indus2 TCIU','Indus2 Vacuum','Septum_SIP2','TL3 Vacuum')
GROUP BY system_name
) s
With this solution the maximum agregation level (2) was reached ( sum(count(value)) ).