how to prevent calling same table multiple times to get the various count

how to minimize calling same table multiple times to get the various count on different types.how to optimize this query?here i called EMPHISTORYREC  6 times.can i avoid calling this table this many times?


SELECT WARE_RUNNING_COUNT,WARE_FAILED_COUNT,WARE_COMPLETED_COUNT,
HOUSE_COMPLETED_COUNT,HOUSE_RUNNING_COUNT,HOUSE_FAILED_COUNT,

(WARE_RUNNING_COUNT+WARE_FAILED_COUNT+WARE_COMPLETED_COUNT) WARE_TOTAL,

(HOUSE_COMPLETED_COUNT+HOUSE_RUNNING_COUNT+HOUSE_FAILED_COUNT) HOUSE_TOTAL,

(WARE_RUNNING_COUNT+HOUSE_RUNNING_COUNT) TOTAL_RUNNING,
(WARE_FAILED_COUNT+HOUSE_FAILED_COUNT) TOTAL_FAILED,
(WARE_COMPLETED_COUNT+HOUSE_COMPLETED_COUNT) TOTAL_COMPLETED

FROM(
	SELECT  (SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('WARE_AA','WARE_BB') AND 
status='NEW' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7) WARE_RUNNING_COUNT,
(SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('WARE_AA','WARE_BB') AND 
status='ERROR' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7) WARE_FAILED_COUNT,
(SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('WARE_AA','WARE_BB') AND 
status='COMPLETED' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7)  WARE_COMPLETED_COUNT,
(SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('HOUSE_AA','HOUSE_BB') AND 
status='COMPLETED' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7)  HOUSE_COMPLETED_COUNT,
(SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('HOUSE_AA','HOUSE_BB') AND 
status='NEW' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7) HOUSE_RUNNING_COUNT,
(SELECT  count(1)
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('HOUSE_AA','HOUSE_BB') AND 
status='ERROR' AND 
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7) HOUSE_FAILED_COUNT,
) T

Open in new window

LVL 20
chaitu chaituAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
chaauConnect With a Mentor Commented:
Sorry, there was a typo in my previous post. Try this one. Also added the comment from PortletPaul here:
;WITH cte as(
SELECT  status, 
CASE WHEN JOB_TYPE IN('WARE_AA','WARE_BB') THEN 'WARE' ELSE 'HOUSE' END AS jType,
count(1) cnt
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('WARE_AA','WARE_BB','HOUSE_AA','HOUSE_BB') 
AND status IN('ERROR','COMPLETED','NEW') AND
[JOB_DATE] > dateadd(day, datediff(day,0, getdate() ), -7) 
GROUP BY
status, 
CASE WHEN JOB_TYPE IN('WARE_AA','WARE_BB') THEN 'WARE' ELSE 'HOUSE' END
)
SELECT SUM(CASE jType='WARE' AND status='NEW' THEN cnt ELSE 0 END) AS WARE_RUNNING_COUNT,
SUM(CASE jType='WARE' AND status='ERROR' THEN cnt ELSE 0 END) AS WARE_FAILED_COUNT,
SUM(CASE jType='WARE' AND status='COMPLETED' THEN cnt ELSE 0 END) AS WARE_COMPLETED_COUNT,
SUM(CASE jType='HOUSE' AND status='NEW' THEN cnt ELSE 0 END) AS HOUSE_RUNNING_COUNT,
SUM(CASE jType='HOUSE' AND status='ERROR' THEN cnt ELSE 0 END) AS HOUSE_FAILED_COUNT,
SUM(CASE jType='HOUSE' AND status='COMPLETED' THEN cnt ELSE 0 END) AS HOUSE_COMPLETED_COUNT,
SUM(CASE jType='WARE' THEN cnt ELSE 0 END) AS WARE_TOTAL,
SUM(CASE jType='HOUSE' THEN cnt ELSE 0 END) AS HOUSE_TOTAL,
SUM(CASE status='NEW' THEN cnt ELSE 0 END) AS TOTAL_RUNNING,
SUM(CASE status='ERROR' THEN cnt ELSE 0 END) AS TOTAL_FAILED,
SUM(CASE status='COMPLETED' THEN cnt ELSE 0 END) AS TOTAL_COMPLETED
FROM cte

Open in new window

0
 
PortletPaulCommented:
first thing is not to approach the date filter the way you are doing it

right now you are applying several functions to the many rows of data

instead apply functions to the single getdate()

e.g. instead of this:

DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7) -- changing data to suit criterion

do this

[JOB_DATE] > dateadd(day, datediff(day,0, getdate() ), -7) -- change criterion to suit data

(select if [JOB_DATE] is >= 7 days before today at 00:00:00)

see: "Sargable" e.g. http://en.wikipedia.org/wiki/Sargable

Rules of thumb
Avoid applying functions on data values in a sql condition.
Avoid non-sargable predicates and replace them with sargable equivalents.
0
 
chaauCommented:
Yes you can, using a CTE. Have a look:

;WITH cte as(
SELECT  status, 
CASE WHEN JOB_TYPE IN('WARE_AA','WARE_BB') THEN 'WARE' ELSE 'HOUSE' END AS jType,
count(1) cnt
FROM EMPHISTORYREC 
WHERE  JOB_TYPE IN('WARE_AA','WARE_BB','HOUSE_AA','HOUSE_BB') 
AND status IN('ERROR','COMPLETED','NEW') AND
DATEDIFF(DAY,[JOB_DATE],GETDATE()) <= 7
GROUP BY
status, 
CASE WHEN JOB_TYPE IN('WARE_AA','WARE_BB') THEN 'WARE' ELSE 'HOUSE' END
)
SELECT SUM(CASE jType='WARE' AND status='NEW' THEN cnt ELSE 0 END) AS WARE_RUNNING_COUNT,
SUM(CASE jType='WARE' AND status='ERROR' THEN cnt ELSE 0 END) AS WARE_FAILED_COUNT,
SUM(CASE jType='WARE' AND status='COMPLETED' THEN cnt ELSE 0 END) AS WARE_COMPLETED_COUNT,
SELECT SUM(CASE jType='HOUSE' AND status='NEW' THEN cnt ELSE 0 END) AS HOUSE_RUNNING_COUNT,
SUM(CASE jType='HOUSE' AND status='ERROR' THEN cnt ELSE 0 END) AS HOUSE_FAILED_COUNT,
SUM(CASE jType='HOUSE' AND status='COMPLETED' THEN cnt ELSE 0 END) AS HOUSE_COMPLETED_COUNT,
SUM(CASE jType='WARE' THEN cnt ELSE 0 END) AS WARE_TOTAL,
SUM(CASE jType='HOUSE' THEN cnt ELSE 0 END) AS HOUSE_TOTAL,
SELECT SUM(CASE status='NEW' THEN cnt ELSE 0 END) AS TOTAL_RUNNING,
SUM(CASE status='ERROR' THEN cnt ELSE 0 END) AS TOTAL_FAILED,
SUM(CASE status='COMPLETED' THEN cnt ELSE 0 END) AS TOTAL_COMPLETED
FROM cte

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
chaitu chaituAuthor Commented:
PortletPaul,

I will change the date function.but what about the summation of various types?
0
 
PortletPaulCommented:
I would suggest this:
SELECT
        COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'NEW'       THEN 1 END) AS WARE_RUNNING_COUNT
      , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'ERROR'     THEN 1 END) AS WARE_FAILED_COUNT
      , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'COMPLETED' THEN 1 END) AS WARE_COMPLETED_COUNT
      , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'COMPLETED' THEN 1 END) AS HOUSE_COMPLETED_COUNT
      , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'NEW'       THEN 1 END) AS HOUSE_RUNNING_COUNT
      , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'ERROR'     THEN 1 END) AS HOUSE_FAILED_COUNT
FROM EMPHISTORYREC
WHERE [JOB_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7) -- change criterion to suit data
;

Open in new window

0
 
PortletPaulCommented:
actually this, sorry forgot the top bit:
SELECT
        WARE_RUNNING_COUNT
      , WARE_FAILED_COUNT
      , WARE_COMPLETED_COUNT
      , HOUSE_COMPLETED_COUNT
      , HOUSE_RUNNING_COUNT
      , HOUSE_FAILED_COUNT
      , (WARE_RUNNING_COUNT + WARE_FAILED_COUNT + WARE_COMPLETED_COUNT)    WARE_TOTAL
      , (HOUSE_COMPLETED_COUNT + HOUSE_RUNNING_COUNT + HOUSE_FAILED_COUNT) HOUSE_TOTAL
      , (WARE_RUNNING_COUNT + HOUSE_RUNNING_COUNT)                         TOTAL_RUNNING
      , (WARE_FAILED_COUNT + HOUSE_FAILED_COUNT)                           TOTAL_FAILED
      , (WARE_COMPLETED_COUNT + HOUSE_COMPLETED_COUNT)                     TOTAL_COMPLETED
FROM (
        SELECT
                COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'NEW'       THEN 1 END) AS WARE_RUNNING_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'ERROR'     THEN 1 END) AS WARE_FAILED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB')   AND status = 'COMPLETED' THEN 1 END) AS WARE_COMPLETED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'COMPLETED' THEN 1 END) AS HOUSE_COMPLETED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'NEW'       THEN 1 END) AS HOUSE_RUNNING_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'ERROR'     THEN 1 END) AS HOUSE_FAILED_COUNT
        FROM EMPHISTORYREC
        WHERE [JOB_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7) -- change criterion to suit data
        ) AS T
;

Open in new window

0
 
chaitu chaituAuthor Commented:
portapaul/chaau,

thanks for the solutions.do i need to use COUNT or SUM here for summation of various types in this case?
0
 
PortletPaulCommented:
adding the extra where clause conditions that chaau has proposed makes sense too
SELECT
        WARE_RUNNING_COUNT
      , WARE_FAILED_COUNT
      , WARE_COMPLETED_COUNT
      , HOUSE_COMPLETED_COUNT
      , HOUSE_RUNNING_COUNT
      , HOUSE_FAILED_COUNT
      , (WARE_RUNNING_COUNT + WARE_FAILED_COUNT + WARE_COMPLETED_COUNT)    WARE_TOTAL
      , (HOUSE_COMPLETED_COUNT + HOUSE_RUNNING_COUNT + HOUSE_FAILED_COUNT) HOUSE_TOTAL
      , (WARE_RUNNING_COUNT + HOUSE_RUNNING_COUNT)                         TOTAL_RUNNING
      , (WARE_FAILED_COUNT + HOUSE_FAILED_COUNT)                           TOTAL_FAILED
      , (WARE_COMPLETED_COUNT + HOUSE_COMPLETED_COUNT)                     TOTAL_COMPLETED
FROM (
        SELECT
                COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') AND status = 'NEW' THEN 1 END)         AS WARE_RUNNING_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') AND status = 'ERROR' THEN 1 END)       AS WARE_FAILED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') AND status = 'COMPLETED' THEN 1 END)   AS WARE_COMPLETED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'COMPLETED' THEN 1 END) AS HOUSE_COMPLETED_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'NEW' THEN 1 END)       AS HOUSE_RUNNING_COUNT
              , COUNT(CASE WHEN JOB_TYPE IN ('HOUSE_AA', 'HOUSE_BB') AND status = 'ERROR' THEN 1 END)     AS HOUSE_FAILED_COUNT
        FROM EMPHISTORYREC
        WHERE [JOB_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7) -- change criterion to suit data
                AND JOB_TYPE IN ('WARE_AA', 'WARE_BB', 'HOUSE_AA', 'HOUSE_BB')
                AND status IN ('ERROR', 'COMPLETED', 'NEW')
        ) AS T
;

Open in new window

0
 
PortletPaulConnect With a Mentor Commented:
no points for this please, chaau has left out ' when ' in his suggestion - and it does look good as it's doing a summarization
;
WITH
        cte
        AS (
                SELECT
                        status
                      , CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') THEN 'WARE' ELSE 'HOUSE' END AS jType
                      , COUNT(1)                                                                  cnt
                FROM EMPHISTORYREC
                WHERE JOB_TYPE IN ('WARE_AA', 'WARE_BB', 'HOUSE_AA', 'HOUSE_BB')
                        AND status IN ('ERROR', 'COMPLETED', 'NEW')
                        AND [JOB_DATE] > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
                GROUP BY status
                       , CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') THEN 'WARE' ELSE 'HOUSE' END
                )
SELECT
        SUM(CASE WHEN jType = 'WARE' AND status = 'NEW' THEN cnt ELSE 0 END)        AS WARE_RUNNING_COUNT
      , SUM(CASE WHEN jType = 'WARE' AND status = 'ERROR' THEN cnt ELSE 0 END)      AS WARE_FAILED_COUNT
      , SUM(CASE WHEN jType = 'WARE' AND status = 'COMPLETED' THEN cnt ELSE 0 END)  AS WARE_COMPLETED_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'NEW' THEN cnt ELSE 0 END)       AS HOUSE_RUNNING_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'ERROR' THEN cnt ELSE 0 END)     AS HOUSE_FAILED_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'COMPLETED' THEN cnt ELSE 0 END) AS HOUSE_COMPLETED_COUNT
      , SUM(CASE WHEN jType = 'WARE' THEN cnt ELSE 0 END)                           AS WARE_TOTAL
      , SUM(CASE WHEN jType = 'HOUSE' THEN cnt ELSE 0 END)                          AS HOUSE_TOTAL
      , SUM(CASE WHEN status = 'NEW' THEN cnt ELSE 0 END)                           AS TOTAL_RUNNING
      , SUM(CASE WHEN status = 'ERROR' THEN cnt ELSE 0 END)                         AS TOTAL_FAILED
      , SUM(CASE WHEN status = 'COMPLETED' THEN cnt ELSE 0 END)                     AS TOTAL_COMPLETED
FROM cte;

Open in new window

It could be done without a CTE also, like this:
SELECT
        SUM(CASE WHEN jType = 'WARE' AND status = 'NEW' THEN cnt ELSE 0 END)        AS WARE_RUNNING_COUNT
      , SUM(CASE WHEN jType = 'WARE' AND status = 'ERROR' THEN cnt ELSE 0 END)      AS WARE_FAILED_COUNT
      , SUM(CASE WHEN jType = 'WARE' AND status = 'COMPLETED' THEN cnt ELSE 0 END)  AS WARE_COMPLETED_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'NEW' THEN cnt ELSE 0 END)       AS HOUSE_RUNNING_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'ERROR' THEN cnt ELSE 0 END)     AS HOUSE_FAILED_COUNT
      , SUM(CASE WHEN jType = 'HOUSE' AND status = 'COMPLETED' THEN cnt ELSE 0 END) AS HOUSE_COMPLETED_COUNT
      , SUM(CASE WHEN jType = 'WARE' THEN cnt ELSE 0 END)                           AS WARE_TOTAL
      , SUM(CASE WHEN jType = 'HOUSE' THEN cnt ELSE 0 END)                          AS HOUSE_TOTAL
      , SUM(CASE WHEN status = 'NEW' THEN cnt ELSE 0 END)                           AS TOTAL_RUNNING
      , SUM(CASE WHEN status = 'ERROR' THEN cnt ELSE 0 END)                         AS TOTAL_FAILED
      , SUM(CASE WHEN status = 'COMPLETED' THEN cnt ELSE 0 END)                     AS TOTAL_COMPLETED
FROM (
        SELECT
                status
              , CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') THEN 'WARE' ELSE 'HOUSE' END AS jType
              , COUNT(1)                                                                  cnt
        FROM EMPHISTORYREC
        WHERE JOB_TYPE IN ('WARE_AA', 'WARE_BB', 'HOUSE_AA', 'HOUSE_BB')
                AND status IN ('ERROR', 'COMPLETED', 'NEW')
                AND [JOB_DATE] > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
        GROUP BY status
               , CASE WHEN JOB_TYPE IN ('WARE_AA', 'WARE_BB') THEN 'WARE' ELSE 'HOUSE' END
        ) AS T
;

Open in new window

don't see any advantage in a CTE for this as there's no repetition needed.
0
 
chaitu chaituAuthor Commented:
small correction here.
when i check for NEW status then condition should be
if(status  =='NEW')
[JOB_START_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
else
[JOB_end_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
0
 
PortletPaulCommented:
sum(1) can do the same job as count(1)

BUT: In chaau's suggestion though he does a count(1) first, then sum( of that )
and that is necessary in his approach.
i.e. don't change the sums he uses to counts - the numbers would be wrong.

mine just uses count(1) so I guess this question doesn't apply
0
 
PortletPaulCommented:
??
when i check for NEW status then condition should be
if(status  =='NEW')
[JOB_START_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
else
[JOB_end_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
do you mean this needs to go into the where clause?

Try this:
WHERE (
         ( status = 'NEW' AND [JOB_START_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7) )
        OR 
          [JOB_end_DATE] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
      )
        AND JOB_TYPE IN ('WARE_AA', 'WARE_BB', 'HOUSE_AA', 'HOUSE_BB')
        AND status IN ('ERROR', 'COMPLETED', 'NEW')

Open in new window

0
 
chaitu chaituAuthor Commented:
yes.

but i did it using CASE

case when job_status = 'NEW' THEN
        [JOB_start_DATE] -- change criterion to suit data
        ELSE
        [JOB_ND_DATE] END  >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -7)
0
All Courses

From novice to tech pro — start learning today.