jclemo
asked on
Join the results of two queries on one record
I have these two queries which return the expected results.. how do I get them into one record grouped on the MM/YYYY which I have defined?
SELECT CAST(DATEPART(month,ScanSt artDate) AS NVARCHAR(50)) + '/' + CAST(DATEPART(year,ScanSta rtDate) AS NVARCHAR(50))[Month]
,COUNT(VulnInstance) AS [Open]
FROM Vulnerabilities
GROUP BY DATEPART(month,ScanStartDa te), DATEPART(year,ScanStartDat e)
ORDER BY DATEPART(month,ScanStartDa te),DATEPA RT(year,Sc anStartDat e)
SELECT CAST(DATEPART(month,Remedi ation_Date ) AS NVARCHAR(50)) + '/' + CAST(DATEPART(year,Remedia tion_Date) AS NVARCHAR(50))[Month]
,COUNT(VulnInstance) AS [Closed]
FROM Vulnerabilities
GROUP BY DATEPART(month,Remediation _Date), DATEPART(year,Remediation_ Date)
ORDER BY DATEPART(month,Remediation _Date),DAT EPART(year ,Remediati on_Date)
I want the results to be as follows:
Month | Open | Closed | Residual
09/2008 | 200 | 20 | 180
10/2008 | 14 | 10 | 184
11/2008 | 10 | 194 | 0
I have the open and close piece.. but I can't figure out how to get them on the same line to do the calculation, thanks
SELECT CAST(DATEPART(month,ScanSt
,COUNT(VulnInstance) AS [Open]
FROM Vulnerabilities
GROUP BY DATEPART(month,ScanStartDa
ORDER BY DATEPART(month,ScanStartDa
SELECT CAST(DATEPART(month,Remedi
,COUNT(VulnInstance) AS [Closed]
FROM Vulnerabilities
GROUP BY DATEPART(month,Remediation
ORDER BY DATEPART(month,Remediation
I want the results to be as follows:
Month | Open | Closed | Residual
09/2008 | 200 | 20 | 180
10/2008 | 14 | 10 | 184
11/2008 | 10 | 194 | 0
I have the open and close piece.. but I can't figure out how to get them on the same line to do the calculation, thanks
forgot to convert the datetime to string
with t( monthh, openn, closee, residual)
as (
select a.[Month], a.[Open], b.[Close], a.[Open] - b.[Close] as Residual
from (
SELECT convert(datetime, cast(datepart(year, Remediation_Date) as varchar) + cast(datepart(month, Remediation_Date) as varchar) + '01' ) [Month]
,COUNT(VulnInstance) AS [Closed]
FROM Vulnerabilities
GROUP BY convert(datetime, cast(datepart(year, Remediation_Date) as varchar) + cast(datepart(month, Remediation_Date) as varchar) + '01' )
) a
, (
SELECT convert(datetime, cast(datepart(year, ScanStartDate) as varchar) + cast(datepart(month, ScanStartDate) as varchar) + '01' ) [Month]
,COUNT(VulnInstance) AS [Open]
FROM Vulnerabilities
GROUP BY convert(datetime, cast(datepart(year, ScanStartDate) as varchar) + cast(datepart(month, ScanStartDate) as varchar) + '01' )
) b
where a.[Month] = b.[Month]
)
select CAST(DATEPART(month, monthh) AS NVARCHAR(50)) + '/' + CAST(DATEPART(year, monthh) AS NVARCHAR(50))
, openn
, closee
, (select sum(residual) from t b where a.monthh => b.monthh) as residual
from t a
ASKER
thanks for the response jamesqu:
based on your response and some tinkering of course I ended up with the following query:
SELECT DATEPART(month,ScanStartDa te),[a].[O pen],[b].[ Closed] FROM
(
SELECT DATEPART(month,ScanStartDa te) AS [Date], Count(VulnInstance)
AS [Open]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,ScanStartDa te)
) a,
(
SELECT DATEPART(month,Remediation _Date) AS [Date], Count(VulnInstance)
AS [Closed]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation _Date)
) b,
Vulnerabilities
WHERE a.Date = DATEPART(month,ScanStartDa te)
AND b.Date = DATEPART(month,ScanStartDa te)
GROUP BY DATEPART(month,ScanStartDa te),[a].[O pen],[b].[ Closed]
ORDER BY DATEPART(month,ScanStartDa te)
This seems to do what I want it to do but I find that BOTH OPEN and CLOSED must have values in them for the query to display a record.. for example.. if I have this data
Month | Open | Closed
3 10 0
or this data:
Month | Open | Closed
3 0 24
I see no record for the month at all.. I'm pretty sure this has to do with my where statement at the bottom.. would you mind taking a look when you get a chance,, thanks in advance
based on your response and some tinkering of course I ended up with the following query:
SELECT DATEPART(month,ScanStartDa
(
SELECT DATEPART(month,ScanStartDa
AS [Open]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,ScanStartDa
) a,
(
SELECT DATEPART(month,Remediation
AS [Closed]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation
) b,
Vulnerabilities
WHERE a.Date = DATEPART(month,ScanStartDa
AND b.Date = DATEPART(month,ScanStartDa
GROUP BY DATEPART(month,ScanStartDa
ORDER BY DATEPART(month,ScanStartDa
This seems to do what I want it to do but I find that BOTH OPEN and CLOSED must have values in them for the query to display a record.. for example.. if I have this data
Month | Open | Closed
3 10 0
or this data:
Month | Open | Closed
3 0 24
I see no record for the month at all.. I'm pretty sure this has to do with my where statement at the bottom.. would you mind taking a look when you get a chance,, thanks in advance
ASKER
k.. got that fixed with the following query.. but now the fields have NULL in them.. I think the last piece of the puzzle is to make those zero.
SELECT DATEPART(month, Vulnerabilities_2.ScanStar tDate) AS [Date], a.[Open], b.Closed FROM
(SELECT DATEPART(month, ScanStartDate) AS Date, COUNT(VulnInstance) AS [Open] FROM Vulnerabilities
WHERE(Addinfo >=(SELECT Threshold FROM CurrentThresholds WHERE (Rating = 'LOW')))
GROUP BY DATEPART(month, ScanStartDate)) AS a
LEFT OUTER JOIN
Vulnerabilities AS Vulnerabilities_2 ON a.Date = DATEPART(month, Vulnerabilities_2.ScanStar tDate)
LEFT OUTER JOIN
(SELECT DATEPART(month, Remediation_Date) AS Date, COUNT(VulnInstance) AS Closed FROM Vulnerabilities
WHERE (Addinfo >=(SELECT Threshold FROM CurrentThresholds WHERE(Rating = 'LOW')))
GROUP BY DATEPART(month, Remediation_Date)) AS b
ON DATEPART(month, Vulnerabilities_2.ScanStar tDate) = b.Date
GROUP BY DATEPART(month, Vulnerabilities_2.ScanStar tDate), a.[Open], b.Closed
ORDER BY [Date]
SELECT DATEPART(month, Vulnerabilities_2.ScanStar
(SELECT DATEPART(month, ScanStartDate) AS Date, COUNT(VulnInstance) AS [Open] FROM Vulnerabilities
WHERE(Addinfo >=(SELECT Threshold FROM CurrentThresholds WHERE (Rating = 'LOW')))
GROUP BY DATEPART(month, ScanStartDate)) AS a
LEFT OUTER JOIN
Vulnerabilities AS Vulnerabilities_2 ON a.Date = DATEPART(month, Vulnerabilities_2.ScanStar
LEFT OUTER JOIN
(SELECT DATEPART(month, Remediation_Date) AS Date, COUNT(VulnInstance) AS Closed FROM Vulnerabilities
WHERE (Addinfo >=(SELECT Threshold FROM CurrentThresholds WHERE(Rating = 'LOW')))
GROUP BY DATEPART(month, Remediation_Date)) AS b
ON DATEPART(month, Vulnerabilities_2.ScanStar
GROUP BY DATEPART(month, Vulnerabilities_2.ScanStar
ORDER BY [Date]
you may need a derived table c
(
select DATEPART(month,Remediation _Date) AS [Date]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation _Date)
union
select DATEPART(month, ScanStartDate) AS [Date]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month, ScanStartDate)
) c
(
select DATEPART(month,Remediation
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation
union
select DATEPART(month, ScanStartDate) AS [Date]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month, ScanStartDate)
) c
SELECT DATEPART(month,ScanStartDate),[a].[Open],[b].[Closed] FROM
Vulnerabilities,
(
select DATEPART(month,Remediation_Date) AS [Date]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation_Date)
union
select DATEPART(month, ScanStartDate) AS [Date]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month, ScanStartDate)
) c
left join
(
SELECT DATEPART(month,ScanStartDate) AS [Date], Count(VulnInstance)
AS [Open]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,ScanStartDate)
) a on a.[Date] = c.[Date]
left join
(
SELECT DATEPART(month,Remediation_Date) AS [Date], Count(VulnInstance)
AS [Closed]
FROM Vulnerabilities
WHERE AddInfo >= (SELECT Threshold FROM CurrentThresholds WHERE Rating = 'LOW')
GROUP BY DATEPART(month,Remediation_Date)
) b on b.[Date] = c.[Date]
WHERE c.[Date] = DATEPART(month,ScanStartDate)
GROUP BY DATEPART(month,ScanStartDate),[a].[Open],[b].[Closed]
ORDER BY DATEPART(month,ScanStartDate)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i have a feeling that your query missing one of the below case
Month | Open | Closed
3 10 0
or this data:
Month | Open | Closed
3 0 24
seems it's the second
Month | Open | Closed
3 10 0
or this data:
Month | Open | Closed
3 0 24
seems it's the second
ASKER
Thanks again jamesgu, do say that because I am using the "ScanStartDate" to generate the dates in the top select query ???
yes, you're about that,
that's why a full list of all month as [c] is needed
that's why a full list of all month as [c] is needed
ASKER
ahh.. every record in my data does have a ScanStartDate value though.. it's a pre-populated field so it is not possible for it to be NULL.. perhaps my question was misleading when I said:
or this data:
Month | Open | Closed
3 0 24
when that scenario really can't exist. Sorry for the confusion.. for the other question about substituting the 0 for the nulls should I open another question after I award the points for this one?
or this data:
Month | Open | Closed
3 0 24
when that scenario really can't exist. Sorry for the confusion.. for the other question about substituting the 0 for the nulls should I open another question after I award the points for this one?
ASKER
Oh.. I didn't see that you put a comment at the top of ID: 22936720 which actually answered my question.. thank you so much.. if only I could give you more than 500 point I would.
ASKER
If only I could give you more than 500 point I would
Open in new window