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.
| Author |
Topic |
|
meikeric
Starting Member
3 Posts |
Posted - 2005-12-08 : 16:24:19
|
Hello all,I am writing a small script in PHP.I have 4 tables1. prices (id, price, station_id, time, user_id)2. areas (id, town)3. stations (id, station_name, address, phone, town, area_id)4. users (id, name)My Query is:SELECT DISTINCT p.station_id, p.id, p.price, p.user_id, s.station_name, s.address, a.town, p.time, u.name FROM prices p, stations s, users u, areas a WHERE p.time > now()-(86*60*60) AND p.station_id=s.id AND p.user_id=u.id AND s.area_id = a.id ORDER BY p.station_id, p.price However, this is giving me duplicates of the station which I do not want.I would like it first to order by the station id, then by the price. Then I would like it to take the top pick from each station_id, so that only the lowest price from each station is picked. Does that make sense?Does anyone have any suggestions as to how to do this? I've been told to use a temporary table. I was hoping to do it without resorting to that. I've not been able to figure out if a Union will do what I need.Any and all suggestions are greatly appreciated.Mike |
|
|
jhermiz
3564 Posts |
Posted - 2005-12-08 : 16:44:29
|
You are getting duplicates because DISTINCT does not work on a column basis, it works for the entire row. What I mean by this is its pulling DISTINCT for each column of that row, so obviously the entire row is not duplicated so it pulls back everyhthing. You need to read about the GROUP BY clause and AGGREGATE functions to correctly get this query. Keeping the web experience alive -- [url]http://www.web-impulse.com[/url] |
 |
|
|
meikeric
Starting Member
3 Posts |
Posted - 2005-12-08 : 17:40:44
|
| [code]SELECT p.station_id, p.id, p.price, p.user_id, s.station_name, s.address, a.town, p.time, u.nameFROM prices p, stations s, users u, areas aWHERE p.time > now( ) - ( 86 *60 *60 )AND p.station_id = s.idAND p.user_id = u.idAND s.area_id = a.idGROUP BY p.station_idORDER BY p.price[/code]is where I'm at now. This is close, but still no cigar.This returns the first rows in the group p.station_id. I need it to order that p.station_id group and then return the lowest price in the field.Any ideas? jhermiz, you do have me scouring the internet. The Aggregate functions I found were SUM, FIRST, LAST, etc. I didn't see any I could use.Thanks!Mike |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-09 : 01:11:36
|
| Post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
meikeric
Starting Member
3 Posts |
Posted - 2005-12-09 : 11:04:04
|
| Table: pricesid | station | user_id | time | price1 | 4 | 62 | 051208152115 | 3152 | 3 | 62 | 051208152115 | 3123 | 2 | 62 | 051208152115 | 3164 | 3 | 62 | 051208152330 | 2095 | 5 | 62 | 051208152400 | 2106 | 3 | 62 | 051208172959 | 3207 | 5 | 62 | 051208172959 | 1108 | 4 | 62 | 051208173019 | 1059 | 5 | 62 | 051208173019 | 356Table: areasid | town1 | Nags Head2 | Kill Devil Hills3 | Manteo4 | Kitty Hawk5 | Currituck6 | Barco7 | CoinjockTable: stationsid | station_name | address | phone | town(erroneous) | town_id1 | 7-Eleven | 7315 S Virginia Dare Trl | 2524414674 | Nags Head | 14 | Chevron | 7309 S Virginia Dare Trl | 2524415169 | Nags Head | 12 | Nags Head Shell | 3643 S Croatan Hwy | 2524415195 | Nags Head | 13 | 7-Eleven | US Highway 64 And 264 | 2524735685 | Manteo | 35 | 7-Eleven | 3868 N Croatan Hwy | 2522614733 | Kitty Hawk | 4Table: usersid | name62 | administratorI need to list/output from lowest to highest the top 5 or 10 gas prices, bearing in mind, I can't have duplicates from each station and I need the most recent price from the station. This is a gas price script, FYI. Ignore the town field in the table stations, as this is being replaced by table: areas. Let me know if this is enough information.Thanks again!Mike |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-12-09 : 11:27:55
|
Are you working in MS Access? Now() isn't a valid SQL Server function.This should work for SQL Server:SELECT TOP 10 p.station_id, p.id, p.price, p.user_id, s.station_name, s.address, a.town, p.time, u.nameFROM prices AS pJOIN ( SELECT tp.station_id, MAX(tp.time) AS MaxTime FROM prices AS tp GROUP BY tp.station_id )AS mpON mp.station_id= p.station_idAND mp.MaxTime= p.timeJOIN stations AS sON p.station_id= s.idJOIN usersAS uON p.user_id= u.idJOIN areasAS aON s.area_id= a.idORDER BY p.price For access, I don't think derived tables work, so I think you'll have to make a query called RecentPriceTime with this code:SELECT tp.station_id, MAX(tp.time) AS MaxTimeFROM prices AS tpGROUP BY tp.station_id and then use this:SELECT TOP 10 p.station_id, p.id, p.price, p.user_id, s.station_name, s.address, a.town, p.time, u.nameFROM prices AS pJOIN RecentPriceTimeAS mpON mp.station_id= p.station_idAND mp.MaxTime= p.timeJOIN stations AS sON p.station_id= s.idJOIN usersAS uON p.user_id= u.idJOIN areasAS aON s.area_id= a.idORDER BY p.price |
 |
|
|
|
|
|
|
|