Solved

Repeating a query across columns

Posted on 2008-10-02
7
273 Views
Last Modified: 2012-05-05
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
Comment
Question by:jclemo
  • 3
  • 2
7 Comments
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
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

by:jclemo
Comment Utility
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

by:
sijishJohn earned 250 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:jclemo
Comment Utility
Yep, that's it.. I was one "SELECT" off.. thanks a ton... How do I get points reallocated?
0
 
LVL 4

Expert Comment

by:sijishJohn
Comment Utility
try to reallocate...else mark it as assisted solution....
thanks
0
 

Author Closing Comment

by:jclemo
Comment Utility
Rockin.. thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now