Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query design help
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Application:
Desktop
Miscellaneous
Thread ID:
01365555
Message ID:
01365574
Views:
11
>Hi All,
>
>I have the following SQL table:
>
>iId    cEDP       nPVC    nSVC   dUpdatedOn
>1       123        10.12      15.30       12/1/2008
>2       123        12.12        0.00       12/2/2008
>3       555         35.40       0.00       12/2/2008 
>
>
>I would like to group by the cEDP and when duplicate EDP exists use max(dUpdatedOn) to get the nPVC and nSVC values except for when nPVC or nSVC value is 0.00. Based on the above data I would like the following results:
>
>
>cEDP nPVC nSVC
>123 12.12 15.30
>555 35.40 0.00
>
>Thank you,
>daniel

Daniel,

Try this (not-tested)
; with CTE_0 as (select * from myTable where nPVC = 0 or nSVC = 0)

select myTable.* inner join (select MAX(dUpdatedOn) as MaxDate, cEDP from myTable group by cEDP 
where cEDP not in (select cEDP from CTE_O)) MaxDate on myTable.cEDP = MaxDate.cEDP 
and myTable.dUpdateOn = MaxDate.MaxDate 
UNION ALL
select * from CTE_0
Of course, you can do it without CTE using it as a derived table.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform