Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-10-28
13
Medium Priority
?
429 Views
Last Modified: 2013-10-29
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
Comment
Question by:chaitu chaitu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607695
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
 
LVL 25

Expert Comment

by:chaau
ID: 39607699
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 39607703
PortletPaul,

I will change the date function.but what about the summation of various types?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Accepted Solution

by:
chaau earned 1400 total points
ID: 39607708
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
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607711
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
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607717
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 39607729
portapaul/chaau,

thanks for the solutions.do i need to use COUNT or SUM here for summation of various types in this case?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607733
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
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 600 total points
ID: 39607741
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 39607749
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
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607752
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
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607758
??
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 39607888
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

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this article, we’ll look at how to deploy ProxySQL.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question