Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 hit Counter

Author  Topic 

lane0618
Posting Yak Master

134 Posts

Posted - 2003-07-10 : 17:18:45
How can I take to take below to end up with the top 10 hits for the current month?

pageID time
130 2003-07-10 11:57:44.107
130 2003-07-10 11:57:44.543
73 2003-07-10 11:57:51.187
91 2003-07-10 11:58:40.590
120 2003-07-10 11:58:45.170
130 2003-07-10 11:58:57.293
114 2003-07-10 12:06:34.873

so I would end up with something like:
TOP 10 link for July
130 - 3 hits
73 - 1 hits
91 - 1 hits
...

Thanks in Advance!
Lane


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-07-10 : 17:22:12
Someone will probably come up with a better way to do the WHERE clause, but mine does work.

SELECT TOP 10 pageID, COUNT(pageID)
FROM Table1
WHERE DATEPART(month, time) = MONTH(GETDATE()) AND DATEPART(year, time) = YEAR(GETDATE())
GROUP BY pageID

Tara

Edited by - tduggan on 07/10/2003 17:40:44
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-10 : 17:31:31
Since GROUP BY in SQL Server Doesn't perform a sort (kidding right?) then won't the results, with out a sort be unpredictable?

In DB2 it's not a prob because

GROUP BY, UNION, DISTINCT, ORDER BY all cause the data to be sorted.

No?



Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-07-10 : 17:35:39
How about this:


SET NOCOUNT ON

CREATE TABLE Table1
(
Column1 INT NOT NULL,
Column2 DATETIME NOT NULL
)

INSERT INTO Table1 VALUES(91, '2003-07-01')
INSERT INTO Table1 VALUES(132, '2003-04-01')
INSERT INTO Table1 VALUES(91, '2002-07-01')
INSERT INTO Table1 VALUES(132, '2001-04-09')
INSERT INTO Table1 VALUES(132, '2003-07-09')
INSERT INTO Table1 VALUES(2, '2003-07-23')
INSERT INTO Table1 VALUES(2, '2003-07-31')
INSERT INTO Table1 VALUES(132, '2003-07-21')

SELECT TOP 10 Column1, COUNT(Column1) AS Hits
FROM Table1
WHERE DATEPART(month, Column2) = MONTH(GETDATE()) AND DATEPART(year, Column2) = YEAR(GETDATE())
GROUP BY Column1
ORDER BY COUNT(Column1) DESC

DROP TABLE Table1

--------Results--------

Column1 Hits
----------- -----------
2 2
132 2
91 1



Tara

Edited by - tduggan on 07/10/2003 17:39:17
Go to Top of Page
   

- Advertisement -