Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with writing a query. Kindly guide...


Problem with writing a query. Kindly guide...

Author
Message
T.Ashish
T.Ashish
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 582
Hi All,

I have RATE and SHIFT columns in a table as mentioned below. When I pass RATE=1 then I get SHIFT = 1,2,3. Now in second step, I need to write a query that will find all rates where atleast these three shifts compulsorily exist (e.g. RATE 3,5 as per below data).

All above work is to be done as a part of a single query and first input of RATE will be dynamic. Below mentioned data is only for example, and real table will be having huge data.

Rate   Shift
1   1
1    2
1    3
2   5
3   1
3   2
3   3
8   4
4   1
4   2
5   1
5   2
5   3
5   4
6   1
7   2
8   2

Regards,
Ashish
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16540 Visits: 16993
You have been around here long enough to know that posting some consumable ddl and data is far more likely to get responses.

That being said why is only 3 and 5 returned? What about 1?

Seems to me like all you need is

GROUP BY Col1
HAVING COUNT(*) >= 3



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
hunchback
hunchback
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 639
This problem is known as Relational Division.


SET NOCOUNT ON;
USE tempdb;
GO
DECLARE @T TABLE (
Rate int NOT NULL,
Shift int NOT NULL,
PRIMARY KEY (Rate, Shift)
);

INSERT INTO @T (
   Rate,
   Shift
   Wink
VALUES
   (1, 1),
   (1, 2),
   (1, 3),
   (2, 5),
   (3, 1),
   (3, 2),
   (3, 3),
   (8, 4),
   (4, 1),
   (4, 2),
   (5, 1),
   (5, 2),
   (5, 3),
   (5, 4),
   (6, 1),
   (7, 2),
   (8, 2);


DECLARE @Rate int = 1;

WITH C1 AS (
SELECT
Shift
FROM
   @T
WHERE
   Rate = @Rate
)
SELECT
   A.Rate
FROM
   @T AS A
   INNER JOIN
   C1 AS B
   ON A.Shift = B.Shift
GROUP BY
   A.Rate
HAVING
   COUNT(*) = (SELECT COUNT(*) FROM C1);
GO

/*

Rate
1
3
5

*/





Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16540 Visits: 16993
Thanks to hunchback for the ddl and data. Here is another way to do the same thing.


select Rate
from @T
cross apply (select count(*) as RowsCount from @t where rate = @Rate) r
group by Rate
having COUNT(*) >= max(r.RowsCount)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
Sean Lange (9/26/2013)
Thanks to hunchback for the ddl and data. Here is another way to do the same thing.


select Rate
from @T
cross apply (select count(*) as RowsCount from @t where rate = @Rate) r
group by Rate
having COUNT(*) >= max(r.RowsCount)



Sean - Should this return Rate = 9? I think your approach simply returns any Rate that has 3 or more rows.


DECLARE @T TABLE (
Rate int NOT NULL,
Shift int NOT NULL,
PRIMARY KEY (Rate, Shift)
);

INSERT INTO @T (
   Rate,
   Shift
   Wink
VALUES
   (1, 1),
   (1, 2),
   (1, 3),
   (2, 5),
   (3, 1),
   (3, 2),
   (3, 3),
   (8, 4),
   (4, 1),
   (4, 2),
   (5, 1),
   (5, 2),
   (5, 3),
   (5, 4),
   (6, 1),
   (7, 2),
   (8, 2),
   (9, 2),
   (9, 3),
   (9, 4);


DECLARE @Rate int = 1;

select Rate
from @T
cross apply (select count(*) as RowsCount from @t where rate = @Rate) r
group by Rate
having COUNT(*) >= max(r.RowsCount)




I think this works as the OP requested also:


SELECT Rate
FROM @T
WHERE Shift IN (SELECT Shift FROM @T WHERE Rate = @Rate)
GROUP BY Rate
HAVING COUNT(*) = (SELECT COUNT(*) FROM @T WHERE Rate = @Rate);





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
I am a bit curious how hunchback and my solutions would scale against a divide and conquer approach.


SELECT Shift
INTO #T
FROM @T
WHERE Rate = @Rate;
DECLARE @Rows INT = @@ROWCOUNT;

SELECT Rate
FROM @T
WHERE Shift IN (SELECT Shift FROM #T)
GROUP BY Rate
HAVING COUNT(*) = @Rows;

GO
DROP TABLE #T;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Mr. Kapsicum
Mr. Kapsicum
Right there with Babe
Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)

Group: General Forum Members
Points: 741 Visits: 968
well, almost all the sql masters have answered the questions.
a simple group by and having count will serve the purpose.
T.Ashish
T.Ashish
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 582
Thanks hunchback, Sean Lange and dwain.c for help.

I'm going with CROSS APPLY as it has least logical reads and lesser cost then other two.

Thanks Again.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
If I am reading this article (http://weblogs.sqlteam.com/peterl/archive/2010/07/02/Proper-Relational-Division-With-Sets.aspx) correctly, this solution should also be pretty fast:


WITH Target AS
(
SELECT Rate, Shift
FROM @T
WHERE Rate = @Rate
)
SELECT c.Rate
FROM
(
SELECT Rate, rc=COUNT(*)
FROM @T
GROUP BY Rate
) a
JOIN
(
SELECT rc=COUNT(*)
FROM Target
) b ON b.rc <= a.rc
JOIN @T AS c ON c.Rate = a.Rate
JOIN Target AS d ON d.Shift = c.Shift
GROUP BY c.Rate
HAVING COUNT(*) = MIN(b.rc);





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
T.Ashish (9/26/2013)
Thanks hunchback, Sean Lange and dwain.c for help.

I'm going with CROSS APPLY as it has least logical reads and lesser cost then other two.

Thanks Again.


I do not recommend that you judge a query's performance by its plan cost.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search