SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

deanglen
Yak Posting Veteran

64 Posts

Posted - 04/30/2013 :  09:39:13  Show Profile  Reply with Quote
Hi

This script below works fine but if I wanted to add another column which is OrderDate from the orders table in the results how would I do this?

select
c.CustomerID, C.Email, c.FirstName + ' ' + c.LastName AS CustomerName, count(o.OrderNumber) As NoOrders
from
customer c
join dbo.Orders o with (NOLOCK) on o.CustomerID = c.CustomerID
and c.IsRegistered = 1
and o.TransactionState = 'CAPTURED'
and c.OkToEmail = 1
where
o.OrderDate >=dateadd(day,datediff(day,0,GetDate())- 365,0)
GROUP BY
c.CustomerID, c.FirstName + ' ' + c.LastName, c.Email
having
count(o.OrderNumber) = 1
order by
c.CustomerID

Thanks

mmkrishna1919
Yak Posting Veteran

India
94 Posts

Posted - 04/30/2013 :  09:52:31  Show Profile  Reply with Quote
Add that column in the select list and group by list.

Thanks.

M.MURALI kRISHNA
Go to Top of Page

ahmeds08
Aged Yak Warrior

India
656 Posts

Posted - 04/30/2013 :  10:15:45  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
select
c.CustomerID, C.Email, c.FirstName + ' ' + c.LastName AS CustomerName, count(o.OrderNumber) As NoOrders,o.orderdate
from
customer c
join dbo.Orders o with (NOLOCK) on o.CustomerID = c.CustomerID
and c.IsRegistered = 1
and o.TransactionState = 'CAPTURED'
and c.OkToEmail = 1
where
o.OrderDate >=dateadd(day,datediff(day,0,GetDate())- 365,0)
GROUP BY
c.CustomerID, c.FirstName + ' ' + c.LastName, c.Email
having
count(o.OrderNumber) = 1
order by
c.CustomerID,
o.orderdate



mohammad.javeed.ahmed@gmail.com
Go to Top of Page

deanglen
Yak Posting Veteran

64 Posts

Posted - 04/30/2013 :  10:22:51  Show Profile  Reply with Quote
Great! Thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000