简体   繁体   中英

Count number of rows in db for each corelated record for different time periods

I am strugling to find the best solution for this situation. I have to tables Account and Incident related 1:N to each other. I want a query that can give the total number of incidents with open status, close status ect, in differet time periods. Example, tickets opened in last 15 days, opened in last 30 days. Expected Ouput example (not showing all columns):

`+--------------+---------------+---------------+----------------+-----------------+
| TckOpenLast7 | TckOpenLast15 | TckOpenLast30 | TckClosedLast7 | TckClosedLast15 |
+--------------+---------------+---------------+----------------+-----------------+
|        40463 |        50463  |         60463 |        4       |               8 |
+--------------+---------------+---------------+----------------+-----------------+`

So I tried this simple query below and gives me the correct results, but I need a more elegant, better performing query.

Select TckOpenLast7= (Select Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=0 And CustomerId=@Accid),
TckOpenLast15=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -15, GETDATE() )And statecode=0 And CustomerId=@Accid),
TckOpenLast30=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=0 And CustomerId=@Accid),
TckClosedLast7=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=1 And CustomerId=@Accid),
TckClosedLast15=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -15, GETDATE()) And statecode=1 And CustomerId=@Accid),
TckClosedLast30=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=1 And CustomerId=@Accid),
TckInProcessLast7=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=1 And CustomerId=@Accid),
TckInProcessLast15=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -15, GETDATE()) And statecode=1 And CustomerId=@Accid),
TckInProcessLast30=(Select  Count(incidentId) From Incident where CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=1 And CustomerId=@Accid)

I am woring on a SSIS package that retrives data from a sql server databese using an ADO.Net connection.

Use conditional aggregation:

select sum(case when CreatedOn >= DATEADD(DAY, -7, GETDATE()) and state = 0
                then 1 else 0
           end) as TckOpenLast7,
       sum(case when CreatedOn >= DATEADD(DAY, -15, GETDATE()) and state = 0
                then 1 else 0
           end) as TckOpenLast15,
       . . .
From Incident
where CustomerId = @Accid;

For performance, you want an index on (CustomerId) , or even (CustomerId, state, CreatedOn) .

Also: Note that GETDATE() has a time component. I am guessing you want the dates without a time component. A simple way is to remove this as a separate "variable" in the FROM clause:

select sum(case when CreatedOn >= DATEADD(DAY, -7, v.date) and state = 0
                then 1 else 0
           end) as TckOpenLast7,
       sum(case when CreatedOn >= DATEADD(DAY, -15, v.date) and state = 0
                then 1 else 0
           end) as TckOpenLast15,
       . . .
from Incident i cross join
     (values (convert(date, getdate()))) v(date)
where CustomerId = @Accid;

You could use conditional aggregation

select
  sum(iif(CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=0, 1, 0) TckOpenLast7,
  sum(iif(CreatedOn >= DATEADD(DAY, -15, GETDATE() )And statecode=0, 1, 0) TckOpenLast15,
  sum(iif(CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=0, 1, 0) TckOpenLast30,
  sum(iif(CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=1, 1, 0) TckClosedLast7,
  sum(iif(CreatedOn >= DATEADD(DAY, -15, GETDATE()) And statecode=1, 1, 0) TckClosedLast15,
  sum(iif(CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=1, 1, 0) TckClosedLast30,
  sum(iif(CreatedOn >= DATEADD(DAY, -7, GETDATE()) And statecode=1, 1, 0) TckInProcessLast7,
  sum(iif(CreatedOn >= DATEADD(DAY, -15, GETDATE()) And statecode=1, 1, 0) TckInProcessLast15,
  sum(iif(CreatedOn >= DATEADD(DAY, -30, GETDATE()) And statecode=1, 1, 0) TckInProcessLast30
from Incident
where CustomerId=@Accid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM