• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

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

0
chaitu chaitu
Asked:
chaitu chaitu
  • 7
  • 4
  • 2
2 Solutions
 
PortletPaulfreelancerCommented:
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
 
chaitu chaituAuthor Commented:
PortletPaul,

I will change the date function.but what about the summation of various types?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
chaauCommented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
??
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now