Solved

# Repeating a query across columns

Posted on 2008-10-02
Medium Priority
317 Views
I have this query which works fine and gives me the data I'm looking for exactly.

SELECT COUNT(DATEDIFF(Day,SLA_Date,GetDate())) AS [0 TO 30 Days]
FROM Vulnerabilities
WHERE Addinfo >= 500 AND CurrentStatus = 'OPEN' AND DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 0 AND 30

However,  I need to have the same query for BETWEEN 31 AND 60 and BETWEEN 61 and 90 etc.. I want to be able to run the query so that I get 3 columns one named 0 to 30 days, one named 31 to 60 days etc.. I just don't know how to repeat the query in the same results.
0
Question by:jclemo
[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
• 3
• 2

LVL 37

Expert Comment

ID: 22631414
SELECT COUNT(case when DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 0 AND 30 then DATEDIFF(Day,SLA_Date,GetDate()) else 0 end) AS [0 TO 30 Days] ,
COUNT(case when DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 31 AND 60 then DATEDIFF(Day,SLA_Date,GetDate()) else 0 end) AS [31 TO 60 Days] ,
COUNT(case when DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 61 AND 90 then DATEDIFF(Day,SLA_Date,GetDate()) else 0 end) AS [61 TO 90 Days]
FROM Vulnerabilities
WHERE Addinfo >= 500 AND CurrentStatus = 'OPEN'
0

Author Comment

ID: 22631451
Actually, it appears that this solution did NOT work.. it gave me three columns with the total count in it..
0

LVL 4

Accepted Solution

sijishJohn earned 1000 total points
ID: 22631485
try this...

SELECT(
SELECT  COUNT(DATEDIFF(Day,SLA_Date,GetDate()))
FROM Vulnerabilities
WHERE Addinfo >= 500  AND CurrentStatus = 'OPEN' AND
DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 0 AND 30) AS [0 TO 30 Days],

(SELECT  COUNT(DATEDIFF(Day,SLA_Date,GetDate()))
FROM Vulnerabilities
WHERE Addinfo >= 500  AND CurrentStatus = 'OPEN' AND
DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 31 AND 60) AS [31 TO 60 Days],

(SELECT  COUNT(DATEDIFF(Day,SLA_Date,GetDate()))
FROM Vulnerabilities
WHERE Addinfo >= 500  AND CurrentStatus = 'OPEN'  AND
DATEDIFF(Day,SLA_Date,GetDate()) BETWEEN 61 AND 90 ) AS [61 TO 90 Days]
0

Author Comment

ID: 22631507
Yep, that's it.. I was one "SELECT" off.. thanks a ton... How do I get points reallocated?
0

LVL 4

Expert Comment

ID: 22631535
try to reallocate...else mark it as assisted solution....
thanks
0

Author Closing Comment

ID: 31502665
Rockin.. thanks
0

## Featured Post

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month12 days, left to enroll

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

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