Any assistance with merging the following two select queries would be greatly appreciated.
The first query returns 1000 rows
The second query returns 1000 rows.
RSV.ResourceID is the primary key
RSV.ResourceID by itself outputs 14,000 rows.
The end goal is to output all 14,000 rows for RSV.ResourceID and have it include a column with results for GBU.BrowserName0 and a column with results for GLD.Size0.
Select RSV.ResourceID, GBU.BrowserName0
From (
select ResourceID,
max(GBU.UsagePercentage0) as UsagePercentage0
from
v_GS_BROWSER_USAGE GBU
WHERE
GBU.UsagePercentage0 > 0
group by ResourceID)
as s
INNER JOIN v_GS_BROWSER_USAGE GBU on (GBU.Resourceid = s.ResourceID and GBU.UsagePercentage0 = s.UsagePercentage0)
RIGHT OUTER JOIN v_R_System_Valid RSV on GBU.Resourceid = RSV.ResourceID
___________________________________________
Select RSV.ResourceID, GLD.Size0
From (select ResourceID,
max(gld.deviceid0) as deviceid0
from
v_GS_LOGICAL_DISK GLD
WHERE gld.deviceid0='C:'
group by ResourceID)
as d
INNER JOIN v_GS_LOGICAL_DISK GLD on (GLD.Resourceid = d.ResourceID and GLD.deviceid0 = d.deviceid0)
RIGHT OUTER JOIN v_R_System_Valid RSV on GLD.ResourceID = RSV.ResourceID
Intersect only returns the row if the values in both columns match. As a result, only the nulls like the following are being returned.
The SQL INTERSECT clause/operator is used to combine two SELECT statements, but returns rows only from the first SELECT statement that are identical to a row in the second SELECT statement. This means INTERSECT returns only common rows returned by the two SELECT statements.
It looks like you want to get the BrowserName for the browser with the highest usage - per resource, and the Size of the C: drive for that resource. I am assuming that v_GS_LOGICAL_DISK only has a single entry per drive per resource - so a single C: drive for each resource.
Something like this may work:
Select rsv.ResourceID
, gb.BrowserName0
, gl.Size0
From v_R_System_Valid rsv
Cross Apply (Select Top 1
gbu.BrowserName0
From v_GS_BROWSER_USAGE gbu
Where gbu.ResourceID = rsv.ResourceID
Order By
gbu.UsagePercentage0 desc
) gb
Cross Apply (Select Top 1
gld.Size0
From v_GS_LOGICAL_DISK gld
Where gld.deviceid0 = 'C:'
And gld.ResourceID = rsv.ResourceID
Order By
gld.Size0 desc
) gl;
Another option would be to just use simple joins - and get the top 1...
Select Top 1 With Ties
From v_R_System_Valid rsv
Inner Join v_GS_BROWSER_USAGE gbu On gbu.ResourceID = rsv.ResourceID
Inner Join v_GS_LOGICAL_DISK gld On gld.ResourceID = rsv.ResourceID
And gld.deviceid0 = 'C:'
Order By
row_number() over(Partition By rsv.ResourceID Order By gbu.BrowserName0 desc);
If you have entries in v_R_System_Valid that do not have entries in the other tables and you want those included in the results then change Cross Apply to Outer Apply in the first query - and Inner Join to Left Join in the second query.