Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Change in Price Query Expand / Collapse
Author
Message
Posted Sunday, February 17, 2013 5:06 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 5:10 PM
Points: 101, Visits: 255
I have the data below and I want to update the pricechange column for when the price changes for an invoice in my netprice column. The pricechange column can be created on the fly, it doesn't exists yet. I want the first(top) record on the invoice to be the price to compare with for each invoice.


orderid Invoice netprice pricechange
1234 98989 39.99
2222 98989 39.99
3333 98989 45.00 x
4444 98989 39.99
5343 98989 39.99
4321 98989 37.00 x
2313 98989 42.00 x
4545 98989 39.99
3211 98989 39.99

1122 77277 45.00
2332 77277 45.00
4455 77277 30.00 x
6989 77277 45.00
3493 77277 55.00 x
3434 77277 45.00



Post #1421013
Posted Sunday, February 17, 2013 5:48 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:37 PM
Points: 2,370, Visits: 3,252
Something like this perhaps?

;WITH MyData (orderid, Invoice, netprice, pricechange) AS (
SELECT 1234,98989,39.99,' '
UNION ALL SELECT 2222, 98989, 39.99,' '
UNION ALL SELECT 3333, 98989, 45.00, 'x'
UNION ALL SELECT 4444, 98989, 39.99,' '
UNION ALL SELECT 5343, 98989, 39.99,' '
UNION ALL SELECT 4321, 98989, 37.00, 'x'
UNION ALL SELECT 2313, 98989, 42.00, 'x'
UNION ALL SELECT 4545, 98989, 39.99,' '
UNION ALL SELECT 3211, 98989, 39.99,' '
UNION ALL SELECT 1122, 77277, 45.00,' '
UNION ALL SELECT 2332, 77277, 45.00,' '
UNION ALL SELECT 4455, 77277, 30.00, 'x'
UNION ALL SELECT 6989, 77277, 45.00,' '
UNION ALL SELECT 3493, 77277, 55.00, 'x'
UNION ALL SELECT 3434, 77277, 45.00,' '
),
FirstOrder AS (
SELECT orderid, invoice, netprice
FROM (
SELECT orderid, invoice, netprice
,n=ROW_NUMBER() OVER (PARTITION BY invoice ORDER BY orderid)
FROM MyData) a
WHERE n=1
)
SELECT a.orderid, a.invoice, a.netprice, a.pricechange
,mypricechange=CASE WHEN a.netprice <> b.netprice THEN 'x' ELSE ' ' END
FROM MyData a
JOIN FirstOrder b ON a.invoice = b.invoice
ORDER BY a.Invoice, a.orderid


You can also do it with a correlated subquery.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1421016
Posted Sunday, February 17, 2013 6:22 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 5:10 PM
Points: 101, Visits: 255
Haven't tested it but looks like this is what I need. Thanks!


Post #1421019
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse