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

data for Min/Max rows Expand / Collapse
Author
Message
Posted Monday, December 31, 2012 1:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 25, 2013 4:17 AM
Points: 7, Visits: 56
Hi All,
Please can someone tell me how to select min/max rows in a single query similar to the one below.


Select custfirst, custlast,
first_order_id,
first_orderdate,
first_order_total_items,
first_order_total_cost
last_orderdate,
last_order_id,
last_orderdate,
last_order_total_items,
last_order_total_cost

from custmer where

custid in (select max(orderdate) as last_orderdate from orders group by custid, orderdate order by orderdate DESC) OR
custid in (select min(orderdate) as first_orderdate from orders group by custid, orderdate)


Thanks in advance
Post #1401304
Posted Monday, December 31, 2012 1:58 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 4,315, Visits: 9,647
Does the OrderId column follow an ascending pattern? What I mean is, can we assume that, for each customer,

Min(OrderId) is the first order
and
Max(OrderId) is the most recent order
?

Also, if a customer has only one order, would you like to see it repeated as both first and last?


____________________________________________________________________________________________

Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
Post #1401315
Posted Monday, December 31, 2012 2:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 25, 2013 4:17 AM
Points: 7, Visits: 56
OrderID is auto gen number but can use orderdate for this purpose.

If there is one order placed, then I want both min/max to contain the same long with rest of the data belongs to min/max rows


Post #1401324
Posted Monday, December 31, 2012 2:51 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 4,315, Visits: 9,647
popq79 34287 (12/31/2012)
OrderID is auto gen number but can use orderdate for this purpose.

If there is one order placed, then I want both min/max to contain the same long with rest of the data belongs to min/max rows




OK, this is a bit ugly (and untested) but should get you on the right track:

with FOrders
as (
select CustId
,Ro = Row_Number() over (
partition by CustId order by OrderDate asc
)
,FirstOrderId = OrderId
,FirstOrderDate = OrderDate
)
,LOrders
as (
select CustId
,Ro = Row_Number() over (
partition by CustId order by OrderDate desc
)
,LastOrderId = OrderId
,LastOrderDate = OrderDate
)
select c.CustId
,FOrders.FirstOrderDate
,FOrders.FirstOrderId
,LOrders.LastOrderDate
,LOrders.LastOrderId
,
from Customer c
left join FOrders on c.CustId = FOrders.CustId
left join LOrders on c.CustId = LOrders.CustId
where (
FOrders.ro = 1
or FOrders.ro is null
)
and (
LOrders.ro = 1
or LOrders.ro is null
)

--Edit fixed row_number() clauses


____________________________________________________________________________________________

Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
Post #1401326
Posted Tuesday, January 01, 2013 8:31 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:58 PM
Points: 2,368, Visits: 3,243
Won't something like this work also?

CREATE TABLE #Customer
(CustID VARCHAR(10)
,OrderDate DATETIME)

INSERT INTO #Customer
SELECT 'A', '2012-01-31' UNION ALL SELECT 'A', '2012-03-31' UNION ALL SELECT 'A', '2012-05-15'
UNION ALL SELECT 'B', '2012-03-31' UNION ALL SELECT 'B', '2012-05-31' UNION ALL SELECT 'B', '2012-07-15'

SELECT CustID, OrderDate -- Include other fields you want returned here
FROM (
SELECT CustID, OrderDate -- Include other fields you need here
,a=MIN(OrderDate) OVER (PARTITION BY CustID)
,b=MAX(OrderDate) OVER (PARTITION BY CustID)
FROM #Customer) a
WHERE a = OrderDate OR b = OrderDate

DROP TABLE #Customer


Happy New Year!



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 #1401642
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse