The following query
DECLARE #SNH TABLE
(
dt date,
QueueName varchar(10),
SN varchar(10)
)
INSERT INTO #SNH (Dt, QueueName, SN)
VALUES ('2001-04-04', 'Queue01', 'Q01SN01'),
('2001-04-05', 'Queue01', 'Q01SN01'),
('2001-04-06', 'Queue01', 'Q01SN01'),
('2001-04-04', 'Queue02', 'Q02SN01'),
('2001-04-05', 'Queue02', 'Q02SN01'),
('2001-04-06', 'Queue02', 'Q02SN02')
DECLARE #QH TABLE
(
DT date,
QueueName varchar(10)
)
INSERT INTO #QH(DT, QueueName)
VALUES ('2001-04-04','Queue01'),
('2001-04-05','Queue01'),
('2001-04-06','Queue01'),
('2001-04-04','Queue02'),
('2001-04-05','Queue02'),
('2001-04-06','Queue02')
SELECT DISTINCT
q.QueueName clnQueueName,
MIN(q.Dt) OVER (PARTITION BY q.QueueName) clnStartDate,
MAX(q.Dt) OVER (PARTITION BY q.QueueName) clnEndDate,
s.SN
FROM
#QH q
LEFT JOIN
#SNH s ON s.QueueName = q.QueueName
returns this output:
clnQueueName
clnStartDate
clnEndDate
SN
Queue01
2001-04-04
2001-04-06
Q01SN01
Queue02
2001-04-04
2001-04-06
Q02SN01
Queue02
2001-04-04
2001-04-06
Q02SN02
which I'm aiming to aggregate into a comma separated list with
SELECT DISTINCT
q.QueueName clnQueueName,
MIN(q.Dt) OVER (PARTITION BY q.QueueName) clnStartDate,
MAX(q.Dt) OVER (PARTITION BY q.QueueName) clnEndDate,
STRING_AGG(s.SN,',')
FROM
#QH q
LEFT JOIN
#SNH s ON s.QueueName = q.QueueName AND s.Dt = q.Dt
as follows
clnQueueName
clnStartDate
clnEndDate
SN
Queue01
2001-04-04
2001-04-06
Q01SN01
Queue02
2001-04-04
2001-04-06
Q02SN01,Q02SN02
Instead I get:
Msg 8120, Level 16, State 1, Line 36
Column '#QH.QueueName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Sorry, I can't get it.
#QH.QueueName isn't even mentioned in the SELECT, only q.QueueName.
What I am missing here?
As per the docs, a group by clause is required if the string_agg is not the only item being selected. Once grouping correctly you no longer need window functions or distinct.
To obtain only distinct values of SN you need to group twice, the first time in a sub-query (in this case a CTE) to get distinct values of SN and the second time with string_agg to get distinct values of QueueName.
WITH cte AS (
SELECT
q.QueueName clnQueueName
, MIN(q.Dt) clnStartDate
, MAX(q.Dt) clnEndDate
, s.SN
FROM #QH q
LEFT JOIN #SNH s
ON s.QueueName = q.QueueName AND s.Dt = q.Dt
GROUP BY q.QueueName, s.SN
)
SELECT clnQueueName
, MIN(clnStartDate) clnStartDate
, MAX(clnEndDate) clnEndDate
, STRING_AGG(SN,',') SN
FROM cte
GROUP BY clnQueueName;
Returns:
clnQueueName
clnStartDate
clnEndDate
SN
Queue01
2001-04-04
2001-04-06
Q01SN01
Queue02
2001-04-04
2001-04-06
Q02SN01,Q02SN02
Related
I am running below query, sometimes these query skip few records to insert into newordersholdentry and didn't get any error.
but if run same query again after finding it is missed some records for order, it will insert all.
Please let me know what could be the reason.
INSERT INTO newordersholdentry
(itemid,
lookupcode,
description,
gamacode,
ordered,
IsForceItem,
Scanned,
Location,
SortOrder
)
SELECT ID,
ItemLookupCode,
Description,
GamaCode,
SUM(Qty) AS Qty,
ForceItem,
0 AS Scanned,
SubDescription1,
Sortorder
FROM
(
SELECT Item.ID,
Item.ItemLookupCode,
Item.Description,
NewOrderItems.GamaCode,
NewOrderItems.Qty,
Item.SubDescription1,
Item.Binlocation,
ISNULL(
(
SELECT TOP (1) SortSno
FROM NewOrderPickPackSorting
WHERE(Bin = LEFT(Item.SubDescription1, 3))
), 99999) AS Expr1,
0 AS ForceItem,
p.sortorder
FROM NewOrderItems(NOLOCK)
INNER JOIN Item(NOLOCK) ON NewOrderItems.GamaCode = Item.SubDescription2
LEFT OUTER JOIN pickpath(NOLOCK) p ON concat(RTRIM(p.aisle), '-', p.section) = UPPER(LEFT(item.subdescription1, 6))
WHERE(NewOrderItems.Discontinue = 0)
AND (NewOrderItems.OrderID = 123456)
) AS t
Group by ID
, ItemLookupcode
, Description
, GAMACODE
, ForceItem
, Expr1
, BinLocation
, SubDescription1
, sortorder
I am working on a SQL statement that will become a part of a view. What I need is to extract only the records that have the same unique key twice. The query looks like below right now.
select distinct
rscmaster_no_in, rsc_no_in, calendar_year, calendar_month,
Wstat_Abrv_Ch,
h.Wstat_no_in, Staffing_Calendar_Date, payhours,
l.OTStatus
from
vw_all_ts_hire h
left join
MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
where
rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty)
and Wstat_Abrv_Ch <> ''
and h.Wstat_no_in in (103, 107)
and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
order by
RscMaster_no_in
The result I get from the query above is:
I need to modify the SQL statement so that the end result is like below:
How can I modify the above statement to spit out the end result like that?
Use the analytic count(*) over () function.
with cte as (
select
count(*) over (partition by YourUniqueKey) as MyRowCount
{rest of your query}
)
select *
from cte
where MyRowCount = 2;
This should give you the results you want (performance is dependent on indexes/table design).
This takes your core logic and puts it into a sub select that only returns records that have a count > 1.
Then use those ID's to select all the data you need but only for those ID's that are in the sub select with count > 1
select distinct rscmaster_no_in,rsc_no_in, calendar_year, calendar_month,
Wstat_Abrv_Ch, h.Wstat_no_in, Staffing_Calendar_Date, payhours ,l.OTStatus
from vw_all_ts_hire h
left join MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
WHERE rscmaster_no_in IN (
SELECT rscmaster_no_in
from vw_all_ts_hire h
left join MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
where rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty)
and Wstat_Abrv_Ch <> ''
and h.Wstat_no_in in (103, 107)
and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
GROUP BY rscmaster_no_in
HAVING COUNT(*) > 1
)
order by RscMaster_no_in
You can use COUNT(*) OVER () window function such as
SELECT *
FROM
(
SELECT COUNT(*) OVER (PARTITION BY rscmaster_no_in) AS cnt,
t.*
FROM tab t
) t
WHERE cnt>1
AND OTStatus = 'ECCOTRemove'
This may help you :
select * from (
select distinct
rscmaster_no_in, rsc_no_in, calendar_year, calendar_month,
Wstat_Abrv_Ch,
h.Wstat_no_in, Staffing_Calendar_Date, payhours,
l.OTStatus,
SELECT COUNT(*) OVER (PARTITION BY rscmaster_no_in) AS uinqueCount
from
vw_all_ts_hire h
left join
MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
where
rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty)
and Wstat_Abrv_Ch <> ''
and h.Wstat_no_in in (103, 107)
and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
) innerReult
where uinqueCount=2 --Or uinqueCount>1 base on your business
order by
RscMaster_no_in
In my first query, I want to get CDept_Id. But CDept_Id column does not exist in inward_doc_tracking_hdr table.
It comes from inward_doc_tracking_trl table. like below
SELECT CDept_id
FROM inward_doc_tracking_trl
WHERE ref_mkey IN ( SELECT mkey
FROM inward_doc_tracking_hdr
WHERE doc_no = 'IW/HU/16/42' )
So, From this. I get CDept_Id. Now I want to add this in my below query.
SELECT mkey ,
Delivered_By ,
Department_Id ,
( SELECT mkey
FROM erp190516.dbo.emp_mst
WHERE mkey IN ( SELECT employee_mkey
FROM erp190516.dbo.user_mst
WHERE mkey = To_User )
) User_Id ,
Doc_Type ,
Email_Id ,
Ref_No ,
CONVERT(VARCHAR(25), Ref_date, 103) Ref_date ,
Inward_Amt ,
Remarks ,
party_name ,
disp_through
FROM erp190516.dbo.inward_doc_tracking_hdr ,
CDept_id -- add CDept_id here
WHERE doc_no = 'IW/HU/16/42'
AND Status_Flag = '13'
How to add this
UPDATE
inward_doc_tracking_hdr mkey is equal to inward_doc_tracking_trl ref_mkey
It is reading the magic glass bulb, but I think you might nead an INNER JOIN to the other table using the mkey and ref_mkey as link:
Select hdr.mkey
,hdr.Delivered_By
,hdr.Department_Id
,hdr.Doc_Type,Email_Id
,hdr.Ref_No
,convert(varchar(25),hdr.Ref_date,103) Ref_date
,hdr.Inward_Amt
,hdr.Remarks
,hdr.party_name
,hdr.disp_through
,trl.CDept_Id
from erp190516.dbo.inward_doc_tracking_hdr AS hdr
inner join erp190516.dbo.inward_doc_tracking_trl AS trl on hdr.mkey=trl.ref_mkey
where hdr.doc_no = 'IW/HU/16/42'
and hdr.Status_Flag = '13'
UPDATE ...even more guessing...
--First CTE to get the partioned order of CDept_Id
;WITH OrderedCDept AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY ref_mkey ORDER BY CDept_Id DESC) AS SortInx
,ref_mkey
,CDept_Id
FROM erp190516.dbo.inward_doc_tracking_trl
)
--Second CTE to use TOP 1 WITH TIES to fetch all first rows
,LatestCDept AS
(
SELECT TOP 1 WITH TIES *
FROM OrderedCDept
ORDER BY SortInx
)
--Now use the second CTE instead of the table to join
Select hdr.mkey
,hdr.Delivered_By
,hdr.Department_Id
,hdr.Doc_Type,Email_Id
,hdr.Ref_No
,convert(varchar(25),hdr.Ref_date,103) Ref_date
,hdr.Inward_Amt
,hdr.Remarks
,hdr.party_name
,hdr.disp_through
,trl.CDept_Id
from erp190516.dbo.inward_doc_tracking_hdr AS hdr
inner join LatestCDept AS trl on hdr.mkey=trl.ref_mkey
where hdr.doc_no = 'IW/HU/16/42'
and hdr.Status_Flag = '13'
Below is my query that fetches the fields based on the left join of 3 tables. My requirement is to get all the fields based on the recent SystemDateTime in table Debug.T. For example, if i try it for HardwareId = 550803413, it returns 2 records with 2 different SystemDateTime. I need to filter it so that I get only 1 record for all HardwareIds based on recent SystemDateTime. Data is stored in Google Big Query.
Any help would be appreciated.
SELECT HardwareId, e.Carrier, max(d.SystemDateTime) as DateTime,
CASE
WHEN lower(DebugData) LIKE 'veri%' THEN 'Verizon'
WHEN REGEXP_MATCH(lower(DebugData),'\\d+') THEN c.Network
END
AS ActualData
FROM (
SELECT
HardwareId, SystemDateTime, max(SystemDateTime) as max_date,
INTEGER(RTRIM(SUBSTR(REGEXP_REPLACE(REGEXP_REPLACE(DebugData,'\\"',' '), '\\?',' ') ,0,3))) AS d1,
INTEGER(RTRIM(SUBSTR(REGEXP_REPLACE(DebugData,'[^a-zA-Z0-9]',' '),4,LENGTH(DebugData)-3))) AS d2
FROM TABLE_DATE_RANGE([Debug.T],TIMESTAMP('2016-05-16'),TIMESTAMP('2016-05-16'))
GROUP BY HardwareId, DebugReason, DebugData, SystemDateTime
HAVING DebugReason = 31) AS d
LEFT JOIN
(
SELECT Mcc, Mnc as Mnc, Network from [Debug.Carrier]
) As c
ON c.Mcc = d.d1 and c.Mnc = d.d2
INNER JOIN
(
SELECT VehicleId, APNCarrier FROM [Info_20160516]
) As e
ON d.HardwareId = e.VehicleId
GROUP BY HardwareId, ActualData, e.Carrier
HAVING HardwareId = 550803413
Current output:
HardwareId DebugReason DebugData e_APNCarrier DateTime ActualDebugData
550473814 50013 23430"? Unknown 2016-05-16 08:09:09.534597 Everyth. Ev.wh./T-Mobile
550473814 50013 23410"? Unknown 2016-05-16 07:50:48.526288 O2 Ltd.
550473814 50013 23415"? Unknown 2016-05-16 23:54:37.487154 Vodafone
Expected output:
Since the recent SystemDateTime is 23:54:37.487154, query should filter the records based on the recent SystemDateTime and provide the result.
HardwareId DebugReason DebugData e_APNCarrier DateTime ActualDebugData
550473814 50013 23415"? Unknown 2016-05-16 23:54:37.487154 Vodafone
so you just want the latest record per HardwareId based on DateTime? Try this:
SELECT * FROM (
SELECT HardwareId, e.Carrier, d.SystemDateTime as DateTime,
CASE
WHEN lower(DebugData) LIKE 'veri%' THEN 'Verizon'
WHEN REGEXP_MATCH(lower(DebugData),'\\d+') THEN c.Network
END
AS ActualData,
ROW_NUMBER() OVER (PARTITION BY HARDWAREID ORDER BY d.SystemDateTime desc) RN
FROM (
SELECT
HardwareId, SystemDateTime, max(SystemDateTime) as max_date,
INTEGER(RTRIM(SUBSTR(REGEXP_REPLACE(REGEXP_REPLACE(DebugData,'\\"',' '), '\\?',' ') ,0,3))) AS d1,
INTEGER(RTRIM(SUBSTR(REGEXP_REPLACE(DebugData,'[^a-zA-Z0-9]',' '),4,LENGTH(DebugData)-3))) AS d2
FROM TABLE_DATE_RANGE([Debug.T],TIMESTAMP('2016-05-16'),TIMESTAMP('2016-05-16'))
GROUP BY HardwareId, DebugReason, DebugData, SystemDateTime
HAVING DebugReason = 31) AS d
LEFT JOIN
(
SELECT Mcc, Mnc as Mnc, Network from [Debug.Carrier]
) As c
ON c.Mcc = d.d1 and c.Mnc = d.d2
INNER JOIN
(
SELECT VehicleId, APNCarrier FROM [Info_20160516]
) As e
ON d.HardwareId = e.VehicleId
HAVING HardwareId = 550803413
)
WHERE RN = 1
I would like to PIVOT the following query result to display a column for each Project Status Code.
WITH r AS (
SELECT ROW_NUMBER() OVER (ORDER BY ph.InsertedDateTime) rownum,
CAST(ph.InsertedDateTime AS DATE) InsertedDate, ph.Gate_1_TargetDate, ph.Gate_2_TargetDate, ph.Gate_3_TargetDate
FROM PROJECT_HIST ph
JOIN (
SELECT ProjectID, MAX(InsertedDateTime) InsertedDateTime
FROM PROJECT_HIST
GROUP BY ProjectID, CAST(InsertedDateTime AS DATE)
) ph_distinct_date ON ph_distinct_date.InsertedDateTime = ph.InsertedDateTime
AND ph_distinct_date.ProjectID = ph.ProjectID
WHERE ph.projectid = 100957
AND NOT (
ph.Gate_1_TargetDate IS NULL
AND ph.Gate_2_TargetDate IS NULL
AND ph.Gate_3_TargetDate IS NULL
)
),
fubar AS (
SELECT rownum, InsertedDate, 0 gateName, NULL targetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 1, Gate_1_TargetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 2, Gate_2_TargetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 3, Gate_3_TargetDate FROM r
)
SELECT f1.InsertedDate 'Change Date', f1.gateName 'ProjectStageCode', f1.targetDate
FROM fubar f1
LEFT JOIN fubar f2 ON f2.rownum = f1.rownum - 1
AND f2.gateName = f1.gateName
PIVOT(min(f1.InsertedDate) FOR f1.gateName IN ([0],[1],[2],[3])) AS p
WHERE f1.rownum = 1
OR f1.targetDate <> f2.targetDate
ORDER BY f1.InsertedDate
;
Without the pivot attempt, this query currently returns this result for this particular project:
What I would like to do is pivot the query to create columns for each Project Stage Code to match the following result:
Essentially, I need to have a row for each unique Change Date and have the targetDate column value fill in the respective newly pivoted numerical ProjectStageCode column.
From the looks of it it seems like you just need to use a subquery before you try to PIVOT the data. You also need to aggregate the targetDate instead of the InsertedDate:
WITH r AS
(
SELECT ROW_NUMBER() OVER (ORDER BY ph.InsertedDateTime) rownum,
CAST(ph.InsertedDateTime AS DATE) InsertedDate, ph.Gate_1_TargetDate, ph.Gate_2_TargetDate, ph.Gate_3_TargetDate
FROM PROJECT_HIST ph
JOIN
(
SELECT ProjectID, MAX(InsertedDateTime) InsertedDateTime
FROM PROJECT_HIST
GROUP BY ProjectID, CAST(InsertedDateTime AS DATE)
) ph_distinct_date
ON ph_distinct_date.InsertedDateTime = ph.InsertedDateTime
AND ph_distinct_date.ProjectID = ph.ProjectID
WHERE ph.projectid = 100957
AND NOT (ph.Gate_1_TargetDate IS NULL
AND ph.Gate_2_TargetDate IS NULL
AND ph.Gate_3_TargetDate IS NULL)
),
fubar AS
(
SELECT rownum, InsertedDate, 0 gateName, NULL targetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 1, Gate_1_TargetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 2, Gate_2_TargetDate FROM r
UNION ALL
SELECT rownum, InsertedDate, 3, Gate_3_TargetDate FROM r
)
SELECT ChangeDate, [0],[1],[2],[3]
FROM
(
SELECT f1.InsertedDate ChangeDate, f1.gateName, f1.targetDate
FROM fubar f1
LEFT JOIN fubar f2
ON f2.rownum = f1.rownum - 1
AND f2.gateName = f1.gateName
WHERE f1.rownum = 1
OR f1.targetDate <> f2.targetDate
) d
PIVOT
(
min(targetDate)
FOR gateName IN ([0],[1],[2],[3])
) AS p;