0

I have a table which store records of all dates of a month. I want to retrieve some data from it. The table is so large that I should only selecting a fews of them. If the records have a column "ric_date" which is a date, how can I select records from each of the dates in a month, while selecting only a fews from each date?

The table is so large that the records for 1 date can have 100000 records.

lamwaiman1988
  • 3,729
  • 15
  • 55
  • 87

2 Answers2

1
WITH T AS (
    SELECT ric_date
    FROM yourTable
    WHERE rice_date BETWEEN @start_date AND @end_date -- thanks Aaron Bertrand
    GROUP BY ric_date
)

SELECT CA.*
FROM T
CROSS APPLY (
    SELECT TOP 500 * -- 'a fews'
    FROM yourTable AS YT
    WHERE YT.ric_date = T.ric_date
    ORDER BY someAttribute -- not required, but useful
) AS CA
J Cooper
  • 4,828
  • 3
  • 36
  • 39
  • I don't know that this answers the query. He wants "manys" days (which now I guess means 500) from each date in a particular month (this seems to solve for the whole table), and you've assumed that `ric_date` does not contain time. – Aaron Bertrand Feb 29 '12 at 01:34
  • @gunbuster363 Doesn't this return data for the whole table, and not just for a specific month? – Aaron Bertrand Feb 29 '12 at 01:43
  • @AaronBertrand - you're right, I added the date range filter. Manys Thanks, :) – J Cooper Feb 29 '12 at 01:52
  • Actually I have modified the query and include the date range. Thanks. – lamwaiman1988 Feb 29 '12 at 06:49
0

Rough idea. This will get the first three rows per day for the current month (or as many that exist for any given day - there may be days with no rows represented).

DECLARE 
    @manys INT = 3, 
    @month DATE = DATEADD(DAY, 1-DAY(GETDATE()), DATEDIFF(DAY, 0, GETDATE()));

;WITH x AS 
(
  SELECT some_column, ric_date, rn = ROW_NUMBER() OVER 
    (PARTITION BY ric_date ORDER BY ric_date)
  FROM dbo.data 
  WHERE ric_date >= @month
  AND ric_date < DATEADD(MONTH, 1, @month)
)
SELECT some_column, ric_date FROM x
WHERE rn <= @manys;

If you don't have supporting indexes (most importantly on ric_date), this won't necessarily scale well at the high end.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Hi Aaron, FYI, your query will return rows from the final day of the month before the current month if by chance the query happens to run when GETDATE() is at exactly midnight. You can fix this by changing >= to >. (This query will also return rows from future months, if the table contains any.) – Steve Kass Feb 29 '12 at 02:06
  • @Steve Whoa, hey Steve. I originally had it slightly differently. Doesn't seem like my version was useful to the OP anyway, – Aaron Bertrand Feb 29 '12 at 02:19
  • Say hi to everyone at the Summit for me. You don't have to mention that I caught you making a mistake in a date/time query. :) – Steve Kass Feb 29 '12 at 02:21
  • Then you better delete your comments! :-) – Aaron Bertrand Feb 29 '12 at 02:22
  • @SteveKass Erland says hello. And we all miss you buddy! – Aaron Bertrand Feb 29 '12 at 08:07