Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
hi i have table customers where each customer transaction is stored in the tabletable fields are customerid, customername,transdsate,item_purchasedi wnat to get count of transactions for each customer.I know group by would do the trick but i want the count in this manner:if a customer has three records of info on same date ,it should count that as 1 transaction instead of 3.How can i achieve it.
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-07-17 : 17:07:59
SELECT CustomerID, COUNT(DISTINCT TransDate)FROM CustomersGROUP BY CustomerIDSELECT CustomerID, COUNT(*)FROM (SELECT CustomerID, TransDateFROM CustomersGROUP BY CustomerID, TransDate) AS d GROUP BY CustomerIDN 56°04'39.26"E 12°55'05.63"
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-07-18 : 01:59:19
if TransDate has timepart also
SELECT CustomerID, COUNT(*)FROM (SELECT CustomerID,DATEADD(dd,DATEDIFF(dd,0,TransDate),0)FROM CustomersGROUP BY CustomerID,DATEADD(dd,DATEDIFF(dd,0,TransDate),0))tGROUP BY CustomerID