-1

I have 1 table named "Transactions" with the following columns: Date, ClientID, Amount.

I would like to have the active clients in the last 30 days.

Something like this:

Date        |    Active_Clients
2017/08/10  |      697
2017/08/11  |      710
2017/08/12  |      689

etc

Meaning: From 2017/08/10 minus 30 days to 2017/08/10 I had 697 active users.

I tryed many ways and didnt make it.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 4
    Tag the dbms you're using. When it comes to date/time many products are far from ANSI SQL compliant. – jarlh Aug 18 '17 at 13:00
  • Column Date's data type? – jarlh Aug 18 '17 at 13:00
  • 2
    _"I tryed many ways and didnt make it."_ For example? – Tim Schmelter Aug 18 '17 at 13:01
  • In addition to jarlh's question, please show what have you tried so far. – Laposhasú Acsa Aug 18 '17 at 13:01
  • Google "Count group by" and there will be thousands of examples for every RDMS. All you will need is, for SQL Server for example, is `where [Date} >= dateadd(day,-30,getdate())` – S3S Aug 18 '17 at 13:07
  • 1
    Possible duplicate of [How to use count and group by at the same select statement](https://stackoverflow.com/questions/2722408/how-to-use-count-and-group-by-at-the-same-select-statement) – S3S Aug 18 '17 at 13:08
  • @TimSchmelter This was my last try. The columns and tables are in portuguese select distinct data , (select distinct id_cliente, count(1) from appvitaminas..transacoes B group by id_cliente having b.Data between DATEADD(day,-30,a.Data) and A.Data ) Active_Clients from appvitaminas..transacoes A – Miguel Cunha Aug 18 '17 at 14:41

3 Answers3

1

One method looks something like this:

select d.dte, count(distinct t.clientid)
from (select '2017-08-10' as dte union all
      select '2017-08-11' as dte union all
      select '2017-08-12' as dte 
     ) d left join
     transactions t
     on t.date <= d.dte and t.date > d.dte - interval '30' day
group by d.dte
order by d.dte;

The exact syntax for date constants, date arithmetic, and subqueries with constant values differs by database.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You can try this code to:

select distinct [Date]
, (select  count(distinct B.clientid) from  Transactions B 
    where b.Date between DATEADD(day,-30,a.Date) and A.Date) Active_Clients
from Transactions A
Maurício Pontalti Neri
  • 1,060
  • 1
  • 10
  • 17
  • Hey Mauricio, The problem here is that it's not taking into account the clienID meaning that the same client may be counted more than once if he went to the store multiple times! Correct me if i'm wrong – Miguel Cunha Aug 18 '17 at 14:37
  • If you use count(distinct B.clientid), you will only count the unique visits from the client. I updated the solution. – Maurício Pontalti Neri Aug 18 '17 at 15:26
0

Thank you guys for your help.

I think I managed to find the answer with @gordonlinoff 's answer.

select distinct data , (select count(distinct id_cliente) from appvitaminas..transacoes B where b.Data between DATEADD(day,-30,a.Data) and A.Data) Active_Clients from appvitaminas..transacoes A