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 |
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2011-07-28 : 11:03:48
|
| Hi,I have a table which has client name, date, price in a database table. There will be multiple records for a client with different prices for the same date.I have 100 records in this table from 07/21/2011 till 07/27/2011.Now i want to retrieve the minimum price for all clients for each date.Example:Client AClient BClient C07212011 - client A - 2507212011 - client A - 1207212011 - client A - 5007212011 - client B - 2307212011 - client B - 1307212011 - client C - 2407212011 - client C - 4207222011 - client A - 4107222011 - client A - 2407222011 - client B - 5207222011 - client B - 1007272011 - client B - 9307272011 - client B - 2707272011 - client C - 3607272011 - client C - 25Result should be :07212011 - client A - 1207212011 - client B - 1307212011 - client C - 2407222011 - client A - 2407222011 - client B - 5207222011 - client B - 1007272011 - client B - 2707272011 - client C - 25How can i get this in a query?Thanks |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-07-28 : 12:17:51
|
| At the risk of doing somebody's homework ...SELECT Date,ClientName,MIN(Price) as MinimumPriceFROM yourTableGROUP BY Date,ClientNameEveryday I learn something that somebody else already knew |
 |
|
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2011-07-28 : 14:02:51
|
| Thanks Jimf.Is there any other way without using group by. This works but, What i gave you was only a sample there are several other fields(12) involved.When i include other fields, the result is getting messed up.I can handle it on .net side but wanted to check if it can be done on database. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-07-28 : 14:10:51
|
| Something like this maybe?JimSELECT DISTINCT Date,ClientName,<other Columns> , MIN(Price) OVER(Partition By Date,ClientName)FROM yourTableEveryday I learn something that somebody else already knew |
 |
|
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2011-07-30 : 01:29:18
|
| Thanks jimf that worked |
 |
|
|
|
|
|
|
|