Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

interesting query Expand / Collapse
Author
Message
Posted Tuesday, March 12, 2013 6:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 5:47 AM
Points: 16, Visits: 91
I wonder if anyone can help with this query which I cant figure out?
I want to limit the amount of rows (or groups) returned, it could be any number but in this case lets say 8 rows, the problem is that it must include all items within 'grp'. The results are to be ordered in the same order as entered into the table. ie dte, 'grp'.

So, my example of returning 8 rows returns everything else correctly BUT would return only half of the '445' group. How can I construct a query that has some kind of look ahead and continues to return rows until all items with the 'grp' are returned (in this case it should actually return 13 rows?

I'm pretty sure CTE is the answer, but cant work out how.

Many thanks in advance, your assistance would be much appreciated.


declare @tbl table (id int identity(1,1), grp int, dte datetime) -- live data contains many additional columns

INSERT @tbl values (1, '20 jan 2011')
INSERT @tbl values (1, '21 jan 2011')
INSERT @tbl values (2, '22 jan 2011')
INSERT @tbl values (2, '22 jan 2011')
INSERT @tbl values (445, '26 jan 2011')
INSERT @tbl values (445, '26 jan 2011')
INSERT @tbl values (445, '26 jan 2011')
INSERT @tbl values (445, '27 jan 2011')
INSERT @tbl values (445, '27 jan 2011')
INSERT @tbl values (445, '28 jan 2011')
INSERT @tbl values (445, '28 jan 2011')
INSERT @tbl values (445, '28 jan 2011')
INSERT @tbl values (445, '29 jan 2011')
INSERT @tbl values (6, '1 feb 2011')
INSERT @tbl values (8, '3 feb 2011')
INSERT @tbl values (1, '30 mar 2011')
INSERT @tbl values (1, '30 mar 2011')
INSERT @tbl values (1, '30 mar 2011')

;
WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER
(PARTITION BY grp
ORDER BY dte ASC, grp)
AS RowNumber
FROM @tbl tbl
)
SELECT * FROM CTE
order by dte, grp



1 1 2011-01-20 00:00:00.000 1
2 1 2011-01-21 00:00:00.000 2
3 2 2011-01-22 00:00:00.000 1
4 2 2011-01-22 00:00:00.000 2
5 445 2011-01-26 00:00:00.000 1
6 445 2011-01-26 00:00:00.000 2
7 445 2011-01-26 00:00:00.000 3
8 445 2011-01-27 00:00:00.000 4
9 445 2011-01-27 00:00:00.000 5
10 445 2011-01-28 00:00:00.000 6
11 445 2011-01-28 00:00:00.000 7
12 445 2011-01-28 00:00:00.000 8
13 445 2011-01-29 00:00:00.000 9
14 6 2011-02-01 00:00:00.000 1
15 8 2011-02-03 00:00:00.000 1
16 1 2011-03-30 00:00:00.000 3
17 1 2011-03-30 00:00:00.000 4
18 1 2011-03-30 00:00:00.000 5
Post #1430165
Posted Tuesday, March 12, 2013 8:14 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 4:39 PM
Points: 4,576, Visits: 8,342
What about grp 1?

Do you want to return entries for grp 1 in records 16, 17 and 18 as well as 1 and 2?

If not - can you specify formal criteria why rows 16, 17, 18 should not be included?


Post #1430182
Posted Wednesday, March 13, 2013 5:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 5:47 AM
Points: 16, Visits: 91
Thankyou for you reply.

grps can be updated many times on many dates, so can appear in the table many times.

The table is ordered by date, so I am only interested in the first 'n' rows plus however many rows needed to get all items for the grp.
In this case, its 8 + 5 rows.

So the answer to your question is I am only interested in rows 1-13 and not any subsequent rows
Post #1430314
Posted Thursday, March 14, 2013 8:22 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:36 AM
Points: 4,985, Visits: 11,681
AllanP999 (3/13/2013)
Thankyou for you reply.

grps can be updated many times on many dates, so can appear in the table many times.

The table is ordered by date, so I am only interested in the first 'n' rows plus however many rows needed to get all items for the grp.
In this case, its 8 + 5 rows.

So the answer to your question is I am only interested in rows 1-13 and not any subsequent rows


Here's one solution:

with Groups
as (
select top 8 grp
from @tbl
order by dte
)
,GroupsDistinct
as (
select distinct grp
from Groups
)
select t.*
from @tbl t
join GroupsDistinct gd on t.grp = gd.grp

This does, however, bring back ids 16, 17 and 18 because they are in grp 1 (which is selected by the Id 1 row). If these rows should not be selected, you need to modify your description of the problem, as already pointed out by Sergiy.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1431004
Posted Thursday, March 14, 2013 8:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550
Here's another, same caveats apply

WITH CTE AS 
(
SELECT id,grp,dte,
ROW_NUMBER() OVER(ORDER BY dte ASC, grp) AS rn
FROM @tbl tbl
),
CTE2 AS (
SELECT id,grp,dte,rn,
MIN(rn) OVER(PARTITION BY grp) AS MinRn
FROM CTE)
SELECT id,grp,dte
--,rn,MinRn
FROM CTE2
WHERE MinRn <= 8
ORDER BY rn;



____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1431022
Posted Thursday, March 14, 2013 4:46 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 5:47 AM
Points: 16, Visits: 91
Great I now have the solution. Many thanks to you all.
Post #1431291
Posted Friday, March 15, 2013 8:08 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:05 PM
Points: 13,137, Visits: 11,975
AllanP999 (3/14/2013)
Great I now have the solution. Many thanks to you all.


Which one did you use or was it a combination? It would be great if you would share what you used as a solution to help other that may stumble into this thread in the future.


_______________________________________________________________

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)
Post #1431551
Posted Friday, March 15, 2013 12:49 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 5:47 AM
Points: 16, Visits: 91
Phil's solution sorted it out thanks
Post #1431686
Posted Sunday, March 17, 2013 8:01 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 4:39 PM
Points: 4,576, Visits: 8,342
AllanP999 (3/15/2013)
Phil's solution sorted it out thanks


So, your answer on my question was not correct:
So the answer to your question is I am only interested in rows 1-13 and not any subsequent rows


as Phil's solution returns rows 16 and 17.
Post #1432068
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse