user1428798
user1428798

Reputation: 1546

how to count new buyers in table orders

i need to count the number of the new buyers in this years ( client who passed order this year and they haven't any order before 2017), how can i do this please

this is my table

SLECT TOP 1000 [OrderId]
      ,[clientId]
      ,[TotalAmount]
      ,[DATE]
  FROM [Orders]

Upvotes: 0

Views: 115

Answers (3)

Maverick Sachin
Maverick Sachin

Reputation: 883

To get the total count number of the new buyers in year - 2017

Select 
    Count(Distinct ClientID) As [Number of new Buyers]
From Orders
Where DATE >= '2017-01-01';

Upvotes: 0

Brian
Brian

Reputation: 1248

Close, Gordon :)

To get a count of new buyers, you'd need to do a Count Distinct:

select count(distinct clientid)
from orders
having min(orderdate) >= '2017-01-01';

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270633

You can get the using group by and having:

select clientid
from orders
group by clientid
having min(orderdate) >= '2017-01-01';

Upvotes: 4

Related Questions