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 |
|
jaimealvarez
Starting Member
31 Posts |
Posted - 2012-07-25 : 16:47:53
|
| Hi,I have this sqlWITH C1 AS(SELECT c.crclient, c.sort, c.name, CASE WHEN (c.Tkeeper = 'Y' and c.crmaximum = 'Y' and c.crrate IS NOT NULL) then c.MaxRate WHEN (c.Tkeeper = 'Y' and c.crmaximum = 'N' and c.crrate IS NOT NULL) then c.crrate end AS 'BillingRate1' FROM C WHERE c.RecKey = 'Y') SELECT c1.crclient, tkinit AS 'Name', c1.sort, timekeep.tkstrate AS 'standard Rate', CASE WHEN (c1.billingrate1 IS NOT NULL) then c1.billingrate1 else timekeep.tkstrate end AS 'Billing Rate'FROM son_db.dbo.timekeep timekeep LEFT OUTER JOIN C1 ON (C1.name = timekeep.tkinit)C1 is bringing values like thisCrClient Sort Name BillingRate1A 1 Smith 20 A 2 Smith 22A 2 Joseph 30B 3 Smith 20B 3 Joseph 40How can I do it, so that in that the next part just pulls the min(sort) by client and Name, so that it looks like this:CrClient Sort Name BillingRate1A 1 Smith 20 A 2 Joseph 30B 3 Smith 20B 3 Joseph 40Any help would be appreciated. Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-25 : 16:51:38
|
| [code]WITH C1 AS(SELECT c.crclient, c.sort, c.name, CASEWHEN (c.Tkeeper = 'Y' and c.crmaximum = 'Y' and c.crrate IS NOT NULL) then c.MaxRateWHEN (c.Tkeeper = 'Y' and c.crmaximum = 'N' and c.crrate IS NOT NULL) then c.crrateend AS 'BillingRate1'FROM C WHERE c.RecKey = 'Y')SELECT crclient,[Name],sort,[Billing Rate]FROM(SELECT ROW_NUMBER OVER (PARTITION BY c1.crclient,tkinit ORDER BY c1.sort) AS Seq,c1.crclient, tkinit AS 'Name', c1.sort, timekeep.tkstrate AS 'standard Rate', CASEWHEN (c1.billingrate1 IS NOT NULL) then c1.billingrate1else timekeep.tkstrateend AS 'Billing Rate'FROM son_db.dbo.timekeep timekeep LEFT OUTER JOIN C1 ON (C1.name = timekeep.tkinit))tWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|