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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL Count Help

Author  Topic 

ccline19
Starting Member

2 Posts

Posted - 2011-01-07 : 16:03:21
I have a very simply query that returns the total number of orders/items for a given customer. My problem/question is I need to know how many of these orders have an order number(ordern) that start with the number '5' and return that as well. I can not see the solution with out having to create a separate query....any suggestions?

SELECT o.CustomerN, COUNT(DISTINCT o.OrderN) AS TotalOrders, COUNT(d.OrderSeqN) AS TotalLineItem
FROM FGIOrderMast o INNER JOIN
FGIOrderDetl d ON o.OrderN = d.OrderN
WHERE o.CustomerN = '23' AND o.dateentered between '10/27/2010' AND '11/23/2010' AND o.OrderStatus = 1
GROUP BY o.CustomerN

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-01-07 : 16:18:20
SUM(case when left(o.OrderN, 1) = '5' then 1 else 0 end)

=======================================
Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986)
Go to Top of Page

ccline19
Starting Member

2 Posts

Posted - 2011-01-07 : 16:38:40
quote:
Originally posted by Bustaz Kool

SUM(case when left(o.OrderN, 1) = '5' then 1 else 0 end)

=======================================
Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986)



Thank you Bustaz.
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-01-10 : 13:32:45
Mi gusto!

=======================================
Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986)
Go to Top of Page
   

- Advertisement -