Solved

Display only acct records with the most recent date

Posted on 2014-02-15
9
180 Views
Last Modified: 2014-02-22
I have a query that produces all unique ACCT rows for each effective date.

But I am having difficulty modifying the query so that it shows only ONE row for each account that contains the most recent effective date.

Maybe somebody has time to take a shot at it?

SELECT     TOP (999999) dbo.Customers.AcctNo, dbo.CustomersRates.Effective, dbo.Customers.Letter, dbo.Customers.BillingContact, dbo.Customers.Title, 
                      dbo.Customers.ContactPerson, dbo.Customers.BillCompanyName, dbo.Customers.BillStreetAddress, dbo.Customers.BillCity, dbo.Customers.BillState, 
                      dbo.Customers.BillZip, dbo.Customers.CompanyName, dbo.Customers.Active, dbo.Customers.StreetAddress, dbo.Customers.City, dbo.Customers.State, 
                      dbo.Customers.Zip, dbo.Customers.PhoneNo, dbo.Customers.PhoneWaybillAr, dbo.Customers.PhoneWaybillNo, dbo.Customers.PhoneNo2, dbo.Customers.Ext, 
                      dbo.Customers.FaxNo, dbo.Customers.Attention, dbo.Customers.BillingRefName, dbo.Customers.RefMask, dbo.Customers.Rep, dbo.CustomersRates.MinCa, 
                      dbo.CustomersRates.MinCaN, dbo.CustomersRates.MinVa, dbo.CustomersRates.MinVaN, dbo.CustomersRates.Min14, dbo.CustomersRates.Min14N, 
                      dbo.CustomersRates.Min20, dbo.CustomersRates.Min20N, dbo.CustomersRates.BaseCharge, dbo.CustomersRates.BaseChargeN, 
                      dbo.CustomersRates.BaseChargeVa, dbo.CustomersRates.BaseChargeVaN, dbo.CustomersRates.BaseCharge14, dbo.CustomersRates.BaseCharge14N, 
                      dbo.CustomersRates.BaseCharge20, dbo.CustomersRates.BaseCharge20N, dbo.CustomersRates.PerMileRate, dbo.CustomersRates.PerMileRateN, 
                      dbo.CustomersRates.PerMileRateVa, dbo.CustomersRates.PerMileRateVaN, dbo.CustomersRates.PerMileRate14, dbo.CustomersRates.PerMileRate14N, 
                      dbo.CustomersRates.PerMileRate20, dbo.CustomersRates.PerMileRate20N, dbo.CustomersRates.WeightCharge, dbo.CustomersRates.WeightChargeN, 
                      dbo.CustomersRates.WeightChargeVa, dbo.CustomersRates.WeightChargeVaN, dbo.CustomersRates.WeightCharge14, dbo.CustomersRates.WeightCharge14N, 
                      dbo.CustomersRates.WeightCharge20, dbo.CustomersRates.WeightCharge20N, dbo.CustomersRates.WaitingCharge, dbo.CustomersRates.WaitingChargeN, 
                      dbo.CustomersRates.PrivCharge, dbo.CustomersRates.PrivChargeN, dbo.CustomersRates.BadAddressCharge, dbo.CustomersRates.BadAddressChargeN, 
                      dbo.CustomersRates.MultipleR, dbo.CustomersRates.MultipleRN, dbo.CustomersRates.MultipleRVa, dbo.CustomersRates.MultipleRVaN, 
                      dbo.CustomersRates.MultipleR14, dbo.CustomersRates.MultipleR14N, dbo.CustomersRates.MultipleR20, dbo.CustomersRates.MultipleR20N, 
                      dbo.CustomersRates.MultipleD, dbo.CustomersRates.MultipleDN, dbo.CustomersRates.MultipleDVa, dbo.CustomersRates.MultipleDVaN, 
                      dbo.CustomersRates.MultipleD14, dbo.CustomersRates.MultipleD14N, dbo.CustomersRates.MultipleD20, dbo.CustomersRates.MultipleD20N, 
                      dbo.CustomersRates.MultipleS, dbo.CustomersRates.MultipleSN, dbo.CustomersRates.MultipleSVa, dbo.CustomersRates.MultipleSVaN, 
                      dbo.CustomersRates.MultipleS14, dbo.CustomersRates.MultipleS14N, dbo.CustomersRates.MultipleS20, dbo.CustomersRates.MultipleS20N, 
                      dbo.CustomersRates.Floor, dbo.CustomersRates.FloorN, dbo.CustomersRates.FloorVa, dbo.CustomersRates.FloorVaN, dbo.CustomersRates.Floor14, 
                      dbo.CustomersRates.Floor14N, dbo.CustomersRates.Floor20, dbo.CustomersRates.Floor20N, dbo.CustomersRates.InsDelChg1, dbo.CustomersRates.InsDelChg2, 
                      dbo.CustomersRates.Dist, dbo.CustomersRates.DistN, dbo.CustomersRates.DistB, dbo.CustomersRates.DistNB, dbo.CustomersRates.DistC, 
                      dbo.CustomersRates.DistNC, dbo.CustomersRates.DistDrv, dbo.CustomersRates.DistDrvN, dbo.CustomersRates.DistDrvB, dbo.CustomersRates.DistDrvNB, 
                      dbo.CustomersRates.DistDrvC, dbo.CustomersRates.DistDrvNC, dbo.CustomersRates.DistWeight, dbo.CustomersRates.DistWeightDrv, dbo.CustomersRates.ID, 
                      dbo.CustomersRates.PriceLevel, dbo.CustomersRates.MinMi, dbo.CustomersRates.MinMiN, dbo.CustomersRates.BaseChargeMi, 
                      dbo.CustomersRates.BaseChargeMiN, dbo.CustomersRates.PerMileRateMi, dbo.CustomersRates.PerMileRateMiN, dbo.CustomersRates.WeightChargeMi, 
                      dbo.CustomersRates.WeightChargeMiN, dbo.CustomersRates.MultipleRMi, dbo.CustomersRates.MultipleRMiN, dbo.CustomersRates.MultipleDMi, 
                      dbo.CustomersRates.MultipleDMiN, dbo.CustomersRates.MultipleSMi, dbo.CustomersRates.MultipleSMiN, dbo.CustomersRates.FloorMi, 
                      dbo.CustomersRates.FloorMiN, dbo.CustomersRates.DistFreeWeight, dbo.CustomersRates.SkidCharge, dbo.CustomersRates.Dist2A, dbo.CustomersRates.Dist2AN, 
                      dbo.CustomersRates.Dist2B, dbo.CustomersRates.Dist2BN, dbo.CustomersRates.Dist2C, dbo.CustomersRates.Dist2CN, dbo.CustomersRates.Dist2D, 
                      dbo.CustomersRates.Dist2DN, dbo.CustomersRates.Dist2E, dbo.CustomersRates.Dist2EN, dbo.CustomersRates.Dist2DrvA, dbo.CustomersRates.Dist2DrvAN, 
                      dbo.CustomersRates.Dist2DrvB, dbo.CustomersRates.Dist2DrvBN, dbo.CustomersRates.Dist2DrvC, dbo.CustomersRates.Dist2DrvCN, 
                      dbo.CustomersRates.Dist2DrvD, dbo.CustomersRates.Dist2DrvDN, dbo.CustomersRates.Dist2DrvE, dbo.CustomersRates.Dist2DrvEN, 
                      dbo.CustomersRates.Dist2Weight, dbo.CustomersRates.Dist2WeightDrv, dbo.CustomersRates.Dist2FreeWeight, dbo.CustomersRates.FreeWeight, 
                      dbo.CustomersRates.DayStart, dbo.CustomersRates.DayEnd, dbo.CustomersRates.PickHold, dbo.CustomersRates.PickHoldN, dbo.CustomersRates.FreeMiles, 
                      dbo.CustomersRates.chkTollsDisabled, dbo.CustomersRates.chkParkingDisabled, dbo.CustomersRates.chkResDisabled, dbo.CustomersRates.chkForceMileage, 
                      dbo.CustomersRates.chkTollsNC, dbo.CustomersRates.chkParkingNC, dbo.CustomersRates.chkResNC, dbo.CustomersRates.SecondCtr, 
                      dbo.CustomersRates.SecondCtrN, dbo.CustomersRates.SecondAWB, dbo.CustomersRates.SecondAWBN, dbo.CustomersRates.OutboundChargeNEaAdd, 
                      dbo.CustomersRates.OutboundChargeN, dbo.CustomersRates.OutboundChargeEaAdd, dbo.CustomersRates.OutboundCharge, 
                      dbo.CustomersRates.InboundChargeNEaAdd, dbo.CustomersRates.InboundChargeN, dbo.CustomersRates.InboundChargeEaAdd, 
                      dbo.CustomersRates.InboundCharge, dbo.CustomersRates.nfoPerMileRate, dbo.CustomersRates.nfoAfterHours, dbo.CustomersRates.nfoWeightCharge, 
                      dbo.CustomersRates.nfoFreeMiles, dbo.CustomersRates.nfoFreeWeight, dbo.CustomersRates.nfoAdditionalBox
FROM         dbo.Customers INNER JOIN
                      dbo.CustomersRates ON dbo.Customers.AcctNo = dbo.CustomersRates.AcctNo
ORDER BY dbo.Customers.AcctNo, dbo.CustomersRates.Effective DESC

Open in new window

0
Comment
Question by:Dale Massicotte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 225 total points
ID: 39862130
You can try this query.
;WITH CTE 
     AS (SELECT TOP (999999) dbo.Customers.AcctNo, 
                             dbo.CustomersRates.Effective, 
                             dbo.Customers.Letter, 
                             dbo.Customers.BillingContact, 
                             dbo.Customers.Title, 
                             dbo.Customers.ContactPerson, 
                             dbo.Customers.BillCompanyName, 
                             dbo.Customers.BillStreetAddress, 
                             dbo.Customers.BillCity, 
                             dbo.Customers.BillState, 
                             dbo.Customers.BillZip, 
                             dbo.Customers.CompanyName, 
                             dbo.Customers.Active, 
                             dbo.Customers.StreetAddress, 
                             dbo.Customers.City, 
                             dbo.Customers.State, 
                             dbo.Customers.Zip, 
                             dbo.Customers.PhoneNo, 
                             dbo.Customers.PhoneWaybillAr, 
                             dbo.Customers.PhoneWaybillNo, 
                             dbo.Customers.PhoneNo2, 
                             dbo.Customers.Ext, 
                             dbo.Customers.FaxNo, 
                             dbo.Customers.Attention, 
                             dbo.Customers.BillingRefName, 
                             dbo.Customers.RefMask, 
                             dbo.Customers.Rep, 
                             dbo.CustomersRates.MinCa, 
                             dbo.CustomersRates.MinCaN, 
                             dbo.CustomersRates.MinVa, 
                             dbo.CustomersRates.MinVaN, 
                             dbo.CustomersRates.Min14, 
                             dbo.CustomersRates.Min14N, 
                             dbo.CustomersRates.Min20, 
                             dbo.CustomersRates.Min20N, 
                             dbo.CustomersRates.BaseCharge, 
                             dbo.CustomersRates.BaseChargeN, 
                             dbo.CustomersRates.BaseChargeVa, 
                             dbo.CustomersRates.BaseChargeVaN, 
                             dbo.CustomersRates.BaseCharge14, 
                             dbo.CustomersRates.BaseCharge14N, 
                             dbo.CustomersRates.BaseCharge20, 
                             dbo.CustomersRates.BaseCharge20N, 
                             dbo.CustomersRates.PerMileRate, 
                             dbo.CustomersRates.PerMileRateN, 
                             dbo.CustomersRates.PerMileRateVa, 
                             dbo.CustomersRates.PerMileRateVaN, 
                             dbo.CustomersRates.PerMileRate14, 
                             dbo.CustomersRates.PerMileRate14N, 
                             dbo.CustomersRates.PerMileRate20, 
                             dbo.CustomersRates.PerMileRate20N, 
                             dbo.CustomersRates.WeightCharge, 
                             dbo.CustomersRates.WeightChargeN, 
                             dbo.CustomersRates.WeightChargeVa, 
                             dbo.CustomersRates.WeightChargeVaN, 
                             dbo.CustomersRates.WeightCharge14, 
                             dbo.CustomersRates.WeightCharge14N, 
                             dbo.CustomersRates.WeightCharge20, 
                             dbo.CustomersRates.WeightCharge20N, 
                             dbo.CustomersRates.WaitingCharge, 
                             dbo.CustomersRates.WaitingChargeN, 
                             dbo.CustomersRates.PrivCharge, 
                             dbo.CustomersRates.PrivChargeN, 
                             dbo.CustomersRates.BadAddressCharge, 
                             dbo.CustomersRates.BadAddressChargeN, 
                             dbo.CustomersRates.MultipleR, 
                             dbo.CustomersRates.MultipleRN, 
                             dbo.CustomersRates.MultipleRVa, 
                             dbo.CustomersRates.MultipleRVaN, 
                             dbo.CustomersRates.MultipleR14, 
                             dbo.CustomersRates.MultipleR14N, 
                             dbo.CustomersRates.MultipleR20, 
                             dbo.CustomersRates.MultipleR20N, 
                             dbo.CustomersRates.MultipleD, 
                             dbo.CustomersRates.MultipleDN, 
                             dbo.CustomersRates.MultipleDVa, 
                             dbo.CustomersRates.MultipleDVaN, 
                             dbo.CustomersRates.MultipleD14, 
                             dbo.CustomersRates.MultipleD14N, 
                             dbo.CustomersRates.MultipleD20, 
                             dbo.CustomersRates.MultipleD20N, 
                             dbo.CustomersRates.MultipleS, 
                             dbo.CustomersRates.MultipleSN, 
                             dbo.CustomersRates.MultipleSVa, 
                             dbo.CustomersRates.MultipleSVaN, 
                             dbo.CustomersRates.MultipleS14, 
                             dbo.CustomersRates.MultipleS14N, 
                             dbo.CustomersRates.MultipleS20, 
                             dbo.CustomersRates.MultipleS20N, 
                             dbo.CustomersRates.Floor, 
                             dbo.CustomersRates.FloorN, 
                             dbo.CustomersRates.FloorVa, 
                             dbo.CustomersRates.FloorVaN, 
                             dbo.CustomersRates.Floor14, 
                             dbo.CustomersRates.Floor14N, 
                             dbo.CustomersRates.Floor20, 
                             dbo.CustomersRates.Floor20N, 
                             dbo.CustomersRates.InsDelChg1, 
                             dbo.CustomersRates.InsDelChg2, 
                             dbo.CustomersRates.Dist, 
                             dbo.CustomersRates.DistN, 
                             dbo.CustomersRates.DistB, 
                             dbo.CustomersRates.DistNB, 
                             dbo.CustomersRates.DistC, 
                             dbo.CustomersRates.DistNC, 
                             dbo.CustomersRates.DistDrv, 
                             dbo.CustomersRates.DistDrvN, 
                             dbo.CustomersRates.DistDrvB, 
                             dbo.CustomersRates.DistDrvNB, 
                             dbo.CustomersRates.DistDrvC, 
                             dbo.CustomersRates.DistDrvNC, 
                             dbo.CustomersRates.DistWeight, 
                             dbo.CustomersRates.DistWeightDrv, 
                             dbo.CustomersRates.ID, 
                             dbo.CustomersRates.PriceLevel, 
                             dbo.CustomersRates.MinMi, 
                             dbo.CustomersRates.MinMiN, 
                             dbo.CustomersRates.BaseChargeMi, 
                             dbo.CustomersRates.BaseChargeMiN, 
                             dbo.CustomersRates.PerMileRateMi, 
                             dbo.CustomersRates.PerMileRateMiN, 
                             dbo.CustomersRates.WeightChargeMi, 
                             dbo.CustomersRates.WeightChargeMiN, 
                             dbo.CustomersRates.MultipleRMi, 
                             dbo.CustomersRates.MultipleRMiN, 
                             dbo.CustomersRates.MultipleDMi, 
                             dbo.CustomersRates.MultipleDMiN, 
                             dbo.CustomersRates.MultipleSMi, 
                             dbo.CustomersRates.MultipleSMiN, 
                             dbo.CustomersRates.FloorMi, 
                             dbo.CustomersRates.FloorMiN, 
                             dbo.CustomersRates.DistFreeWeight, 
                             dbo.CustomersRates.SkidCharge, 
                             dbo.CustomersRates.Dist2A, 
                             dbo.CustomersRates.Dist2AN, 
                             dbo.CustomersRates.Dist2B, 
                             dbo.CustomersRates.Dist2BN, 
                             dbo.CustomersRates.Dist2C, 
                             dbo.CustomersRates.Dist2CN, 
                             dbo.CustomersRates.Dist2D, 
                             dbo.CustomersRates.Dist2DN, 
                             dbo.CustomersRates.Dist2E, 
                             dbo.CustomersRates.Dist2EN, 
                             dbo.CustomersRates.Dist2DrvA, 
                             dbo.CustomersRates.Dist2DrvAN, 
                             dbo.CustomersRates.Dist2DrvB, 
                             dbo.CustomersRates.Dist2DrvBN, 
                             dbo.CustomersRates.Dist2DrvC, 
                             dbo.CustomersRates.Dist2DrvCN, 
                             dbo.CustomersRates.Dist2DrvD, 
                             dbo.CustomersRates.Dist2DrvDN, 
                             dbo.CustomersRates.Dist2DrvE, 
                             dbo.CustomersRates.Dist2DrvEN, 
                             dbo.CustomersRates.Dist2Weight, 
                             dbo.CustomersRates.Dist2WeightDrv, 
                             dbo.CustomersRates.Dist2FreeWeight, 
                             dbo.CustomersRates.FreeWeight, 
                             dbo.CustomersRates.DayStart, 
                             dbo.CustomersRates.DayEnd, 
                             dbo.CustomersRates.PickHold, 
                             dbo.CustomersRates.PickHoldN, 
                             dbo.CustomersRates.FreeMiles, 
                             dbo.CustomersRates.chkTollsDisabled, 
                             dbo.CustomersRates.chkParkingDisabled, 
                             dbo.CustomersRates.chkResDisabled, 
                             dbo.CustomersRates.chkForceMileage, 
                             dbo.CustomersRates.chkTollsNC, 
                             dbo.CustomersRates.chkParkingNC, 
                             dbo.CustomersRates.chkResNC, 
                             dbo.CustomersRates.SecondCtr, 
                             dbo.CustomersRates.SecondCtrN, 
                             dbo.CustomersRates.SecondAWB, 
                             dbo.CustomersRates.SecondAWBN, 
                             dbo.CustomersRates.OutboundChargeNEaAdd, 
                             dbo.CustomersRates.OutboundChargeN, 
                             dbo.CustomersRates.OutboundChargeEaAdd, 
                             dbo.CustomersRates.OutboundCharge, 
                             dbo.CustomersRates.InboundChargeNEaAdd, 
                             dbo.CustomersRates.InboundChargeN, 
                             dbo.CustomersRates.InboundChargeEaAdd, 
                             dbo.CustomersRates.InboundCharge, 
                             dbo.CustomersRates.nfoPerMileRate, 
                             dbo.CustomersRates.nfoAfterHours, 
                             dbo.CustomersRates.nfoWeightCharge, 
                             dbo.CustomersRates.nfoFreeMiles, 
                             dbo.CustomersRates.nfoFreeWeight, 
                             dbo.CustomersRates.nfoAdditionalBox, 
                             ROW_NUMBER() 
                               OVER ( 
                                 PARTITION BY dbo.Customers.AcctNo 
                                 ORDER BY dbo.CustomersRates.Effective DESC) rn 
           FROM dbo.Customers 
                INNER JOIN dbo.CustomersRates 
                        ON dbo.Customers.AcctNo = dbo.CustomersRates.AcctNo) 
SELECT * 
  FROM CTE 
 WHERE rn = 1 
 ORDER BY AcctNo, 
          Effective DESC 

Open in new window

just curious, why do you want to select TOP (999999) records?
0
 

Author Comment

by:Dale Massicotte
ID: 39862136
Not because I am lazy - but because my time is limited as a business owner

When upgrading from SQL2000 to 2008 my queries did not run the query correctly 'TOP 100 PERCENT' so I found that using a real number made it work - however I know that it is not the best way to do things  :)
0
 

Author Comment

by:Dale Massicotte
ID: 39862152
A question

I copied the query into SQL Mgt Studio to test and it worked.  But will not allow me to save.

Does that make any sense?

ADO error    ;WITH CTE
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39862180
I suspect you are using the (very flawed) designer.  Instead use the query window to save your VIEW
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39862277
Do you want to create a view with the query?
Also, you can remove the TOP in SELECT clause.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 25 total points
ID: 39862849
Sharath,

Also, you can remove the TOP in SELECT clause.
Unfortunately (as the author describes here http:#a39862136), they are using the kluge of TOP (999999) ... ORDER BY as they want a sorted VIEW.  The author understands it is not a good idea, but is used for expediency and they no doubt realize it may break with the next version of SQL Server.
0
 

Author Comment

by:Dale Massicotte
ID: 39863061
I use CTRL + N to open a new query window in Mgt Studio.
I removed the TOP 99999 from select

I also no longer need to sort by EFFECTIVE as there is only one EFFECTIVE per ACCT with this new query.   Maybe I do not understand something...

I want to save this as a view in my database.

When I try I get
Incorrect syntax near ';'.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39863779
What is the compatibility level of the database?

I suspect if it were 90 or greater is should be fine.

If it is in fact 90 or greater then post the code in its entirety here.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39865417
I suggest using a derived table instead of a CTE.  Btw, I also used aliases in place of the full table name, since it makes the query easier to read.


SELECT *
FROM (
    SELECT     TOP (999999) c.AcctNo, cr.Effective, c.Letter, c.BillingContact, c.Title,
                          c.ContactPerson, c.BillCompanyName, c.BillStreetAddress, c.BillCity, c.BillState,
                          c.BillZip, c.CompanyName, c.Active, c.StreetAddress, c.City, c.State,
                          c.Zip, c.PhoneNo, c.PhoneWaybillAr, c.PhoneWaybillNo, c.PhoneNo2, c.Ext,
                          c.FaxNo, c.Attention, c.BillingRefName, c.RefMask, c.Rep, cr.MinCa,
                          cr.MinCaN, cr.MinVa, cr.MinVaN, cr.Min14, cr.Min14N,
                          cr.Min20, cr.Min20N, cr.BaseCharge, cr.BaseChargeN,
                          cr.BaseChargeVa, cr.BaseChargeVaN, cr.BaseCharge14, cr.BaseCharge14N,
                          cr.BaseCharge20, cr.BaseCharge20N, cr.PerMileRate, cr.PerMileRateN,
                          cr.PerMileRateVa, cr.PerMileRateVaN, cr.PerMileRate14, cr.PerMileRate14N,
                          cr.PerMileRate20, cr.PerMileRate20N, cr.WeightCharge, cr.WeightChargeN,
                          cr.WeightChargeVa, cr.WeightChargeVaN, cr.WeightCharge14, cr.WeightCharge14N,
                          cr.WeightCharge20, cr.WeightCharge20N, cr.WaitingCharge, cr.WaitingChargeN,
                          cr.PrivCharge, cr.PrivChargeN, cr.BadAddressCharge, cr.BadAddressChargeN,
                          cr.MultipleR, cr.MultipleRN, cr.MultipleRVa, cr.MultipleRVaN,
                          cr.MultipleR14, cr.MultipleR14N, cr.MultipleR20, cr.MultipleR20N,
                          cr.MultipleD, cr.MultipleDN, cr.MultipleDVa, cr.MultipleDVaN,
                          cr.MultipleD14, cr.MultipleD14N, cr.MultipleD20, cr.MultipleD20N,
                          cr.MultipleS, cr.MultipleSN, cr.MultipleSVa, cr.MultipleSVaN,
                          cr.MultipleS14, cr.MultipleS14N, cr.MultipleS20, cr.MultipleS20N,
                          cr.Floor, cr.FloorN, cr.FloorVa, cr.FloorVaN, cr.Floor14,
                          cr.Floor14N, cr.Floor20, cr.Floor20N, cr.InsDelChg1, cr.InsDelChg2,
                          cr.Dist, cr.DistN, cr.DistB, cr.DistNB, cr.DistC,
                          cr.DistNC, cr.DistDrv, cr.DistDrvN, cr.DistDrvB, cr.DistDrvNB,
                          cr.DistDrvC, cr.DistDrvNC, cr.DistWeight, cr.DistWeightDrv, cr.ID,
                          cr.PriceLevel, cr.MinMi, cr.MinMiN, cr.BaseChargeMi,
                          cr.BaseChargeMiN, cr.PerMileRateMi, cr.PerMileRateMiN, cr.WeightChargeMi,
                          cr.WeightChargeMiN, cr.MultipleRMi, cr.MultipleRMiN, cr.MultipleDMi,
                          cr.MultipleDMiN, cr.MultipleSMi, cr.MultipleSMiN, cr.FloorMi,
                          cr.FloorMiN, cr.DistFreeWeight, cr.SkidCharge, cr.Dist2A, cr.Dist2AN,
                          cr.Dist2B, cr.Dist2BN, cr.Dist2C, cr.Dist2CN, cr.Dist2D,
                          cr.Dist2DN, cr.Dist2E, cr.Dist2EN, cr.Dist2DrvA, cr.Dist2DrvAN,
                          cr.Dist2DrvB, cr.Dist2DrvBN, cr.Dist2DrvC, cr.Dist2DrvCN,
                          cr.Dist2DrvD, cr.Dist2DrvDN, cr.Dist2DrvE, cr.Dist2DrvEN,
                          cr.Dist2Weight, cr.Dist2WeightDrv, cr.Dist2FreeWeight, cr.FreeWeight,
                          cr.DayStart, cr.DayEnd, cr.PickHold, cr.PickHoldN, cr.FreeMiles,
                          cr.chkTollsDisabled, cr.chkParkingDisabled, cr.chkResDisabled, cr.chkForceMileage,
                          cr.chkTollsNC, cr.chkParkingNC, cr.chkResNC, cr.SecondCtr,
                          cr.SecondCtrN, cr.SecondAWB, cr.SecondAWBN, cr.OutboundChargeNEaAdd,
                          cr.OutboundChargeN, cr.OutboundChargeEaAdd, cr.OutboundCharge,
                          cr.InboundChargeNEaAdd, cr.InboundChargeN, cr.InboundChargeEaAdd,
                          cr.InboundCharge, cr.nfoPerMileRate, cr.nfoAfterHours, cr.nfoWeightCharge,
                          cr.nfoFreeMiles, cr.nfoFreeWeight, cr.nfoAdditionalBox,
                          ROW_NUMBER() OVER (PARTITION BY cr.AcctNo ORDER BY cr.Effective DESC) AS row_num
    FROM         dbo.Customers c INNER JOIN
                          dbo.CustomersRates cr ON c.AcctNo = cr.AcctNo
) AS derived
WHERE
    row_num = 1
ORDER BY
    AcctNo, Effective DESC
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question