SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Retrieving Unique Customers in SQL


Retrieving Unique Customers in SQL

Author
Message
CheyneL
CheyneL
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 24
Hi,

I am trying to work out the number of UNIQUE customers within a date range. So between the 1st and 8th I want to display the NUMBER of Unique customers per day(1st - 8th), meaning the customers on the 1st must not show on the 2nd - 8th, and those on the 2nd must not show on the 3rd - 8th etc.

My query up to now is displaying ALL the customers, from the 1st to the 8th:

CREATE TABLE #UNIQUE_CUST_UNSUCC1
(DateTimeStamp Varchar(20),
CustomerName Varchar(100),
CustomerID INT)
INSERT INTO #UNIQUE_CUST_UNSUCC1
(DateTimeStamp,
CustomerName,
CustomerID)

SELECT (CAST (CAST (RIGHT ('0' + CAST(YEAR([DateTimeStamp]) AS VARCHAR(4)),4) + '/' +
RIGHT('0' + CAST((MONTH([DateTimeStamp])) AS VARCHAR(2)),2) + '/' +
RIGHT('0' + CAST(DAY([DateTimeStamp]) AS VARCHAR(2)),2) AS CHAR(10))AS VARCHAR(10))),
CUS.CustomerName,CUS.CustomerID
FROM EasyRecharge.dbo.CustomerUsage CUS
INNER JOIN EasyRecharge.dbo.Customers_Dormant CNU on CUS.FromMSISDN = CNU.MSISDN
WHERE ([DateTimeStamp]
between '2009/07/01 00:00:00' AND '2009/07/08 23:59:59')
GROUP BY CUS.CustomerName,CUS.CustomerID, (CAST (CAST (RIGHT ('0' + CAST(YEAR([DateTimeStamp]) AS VARCHAR(4)),4) + '/' +
RIGHT('0' + CAST((MONTH([DateTimeStamp])) AS VARCHAR(2)),2) + '/' +
RIGHT('0' + CAST(DAY([DateTimeStamp]) AS VARCHAR(2)),2) AS CHAR(10))AS VARCHAR(10)))
ORDER BY (CAST (CAST (RIGHT ('0' + CAST(YEAR([DateTimeStamp]) AS VARCHAR(4)),4) + '/' +
RIGHT('0' + CAST((MONTH([DateTimeStamp])) AS VARCHAR(2)),2) + '/' +
RIGHT('0' + CAST(DAY([DateTimeStamp]) AS VARCHAR(2)),2) AS CHAR(10))AS VARCHAR(10)))

I hope someone can help me.'

Thank you

Regards,
Cheyne
matt6288
matt6288
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1334 Visits: 1360

SELECT MIN(dateTimeStamp), CUS.customerName, CUS.CustomerID
FROM EasyRecharge.dbo.CustomerUsage CUS
INNER JOIN EasyRecharge.dbo.Customers_Dormant CNU on CUS.FromMSISDN = CNU.MSISDN
WHERE ([DateTimeStamp]
between '2009/07/01 00:00:00' AND '2009/07/08 23:59:59')
GROUP BY CUS.customerName, CUS.customerID




This is assuming that the dateTimeStamp column is of dateTime datatype. You might have to do all of that formatting to this column but I think this is close to what you were asking.

Let me know if this works.
CheyneL
CheyneL
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 24
It doesnt seem to work. It is still displaying all the customers.

Basically what I want is,eg day 1 -7. day 7 must show only the unique customers, which means that the customers between day 1 - 6 must not be displayed in day 7. The same goes for day 6 , must not have day 1 - 5 customers in it.

But I want to display the number of customers, and not the actualy names. Eg

Day 1 : 5 Unique customers
Day 2 : 2 Unique customers
Day 3 : 0 Unique customers(which means that the same customers in Day 1 and 2 might have done some transactions
Day 4 : 15 Unique Customers(this is excluding day 1 - 3' customers)

ect.

any ideas how ?
manish.singh-1128425
manish.singh-1128425
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 106
Hi,

In that case you can try the bellow query

SELECT a.dt, COUNT(*) As Customer_Count
FROM
(
SELECT MIN(Convert(varchar,dateTimeStamp,101)) AS dt, CUS.customerName, CUS.CustomerID
FROM EasyRecharge.dbo.CustomerUsage CUS
INNER JOIN EasyRecharge.dbo.Customers_Dormant CNU on CUS.FromMSISDN = CNU.MSISDN
WHERE ([DateTimeStamp]
between '2009/07/01 00:00:00' AND '2009/07/08 23:59:59')
GROUP BY CUS.customerName, CUS.customerID
) A
GROUP BY a.dt


CheyneL
CheyneL
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 24
That actually worked perfect!!!!!!!

Thank you so much!!!

How do I accept your reply as the answer?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search