Solved

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

Posted on 2013-10-28
13
424 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
  • 7
  • 4
  • 2
13 Comments
 
LVL 48

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 24

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 24

Accepted Solution

by:
chaau earned 350 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 48

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 48

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 48

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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 150 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 48

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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

777 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