patternsqlMinor
Query to get queue position for each group
Viewed 0 times
eachgroupquerypositiongetforqueue
Problem
I have a queue table with the following sample data:
I want to query for the queue position of each company group to show where in line each company is (imagine acme has 1,000 rows before 500 Genco starts and then acme has 5,000 more after the Genco records process). Result I would want would look like the following:
I played around with ranking and grouping but the fact that a company group can be multiple times anywhere in the queue kept messing with my aggregates. I also tried a
id company location
1 acme new york
2 acme philadelphia
3 genco st.louis
4 genco san diego
5 genco san francisco
6 acme miamiI want to query for the queue position of each company group to show where in line each company is (imagine acme has 1,000 rows before 500 Genco starts and then acme has 5,000 more after the Genco records process). Result I would want would look like the following:
queuePositionId company
1 acme
3 genco
6 acmeI played around with ranking and grouping but the fact that a company group can be multiple times anywhere in the queue kept messing with my aggregates. I also tried a
dense_rank but couldn't figure out the ordering. Any ideas?Solution
This is a Gaps and Islands question. See here for more details on problems like this.
This should do what you need:
UPDATE
Andriy noted that my solution was a SQL Servre 2012+ solution. The following code should work for versions down to 2005.
This should do what you need:
-- Generate test data
DECLARE @Companies TABLE
(
ID INT
,Company NVARCHAR(100)
,Location NVARCHAR(100)
);
INSERT @Companies
SELECT *
FROM (VALUES (1, 'acme', 'new york')
,(2, 'acme', 'philadelphia')
,(3, 'genco', 'st.louis')
,(4, 'genco', 'san diego')
,(5, 'genco', 'san francisco')
,(6, 'acme', 'miami')
) AS CompanyLocations(ID, Company, Location);
-- Find company positions
;WITH cte_Companies
AS
(
SELECT ID
,Company
,CASE
WHEN LAG(Company) OVER(ORDER BY ID) = Company
THEN 1
ELSE 0
END AS CompanyPosition
FROM @Companies
)
SELECT ID, Company
FROM cte_Companies
WHERE CompanyPosition = 0UPDATE
Andriy noted that my solution was a SQL Servre 2012+ solution. The following code should work for versions down to 2005.
-- Generate test data
DECLARE @Companies TABLE
(
ID INT
,Company NVARCHAR(100)
,Location NVARCHAR(100)
);
INSERT @Companies
SELECT *
FROM (VALUES (1, 'acme', 'new york')
,(2, 'acme', 'philadelphia')
,(3, 'genco', 'st.louis')
,(4, 'genco', 'san diego')
,(5, 'genco', 'san francisco')
,(6, 'acme', 'miami')
-- Further test data
,(7, 'genco', 'London')
,(8, 'genco', 'Portsmouth')
) AS CompanyLocations(ID, Company, Location);
-- Find company positions
SELECT ID, Company
FROM @Companies c1
WHERE NOT EXISTS (
SELECT *
FROM @Companies c2
WHERE c1.Company = c2.Company
AND c1.ID - 1 = c2.ID
)Code Snippets
-- Generate test data
DECLARE @Companies TABLE
(
ID INT
,Company NVARCHAR(100)
,Location NVARCHAR(100)
);
INSERT @Companies
SELECT *
FROM (VALUES (1, 'acme', 'new york')
,(2, 'acme', 'philadelphia')
,(3, 'genco', 'st.louis')
,(4, 'genco', 'san diego')
,(5, 'genco', 'san francisco')
,(6, 'acme', 'miami')
) AS CompanyLocations(ID, Company, Location);
-- Find company positions
;WITH cte_Companies
AS
(
SELECT ID
,Company
,CASE
WHEN LAG(Company) OVER(ORDER BY ID) = Company
THEN 1
ELSE 0
END AS CompanyPosition
FROM @Companies
)
SELECT ID, Company
FROM cte_Companies
WHERE CompanyPosition = 0-- Generate test data
DECLARE @Companies TABLE
(
ID INT
,Company NVARCHAR(100)
,Location NVARCHAR(100)
);
INSERT @Companies
SELECT *
FROM (VALUES (1, 'acme', 'new york')
,(2, 'acme', 'philadelphia')
,(3, 'genco', 'st.louis')
,(4, 'genco', 'san diego')
,(5, 'genco', 'san francisco')
,(6, 'acme', 'miami')
-- Further test data
,(7, 'genco', 'London')
,(8, 'genco', 'Portsmouth')
) AS CompanyLocations(ID, Company, Location);
-- Find company positions
SELECT ID, Company
FROM @Companies c1
WHERE NOT EXISTS (
SELECT *
FROM @Companies c2
WHERE c1.Company = c2.Company
AND c1.ID - 1 = c2.ID
)Context
StackExchange Database Administrators Q#131916, answer score: 3
Revisions (0)
No revisions yet.