Author |
Topic |
pras2007
Posting Yak Master
216 Posts |
Posted - 2007-09-17 : 18:14:46
|
Hello All,I have table that I want to select a row based on the color field and the latest report_date field. Below are the conditions that I’m looking for. I’ve tried several query but getting the desired solution. Does anyone know how to accomplish this task? Please advice.Thanks.Example: - If you have multiple rows matches with the same report_date value and the same color value then Amount field will be grouped- If you have multiple rows matches where the Report_date values are different with the same Color value then select the latest report_date value and group the Amount value.- If you have a single value for color with a single report_date value then list the record BEFORE ID REPORT_DATE AMOUNT COLOR11 08/29/07 30 BLUE11 08/29/07 30 BLUE11 09/12/07 30 BLUE12 09/10/07 34 GREEN12 09/13/07 30 GREEN12 09/11/07 75 GREEN12 09/13/07 34 GREEN12 09/13/07 45 GREEN13 09/13/07 80 PURPLE13 09/13/07 80 PURPLE14 09/13/07 30 BROWN15 06/12/07 10 WHITE15 09/14/07 20 WHITE15 09/14/07 20 WHITE16 09/17/07 100 RED16 09/17/07 100 RED16 09/17/07 100 RED17 04/20/07 63 YELLOW AFTER ID REPORT_DATE AMOUNT COLOR11 09/12/07 30 BLUE12 09/13/07 109 GREEN 13 09/13/07 80 PURPLE14 09/13/07 30 BROWN15 09/14/07 20 WHITE16 09/17/07 100 RED17 04/20/07 63 YELLOW |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-17 : 18:41:28
|
[code]DECLARE @Sample TABLE (ID INT, REPORT_DATE DATETIME, AMOUNT INT, COLOR SYSNAME)INSERT @SampleSELECT 11, '08/29/07', 30, 'BLUE' UNION ALLSELECT 11, '08/29/07', 30, 'BLUE' UNION ALLSELECT 11, '09/12/07', 30, 'BLUE' UNION ALLSELECT 12, '09/10/07', 34, 'GREEN' UNION ALLSELECT 12, '09/13/07', 30, 'GREEN' UNION ALLSELECT 12, '09/11/07', 75, 'GREEN' UNION ALLSELECT 12, '09/13/07', 34, 'GREEN' UNION ALLSELECT 12, '09/13/07', 45, 'GREEN' UNION ALLSELECT 13, '09/13/07', 80, 'PURPLE' UNION ALLSELECT 13, '09/13/07', 80, 'PURPLE' UNION ALLSELECT 14, '09/13/07', 30, 'BROWN' UNION ALLSELECT 15, '06/12/07', 10, 'WHITE' UNION ALLSELECT 15, '09/14/07', 20, 'WHITE' UNION ALLSELECT 15, '09/14/07', 20, 'WHITE' UNION ALLSELECT 16, '09/17/07', 100, 'RED' UNION ALLSELECT 16, '09/17/07', 100, 'RED' UNION ALLSELECT 16, '09/17/07', 100, 'RED' UNION ALLSELECT 17, '04/20/07', 63, 'YELLOW'SELECT s.ID, s.COLOR, s.REPORT_DATE, SUM(s.AMOUNT) AS AMOUNTFROM ( SELECT ID, MAX(REPORT_DATE) AS RD FROM @Sample GROUP BY ID ) AS dINNER JOIN ( SELECT DISTINCT ID, REPORT_DATE, AMOUNT, COLOR FROM @Sample ) AS s ON s.ID = d.ID AND s.REPORT_DATE = d.RDGROUP BY s.ID, s.COLOR, s.REPORT_DATE[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-17 : 18:42:25
|
SimplerSELECT s.ID, s.COLOR, s.REPORT_DATE, SUM(DISTINCT s.AMOUNT) AS AMOUNTFROM ( SELECT ID, MAX(REPORT_DATE) AS RD FROM @Sample GROUP BY ID ) AS dINNER JOIN @Sample AS s ON s.ID = d.ID AND s.REPORT_DATE = d.RDGROUP BY s.ID, s.COLOR, s.REPORT_DATE E 12°55'05.25"N 56°04'39.16" |
 |
|
pras2007
Posting Yak Master
216 Posts |
Posted - 2007-09-19 : 00:27:14
|
Peso,Thanks for the reply, but when I tried your code I'm getting this error:Server: Msg 409, Level 16, State 2, Line 1The sum-unique aggregate operation cannot take a nvarchar data type as an argument.Do you know what I'm doing wrong? Thanks once again. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-19 : 00:42:00
|
You are trying to use SUM() on a column which is nvarchar, rather than numericKristen |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-19 : 02:15:19
|
Try CASTing the Amount column to MONEY.I have to ask you, why on earth are you storing numeric data in a NVARCHAR column?SELECT s.ID, s.COLOR, s.REPORT_DATE, SUM(DISTINCT CAST(s.AMOUNT AS MONEY)) AS AMOUNTFROM ( SELECT ID, MAX(CAST(REPORT_DATE AS DATETIME)) AS RD FROM @Sample GROUP BY ID ) AS dINNER JOIN @Sample AS s ON s.ID = d.ID AND s.REPORT_DATE = d.RDGROUP BY s.ID, s.COLOR, s.REPORT_DATE E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|