Solved

Repeating a query across columns

Posted on 2008-10-02
7
302 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
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

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

by:
sijishJohn earned 250 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
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.

 

Author Comment

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

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

Author Closing Comment

by:jclemo
ID: 31502665
Rockin.. thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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