| Author |
Topic |
|
sqlhelp14
Yak Posting Veteran
55 Posts |
Posted - 2008-04-08 : 11:32:50
|
| i have one query:SELECT MAX(salesid) AS salesid, max(salestitle) as salestitle FROM saleshistory s where list = 'sales' GROUP BY DATEADD(dd, DATEDIFF(dd, 0, salesdate), 0) order by salestitlei m getting more than 1000 rows.. in outputsome of them are :salesid salestitle---------------------------------1187 1/20/2005 9:45:54 AM82162 2K8_KTBcalendar_pre1 9/19/2007 10:46:02 AM82496 2K8_KTBcalendar_pre1 9/20/2007 3:29:44 PM91857 2K8_KTBcalender_inst 10/16/2007 2:47:31 PM175660 AmerHeartMo_2008 2/22/2008 1:05:24 PM179614 AmerHeartMo_2008 2/26/2008 4:54:18 PM2554 American_Heart_Month_Feb200525335 Cholesterol_Educ_Mo_2006_Rem_test25186 Cholesterol_Educ_Mo_2006_Rem_test25282 Cholesterol_Educ_Mo_2006_Rem_testi want (desired)output like: - 1187 1/20/2005 9:45:54 AM82496 2K8_KTBcalendar_pre1 9/20/2007 3:29:44 PM91857 2K8_KTBcalender_inst 10/16/2007 2:47:31 PM179614 AmerHeartMo_2008 2/26/2008 4:54:18 PM2554 American_Heart_Month_Feb200525335 Cholesterol_Educ_Mo_2006_Rem_testi want max(salesid) among that same salestitlewhen i m doing like:SELECT MAX(salesid) AS salesid, max(salestitle) as salestitle FROM saleshistory s where list = 'sales' GROUP BY substring([salestitle],1,charindex(' ',[salestitle])-1)i m getting an errorMsg 536, Level 16, State 3, Line 1Invalid length parameter passed to the substring function.(0 row(s) affected)can anybody help me to get correct output.thanks. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-08 : 11:36:44
|
[code]SELECT SalesID, SalesTitle, SalesDateFROM ( SELECT SalesID, SalesTitle, SalesDate, ROW_NUMBER() OVER (PARTITION BY SalesTitle ORDER BY SalesDate DESC) AS RecID FROM SalesHistory ) AS dWHERE RecID = 1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-08 : 11:38:58
|
Make sure you always have a space in salestitle field.Else use:-SELECT MAX(salesid) AS salesid, max(salestitle) as salestitleFROM saleshistory s where list = 'sales'GROUP BY case when charindex(' ',[salestitle])>0 then substring([salestitle],1,charindex(' ',[salestitle])-1)else [salestitle]end |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-08 : 11:40:02
|
And as always, what make you think MAX(SalesID) and MAX(SalesTitle) derives from same record? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
sqlhelp14
Yak Posting Veteran
55 Posts |
Posted - 2008-04-08 : 11:41:17
|
| thanks peso & visakh16.now its working perfect.i got results and also knew about row_number() function.thanks both of you. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-08 : 11:43:32
|
Use Visakh's suggestion. You should always try to make things as difficult as possible. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-08 : 11:56:27
|
quote: Originally posted by Peso Use Visakh's suggestion. You should always try to make things as difficult as possible. E 12°55'05.25"N 56°04'39.16"
I was just suggesting a way for preventing the error OP posted. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-08 : 11:57:21
|
quote: Originally posted by Peso And as always, what make you think MAX(SalesID) and MAX(SalesTitle) derives from same record? E 12°55'05.25"N 56°04'39.16"
I think OP was asking for maximum value of salesid and title rather than details from the max record. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-09 : 03:17:08
|
quote: Originally posted by visakh16
quote: Originally posted by Peso And as always, what make you think MAX(SalesID) and MAX(SalesTitle) derives from same record? E 12°55'05.25"N 56°04'39.16"
I think OP was asking for maximum value of salesid and title rather than details from the max record.
The confusion is that OP used the code which wont produce the expected result MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|