SQL-spørring resultat må summeres

stemmer
30

koden er

select customerid, count(campaignid) as T, Convert (varchar, CreatedOn,23) from customerbenefits 
where campaignid='6EDBB808-1A91-4B1D-BE1D-27EF15C5D4C7'
and createdon between '2019-09-01' and '2019-10-01'
group by customerid,CreatedOn
having count(campaignid)>1
order by createdon desc 

resultatet er

 --                         id            /       count  /time         
 --18655680-3B5E-4001-1984-00000000  / 12   /2019-09-30
 --18655680-3B5E-4001-1984-00000000 /  7    /    2019-09-30
 --18655680-3B5E-4001-1984-00000000 / 6    / 2019-09-30

Jeg vil resultere i

 --                         id      /             count / time     
 --  18655680-3B5E-4001-1984-00000000 / 25/ 2019-09-30

Jeg vil ha det gruppert i tids filtrer og sum teller.

Hvordan kan jeg endre spørringen?

Publisert på 07/10/2019 klokken 12:59
bruker
På andre språk...                            


1 svar

stemmer
0

Bruk to nivåer av aggregering:

select customerid, dte, sum(T)
from (select customerid, count(*) as T, convert(varchar(255), CreatedOn, 23) as dte
      from customerbenefits 
      where campaignid = '6EDBB808-1A91-4B1D-BE1D-27EF15C5D4C7' and
            createdon >= '2019-09-01' and
            createdon < '2019-10-01'
      group by customerid, CreatedOn
      having count(*) > 1
     ) t
group by customerid, dte
order by createdon desc ;

Legg merke til at jeg endret datoen sammenligninger så midnatt på 2019-10-01 er ikke inkludert i dataene for september.

Svarte 07/10/2019 kl. 13:01
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more