josephdaviskcrm
asked on
SQL - Top six per unit
SELECT TOP (6) StoreID, Location, OERDate AS Date, Rating, Score
FROM dbo.*********
WHERE (StoreID IN ('000001', '000005')) AND (OERDate BETWEEN '1/1/2003' AND '1/1/2008')
-------------------------- ---------- ---------- -----
The above SQL gives the top six for the whole set. I want to get the top six for each StoreID, so there would potentially be 12 records returned.
FROM dbo.*********
WHERE (StoreID IN ('000001', '000005')) AND (OERDate BETWEEN '1/1/2003' AND '1/1/2008')
--------------------------
The above SQL gives the top six for the whole set. I want to get the top six for each StoreID, so there would potentially be 12 records returned.
try this:
select * from
(
SELECT StoreID, Location, OERDate AS Date, Rating, Score, ranking = dense_rank() over(partition by storeid order by rating asc)
FROM dbo.tablename
WHERE (StoreID IN ('000001', '000005')) AND (OERDate BETWEEN '1/1/2003' AND '1/1/2008')
) a
where ranking <= 6
select * from
(
SELECT StoreID, Location, OERDate AS Date, Rating, Score, ranking = dense_rank() over(partition by storeid order by rating asc)
FROM dbo.tablename
WHERE (StoreID IN ('000001', '000005')) AND (OERDate BETWEEN '1/1/2003' AND '1/1/2008')
) a
where ranking <= 6
ASKER
angelIII:
the database object is structure so there really isn't a primary key defined I don't think. I guess if there had to be a primary key it would be storeID and Date together. Also, sorry I forgot to add the order by clause, thats important. I meant to add ORDER BY date DESC. So that it selects the 6 items with the most recent dates from each unit.
chapmandew:
The dense_rank() over(partition by storeid order by rating asc) statement threw an error. SQL Server said that it didn't support the OVER statement. Also, can you explain what that code is supposed to do? I'm not familiar with those commands.
the database object is structure so there really isn't a primary key defined I don't think. I guess if there had to be a primary key it would be storeID and Date together. Also, sorry I forgot to add the order by clause, thats important. I meant to add ORDER BY date DESC. So that it selects the 6 items with the most recent dates from each unit.
chapmandew:
The dense_rank() over(partition by storeid order by rating asc) statement threw an error. SQL Server said that it didn't support the OVER statement. Also, can you explain what that code is supposed to do? I'm not familiar with those commands.
Are you sure that you're using 2005 and that your db compatability level is set to 90?
What it does is for each storeid that it partitions by (groups by) it orders them sequentially based on the order of the rating. It's new functionality in SQL 2005.
What it does is for each storeid that it partitions by (groups by) it orders them sequentially based on the order of the rating. It's new functionality in SQL 2005.
ASKER
Yeah I'm sure its 2005. I don't know about the compatability level. I'm working in a large organization on their database, so I'm sure that it should be all set up right.
Is there an easier way to do it thats not trying to use this new functionality?
Is there an easier way to do it thats not trying to use this new functionality?
Well, I just tried it on a 2005 instance with a compat. level of 80 and it worked fine.
What do you get when you run this?
SELECT SERVERPROPERTY('ProductVer sion')
What do you get when you run this?
SELECT SERVERPROPERTY('ProductVer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
chapmandew: 9.00.3054.00
angelIII: That code is throwing me an error too. It says that it can't be parsed.
I'm just going to go through and grab the data one store at a time. Thanks guys.
angelIII: That code is throwing me an error too. It says that it can't be parsed.
I'm just going to go through and grab the data one store at a time. Thanks guys.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>angelIII: That code is throwing me an error too. It says that it can't be parsed.
question: are you using the query designer?
answer: don't. it's too limited
question: are you using the query designer?
answer: don't. it's too limited
ASKER
That second version worked perfectly. Thanks to both of you.
also, what is the "order by" for the TOP ?