| Author |
Topic |
|
Freshies4eva
Starting Member
7 Posts |
Posted - 2005-09-30 : 15:48:22
|
| Hi,I'm trying to write a query that will present me with a list of top sales locations and dates like so:Date Loc Total9/28 100 49/27 103 79/26 102 69/25 100 8my table is(item_id int, location int, sell_date)1. I can get a count of sales by day and location doing a simple count and groupselect sell_date, location, cnt = count(*) from salesgroup by sell_date, locationorder by sell_date desc2. I can get the top location and date for a specific date using a set of subqueriesselect q1.sellDate, q1.loc, q2.cnt from(select sellDate = sell_date, location as loc, count(*) as cnt from saleswhere sell_date = '9/25/2005'group by sell_date, location) as q1join(select location as loc, count(*) as cnt from saleswhere sell_date = '9/25/2005'group by location) as q2 on q1.loc = q2.locwhere q1.cnt =(select max(cnt) from (select location as loc, count(*) as cnt from sales where sell_date = '9/25/2005' group by location ) as q3)BUT, I can't figure out how select all dates and end up with a descending list by date of the top sales location.Any ideas? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-09-30 : 15:58:14
|
| Not too sure what you wantselect t.loc, t.sell_date, count(*)from sales tjoin (select top 1 loc, count(*) from sales t2 group by loc order by count(*)) t2on t2.loc = t.locgroup by t.loc, t.sell_dateorder by t.sell_date descThat's descending by date for the top sales location so matches the description but not the queries you have given.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Freshies4eva
Starting Member
7 Posts |
Posted - 2005-10-04 : 12:49:06
|
| Thanks for your response. Didn't think about TOP, that is a good idea. However, this doesn't accomplish one of the goals which is to select the top location by day over a period of days. I played around but had no luck breaking it out by day over a period of days. Given this...location sell_date Unit Sales ----------- ------------------------------------102 2005-10-03 00:00:00 3100 2005-10-03 00:00:00 1101 2005-10-03 00:00:00 1101 2005-10-02 00:00:00 3102 2005-10-02 00:00:00 1100 2005-10-02 00:00:00 1100 2005-10-01 00:00:00 3101 2005-10-01 00:00:00 1102 2005-10-01 00:00:00 1I want to end up with...sell_date location num sales10/3/2005 102 310/2/2005 101 310/1/2005 100 3Any suggestions? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-05 : 01:30:32
|
| Try thisSelect sell_date, location, [unit sales] as [num sales] from yourTable TWhere [unit sales]=(Select Top 1 [unit sales] from yourTable where sell_date=T. sell_date and location=T.location)MadhivananFailing to plan is Planning to fail |
 |
|
|
Freshies4eva
Starting Member
7 Posts |
Posted - 2005-10-05 : 11:03:53
|
That query returns the original (aggregated) results. Did I miss something? Below is a sample of the original listing: the id of the item sold, where it was sold, and on what date. I'd love to go from this to a result showing the top sales location by day.item_id location sell_date1 100 2005-10-01 00:00:002 100 2005-10-01 00:00:003 100 2005-10-01 00:00:004 101 2005-10-01 00:00:005 102 2005-10-01 00:00:006 100 2005-10-02 00:00:007 101 2005-10-02 00:00:008 101 2005-10-02 00:00:009 101 2005-10-02 00:00:0010 102 2005-10-02 00:00:0011 100 2005-10-03 00:00:0012 101 2005-10-03 00:00:0013 102 2005-10-03 00:00:0014 102 2005-10-03 00:00:0015 102 2005-10-03 00:00:00 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-06 : 01:05:29
|
| Is this?Select item_id, location, sell_date from yourTable TWhere sell_date =(Select Top 1 sell_date from yourTable where location=T.location)Otherwise post the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
Freshies4eva
Starting Member
7 Posts |
Posted - 2005-10-11 : 15:20:13
|
Thanks for your suggestion. I'm going to include a sample of the table and the desired output.I'm trying to summarize this table (sales), showing item id, location where it was sold, and date solditem_id location sell_date ----------- ----------- ------------------- 1 100 2005-10-01 00:00:002 100 2005-10-01 00:00:003 100 2005-10-01 00:00:004 101 2005-10-01 00:00:005 102 2005-10-01 00:00:006 100 2005-10-02 00:00:007 101 2005-10-02 00:00:008 101 2005-10-02 00:00:009 101 2005-10-02 00:00:0010 102 2005-10-02 00:00:0011 100 2005-10-03 00:00:0012 101 2005-10-03 00:00:0013 102 2005-10-03 00:00:0014 102 2005-10-03 00:00:0015 102 2005-10-03 00:00:00 into this query view (top sales leader by day)sell_date location num_sales ---------- ----------- ----------- 10/03/05 102 310/02/05 101 310/01/05 100 3 which shows the top sales location, the number of items it sold that day, and the date. I would like to show more than the top location, but trying to keep it simple right now.Thank you for your help! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-12 : 03:25:29
|
Not sure whether this will work for all dataSelect * from ( Select Sell_date, Location,count(Location) as num_sales from yourTable group by Sell_date, Location ) Twhere num_sales=(select Top 1 count(*) from yourTable group by sell_date,Location order by count(*) desc) MadhivananFailing to plan is Planning to fail |
 |
|
|
Freshies4eva
Starting Member
7 Posts |
Posted - 2005-10-13 : 14:36:36
|
We're getting closer! Excuse me, I should say you are getting closer :)This works, but only because the table data I provided was such that on any one of the three days the leading location had 3 total sales. I added one more for location 1 (note the last row - 16) and that breaks the query.Here is the new table...1 100 2005-10-01 00:00:002 100 2005-10-01 00:00:003 100 2005-10-01 00:00:004 101 2005-10-01 00:00:005 102 2005-10-01 00:00:006 100 2005-10-02 00:00:007 101 2005-10-02 00:00:008 101 2005-10-02 00:00:009 101 2005-10-02 00:00:0010 102 2005-10-02 00:00:0011 100 2005-10-03 00:00:0012 101 2005-10-03 00:00:0013 102 2005-10-03 00:00:0014 102 2005-10-03 00:00:0015 102 2005-10-03 00:00:0016 100 2005-10-01 00:00:00 Here is your query (other than formatting I only changed it so it outputs results by date desc).select * from ( select Sell_date, Location, count(Location) as num_sales from sales group by Sell_date, Location ) Twhere num_sales = ( select Top 1 count(*) from sales group by sell_date,Location order by count(*) desc )order by Sell_date desc With the new table, you will see that it only returns one row, for Oct 1, because that is where num_sales matches the derived table. This has been bugging me for a couple weeks, getting close. I really appreciate your help. |
 |
|
|
|