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)) ).
Related
scenario: I have table A and Table B. Both have the primary key of Rep_time_frame. We replicate data by terms. however, that process at times was repeated.
task: Match the source counts with the destination table counts.
problem: I can match the rows of the source table but not the destination table because the replication for some terms happened twice. So, my counts are off.
SELECT d.REPT_TIME_FRAME,
c.SOURCE_TOTAL_RECORDS,
COUNT(*) TOTAL_RECORDS,
CASE
WHEN COUNT(*) > c.SOURCE_TOTAL_RECORDS THEN c.SOURCE_TOTAL_RECORDS
ELSE COUNT(*)
END FINAL_TOTAL_RECORDS,
***CASE
WHEN EXISTS (SELECT REPT_TIME_FRAME
FROM TableA_Counts
WHERE REPT_TIME_FRAME IN (201705, 201708, 201801, 201706, 201710, 201803)
GROUP BY REPT_TIME_FRAME
HAVING COUNT(*) > 1) THEN 'UPLOADED MORE THAN ONCE'
ELSE'UPLOADED ONCE'
END NUMBER_OF_REPLICATIONS***
FROM TableA d
INNER JOIN TableA_Counts c ON (d.REPT_TIME_FRAME = c.REPT_TIME_FRAME)
WHERE d.REPT_TIME_FRAME IN (201705, 201708, 201801, 201706, 201710, 201803)
GROUP BY d.REPT_TIME_FRAME, c.SOURCE_TOTAL_RECORDS```
please note that the bolded area, when ran returns for the newly made column Number_of_replications "Uploaded More than once" on all counts. For the purposes of this example. Here are the facts.
201705 was replicated 2X
201801 once and 201708 once.
expected result. I want to have 201705 'Uploaded more than once', and the rest 201801 and 201708 'Uploaded Once'
Does this help?
http://sqlfiddle.com/#!18/8692a/1
I always try and get the counts then do a case statement on the final figures. I've based that SqlFiddle on the notion that TableA_Counts is the table with more than one entry for 201705. Is that correct?
CREATE TABLE TableA (REPT_TIME_FRAME int)
INSERT INTO TableA(REPT_TIME_FRAME)VALUES
(201705),
(201708),
(201801),
(201706),
(201710),
(201803 )
CREATE TABLE TableA_Counts (REPT_TIME_FRAME int)
INSERT INTO TableA_Counts(REPT_TIME_FRAME)VALUES
(201705),
(201708),
(201705),
(201801),
(201706),
(201710),
(201803)
SELECT
d.REPT_TIME_FRAME,count(*) as TOTAL_RECORDS,SOURCE_TOTAL_RECORDS,
CASE WHEN SOURCE_TOTAL_RECORDS > 1 then 'more than' else 'just one' end as NUMBER_OF_REPLICATIONS
FROM
TableA d
LEFT JOIN
(SELECT REPT_TIME_FRAME ,count(*) as SOURCE_TOTAL_RECORDS from TableA_Counts group by REPT_TIME_FRAME) c ON (d.REPT_TIME_FRAME = c.REPT_TIME_FRAME)
group by d.REPT_TIME_FRAME,SOURCE_TOTAL_RECORDS
SELECT d.REPT_TIME_FRAME,
c.SOURCE_TOTAL_RECORDS,
COUNT(*) TOTAL_RECORDS,
CASE
WHEN COUNT(*) > c.SOURCE_TOTAL_RECORDS THEN c.SOURCE_TOTAL_RECORDS
ELSE COUNT(*)
END FINAL_TOTAL_RECORDS,
a.count AS NUM_OF_REPLICATIONS
FROM TableA d
INNER JOIN TableACounts c ON (d.REPT_TIME_FRAME = c.REPT_TIME_FRAME)
INNER JOIN (SELECT REPT_TIME_FRAME ,count(REPT_TIME_FRAME) as count
FROM TableAcounts
WHERE REPT_TIME_FRAME IN (201705, 201708, 201801, 201706, 201710, 201803)
GROUP BY REPT_TIME_FRAME) AS a ON (a.REPT_TIME_FRAME = d.REPT_TIME_FRAME)
WHERE d.REPT_TIME_FRAME IN (201705, 201708, 201801, 201706, 201710, 201803)
GROUP BY d.REPT_TIME_FRAME, c.SOURCE_TOTAL_RECORDS, a.count
I know this question might sound like a duplicate, but I've been through every question I could find; though it's still possible it might be a duplicate of a question I might have missed.
I have what at surface value appears to be a trivial requirement but no matter how I script it out there's always some caveat that's just not working. I've tried GROUP, DISTINCT, JOIN, aggregate functions, etc.
Scenario:
PRIMARYTABLE contains a set of campaigns and SECONDARYTABLE contains the dates on which campaigns were run. There can be multiple runs per campaign and I've included a SUBKEY for each run.
Requirement:
I need to be able to get the most recently run campaigns into a list so the user can more easily select from the campaigns that get run the most frequent.
PRIMARYTABLE
KEYCOLUMN INFOCOLUMN
100000 Test 1
100001 Test Campaign
100002 Test Image 2
100003 Test Img
100004 Image Test
100005 Test
100006 Test Image 3
100007 Test Image 4
100008 Test Image 5
100009 Image Comparison Test 2
100010 Testing
100011 Test Fields
100012 Test 5
100013 test
SECONDARYTABLE
KEYCOLUMN SUBKEY DATECOLUMN
100000 100000 2017-06-02 04:09:57.593
100001 100001 2017-06-19 12:09:54.093
100001 100002 2017-06-27 10:51:14.140
100004 100003 2017-06-27 12:33:47.747
100006 100004 2017-06-28 10:29:53.387
100007 100005 2017-06-28 10:36:23.710
100008 100006 2017-06-29 22:31:03.790
100009 100007 2017-06-29 23:07:52.870
100009 100010 2017-10-04 16:05:40.583
100009 100011 2017-10-04 16:09:55.470
100011 100008 2017-09-08 14:02:28.017
100012 100009 2017-09-11 16:17:23.870
100013 100012 2017-11-07 16:55:55.403
100013 100013 2017-11-08 15:37:16.430
Below is somewhat of an idea of more or less what I'm after.
SELECT DISTINCT( a.[INFOCOLUMN] )
FROM [PRIMARYTABLE] a
INNER JOIN [SECONDARYTABLE] b ON ( a.[KEYCOLUMN] = b.[KEYCOLUMN] )
ORDER BY a.[DATECOLUMN]
Here's hoping for a Homer Simpson "Doh!" moment once I see how it's supposed to be done.
Much appreciated.
the most recently run campaigns >> use row_number() over(.. order by ... DESC)
that get run the most frequent >> use count(*) over(partition by ..)
Using window functions row_number() over() and count() over() enables selection by row of data that is "most recent" and ordering by "most frequent". Note that the DESCending order of dates brings about "recent" = 1.
select
p.*, s.*
from PRIMARYTABLE p
inner join (
select KEYCOLUMN, SUBKEY, DATECOLUMN
, row_number() over(partition by KEYCOLUMN order by DATECOLUMN DESC) recent
, count(*) over(partition by KEYCOLUMN) frequency
from SECONDARYTABLE
) s on p.KEYCOLUMN = s.KEYCOLUMN and s.recent = 1
order by s.frequency DESC, p.INFOCOLUMN
You can try this:
DECLARE #PRIMARYTABLE TABLE
(
[KEYCOLUMN] INT
,[INFOCOLUMN] VARCHAR(24)
);
DECLARE #SECONDARYTABLE TABLE
(
[KEYCOLUMN] INT
,[SUBKEY] INT
,[DATECOLUMN] DATETIME2
);
INSERT INTO #PRIMARYTABLE ([KEYCOLUMN], [INFOCOLUMN])
VALUES (100000, 'Test 1')
,(100001, 'Test Campaign')
,(100002, 'Test Image 2')
,(100003, 'Test Img')
,(100004, 'Image Test')
,(100005, 'Test')
,(100006, 'Test Image 3')
,(100007, 'Test Image 4')
,(100008, 'Test Image 5')
,(100009, 'Image Comparison Test 2')
,(100010, 'Testing')
,(100011, 'Test Fields')
,(100012, 'Test 5')
,(100013, 'test');
INSERT INTO #SECONDARYTABLE ([KEYCOLUMN], [SUBKEY], [DATECOLUMN])
VALUES (100000, 100000, '2017-06-02 04:09:57.593')
,(100001, 100001, '2017-06-19 12:09:54.093')
,(100001, 100002, '2017-06-27 10:51:14.140')
,(100004, 100003, '2017-06-27 12:33:47.747')
,(100006, 100004, '2017-06-28 10:29:53.387')
,(100007, 100005, '2017-06-28 10:36:23.710')
,(100008, 100006, '2017-06-29 22:31:03.790')
,(100009, 100007, '2017-06-29 23:07:52.870')
,(100009, 100010, '2017-10-04 16:05:40.583')
,(100009, 100011, '2017-10-04 16:09:55.470')
,(100011, 100008, '2017-09-08 14:02:28.017')
,(100012, 100009, '2017-09-11 16:17:23.870')
,(100013, 100012, '2017-11-07 16:55:55.403')
,(100013, 100013, '2017-11-08 15:37:16.430');
SELECT a.[INFOCOLUMN]
,b.[DATECOLUMN]
FROM #PRIMARYTABLE A
CROSS APPLY
(
SELECT TOP 1 [DATECOLUMN]
FROM #SECONDARYTABLE B
WHERE A.[KEYCOLUMN] = B.[KEYCOLUMN]
ORDER BY [DATECOLUMN] DESC
) b;
It will give you the last execution of each campaign. You can filter then by date or ORDER BY and get TOP N from the final query.
Or you can use ROW_NUMBER:
WITH DataSource AS
(
SELECT A.[INFOCOLUMN]
,B.[DATECOLUMN]
,ROW_NUMBER() OVER (PARTITION BY A.[KEYCOLUMN] ORDER BY B.[KEYCOLUMN]) AS [RowID]
FROM #PRIMARYTABLE A
INNER JOIN #SECONDARYTABLE B
ON A.[KEYCOLUMN] = B.[KEYCOLUMN]
)
SELECT [INFOCOLUMN]
,[DATECOLUMN]
FROM DataSource
WHERE [RowID] = 1;
try this, it will return the list of campaigns in most frequent order of use. Note campaigns never run wont appear in your list. in this case you will to do a left join
SELECT a.[INFOCOLUMN]
FROM [PRIMARYTABLE] a
/* left */ JOIN [SECONDARYTABLE] b ON a.[KEYCOLUMN] = b.[KEYCOLUMN]
group BY a.[infocolumn]
order by max(datecolumn) desc
here is a stub i did to test it
select 10000 id,'Campain A' cname into #a1 union all
select 10002,'Campain B' union all
select 10004,'Campain C' union all
select 10009,'Campain E'
select 10000 id,'20170101' thedate into #a2 union all
select 10000,'20170102' union all
select 10009,'20170103' union all
select 10002,'20170104' union all
select 10004,'20170105' union all
select 10000,'20170201' union all
select 10000,'20170302' union all
select 10009,'20170403' union all
select 10002,'20170104' union all
select 10004,'20170205' union all
select 10000,'20170101' union all
select 10004,'20170302' union all
select 10000,'20170103' union all
select 10002,'20170404' union all
select 10002,'20170105'
select #a1.cname
from #a1 join #a2 on #a1.id = #a2.id
group by #a1.cname
order by max(thedate) desc
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
Im trying to work with recursive CTE, but my query moves into an infinite loop. This is my code. What is the problem here?
With Family As
(
Select s.EmpID, S.Name, s.RepID , 0 as Depth
From TestingRec s
Where s.EmpID=s.RepID
Union All
Select s2.EmpID,S2.Name, s2.RepID , Depth + 1
From TestingRec s2
Inner Join Family
On s2.RepID = Family.EmpID
)
Select *
From Family
From the anchor part of the CTE:
Select s.EmpID, S.Name, s.RepID , 0 as Depth
From TestingRec s
Where s.EmpID=s.RepID
We can see that there are rows in TestingRec for which EmpID and RepID are equal. In the recursive part, we select rows which match an EmpID that we've already found:
Select s2.EmpID,S2.Name, s2.RepID , Depth + 1
From TestingRec s2
Inner Join Family
On s2.RepID = Family.EmpID
However, there's nothing here to prevent us re-matching those rows that the anchor part found and adding them into the result set again, just with a new depth assigned.
The fix may be as simple as a WHERE clause in the recursive part which has s2.RepID <> s2.EmpID
In your first query you get all the records where EmpId=RepId. When means for the initial records both empid and repid are equal.
Then in your anchor query(union all) you are refering empid=family.repid, which means here also you get the first query results. So i think you need to exclude the first level records in the second query. The following query might work
With Family As
(
Select s.EmpID, S.Name, s.RepID , 0 as Depth
From TestingRec s
Where s.EmpID=s.RepID
Union All
Select s2.EmpID,S2.Name, s2.RepID , Depth + 1
From TestingRec s2
Inner Join Family
On s2.RepID = Family.EmpID
WHERE s2.RepID <> s2.EmpID
)
Select *
From Family
Try this:
With Family As
(
Select s.EmpID, S.Name, s.RepID , 0 as Depth
From TestingRec s
Where s.EmpID=s.RepID
Union All
Select s2.EmpID,S2.Name, s2.RepID , Depth + 1
From TestingRec s2
Inner Join Family
On s2.RepID = Family.EmpID
And s2.repid <> s2.empid --This excludes the results from the first half
)
Select *
From Family
Good Morning All
My boss helped me design a query where it populates 1.37 million lines of random data, he has now asked me to insert/update the results into a blank table. But for some reason I cannot get it to work.
The three columns are ArrivalDate, PitchType_Skey and Site_Skey. But when I run my query (See below) I get an error message and I don't know why. Can you help?
Msg 121, Level 15, State 1, Line 2
The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
Query:
USE Occupancy
INSERT INTO Bookings (ArrivalDate, Site_Skey, PitchType_Skey)
SELECT
Time.Date, Site.Site_Skey, Site.SiteWeighting, PitchType.PitchType_Skey,
PitchType.PitchTypeWeighting,
RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting AS Expr1
FROM
Capacity
INNER JOIN
Site ON Capacity.Site_Skey = Site.Site_Skey
INNER JOIN
PitchType ON Capacity.PitchType_Skey = PitchType.PitchType_Skey
INNER JOIN
Time
INNER JOIN
AGKey ON Time.ArrivalDayWeighting = AGKey.[Key] ON Capacity.StartDate <= Time.Date AND Capacity.EndDate >= Time.Date
CROSS JOIN
(SELECT 0 AS col1
UNION ALL
SELECT 1 AS col1) AS aaav
WHERE
(Time.CalendarYear = 2010)
AND (RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting >= 1.22)
Thanks
Wayne
The error message give you the answer. You have more items in your SELECT list (6)
Time.Date
Site.Site_Skey
Site.SiteWeighting
PitchType.PitchType_Skey
PitchType.PitchTypeWeighting
RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting AS Expr1
Than you do in your INSERT list (3)
ArrivalDate
Site_Skey
PitchType_Skey
Either remove some columns from your SELECT list or add some to your INSERT list.
As you haven't given the complete structure of your Bookings table I can only guess that you will need to do this
USE Occupancy
INSERT INTO Bookings
(
ArrivalDate,
Site_Skey,
PitchType_Skey
)
SELECT
Time.Date,
Site.Site_Skey,
PitchType.PitchType_Skey
FROM
Capacity
INNER JOIN Site ON Capacity.Site_Skey = Site.Site_Skey
INNER JOIN PitchType ON Capacity.PitchType_Skey = PitchType.PitchType_Skey
INNER JOIN Time
INNER JOIN AGKey ON Time.ArrivalDayWeighting = AGKey.[Key] ON Capacity.StartDate <= Time.Date AND Capacity.EndDate >= Time.Date
CROSS JOIN
(
SELECT 0 AS col1
UNION ALL
SELECT 1 AS col1
) AS aaav
WHERE
Time.CalendarYear = 2010
AND (RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting >= 1.22)
I have found the solution and I cant believe how easy it was, I just un-ticked the boxes I didn't want on the Query Designer.