SQL question from a newbie
Hi,
I have the following SQL script and am stuck:
SELECT
v_R_System . Name0 AS MachineName ,
DATEDIFF ( day , v_MonthlyUsageSummary . LastUsage , GETDATE ()) AS DaysSinceLastUsed ,
v_MeteredProductRule . RuleID ,
v_MeteredFiles . FileName
FROM v_R_System
INNER JOIN v _MonthlyUsageSummary ON v_R_System . ResourceID = v_MonthlyUsageSummary . ResourceID
INNER JOIN v_MeteredFiles
INNER JOIN v_MeteredProductRule ON v_MeteredFiles . RuleID = v_MeteredProductRule . RuleID ON v_MonthlyUsageSummary . FileID = v_MeteredFiles . MeteredFileID
WHERE ( v_MeteredProductRule . RuleID = 173 )
ORDER BY MachineName , DaysSinceLastUsed
This is an SCCM query and I am trying to get a list of machines with the LastUsage with the least value (fewest number of days). The problem I am having is that I am getting ALL the usage records for each machine???
I have tried using the MIN function but end up with all kinds of syntax, non-boolean, etc., errors and have NO clue what to do.
I cant imagine its that difficult... or is it?
Thanks for your help!
Lee
I have the following SQL script and am stuck:
SELECT
v_MeteredFiles
FROM
WHERE
ORDER
I have tried using the MIN function but end up with all kinds of syntax, non-boolean, etc., errors and have NO clue what to do.
I cant imagine its that difficult... or is it?
Thanks for your help!
Lee
0 Comments
[ + ] Show comments
Answers (5)
Please log in to answer
Posted by:
airwolf
14 years ago
Posted by:
Jsaylor
14 years ago
I'm not entirely sure this is possible in a single SQL statement for a return result against multiple single column values (IE, computer names.) If you attempt to specify any number in the WHERE clause, you're going to only return results against all computers that have that number (E.G., if the minimum value of v_MonthlyUsageSummary.LastUsage is 6 days for any computer in your environment, then only computers with a value of 6 days will display.)
You might try digging through some SQL manuals and have a look at the available functions, and keep in mind that if you're going to use any aggregate clauses, you're going to have to use GROUP BY, and HAVING to keep the syntax correct. Try poking around here if you haven't already for ideas.
You might try digging through some SQL manuals and have a look at the available functions, and keep in mind that if you're going to use any aggregate clauses, you're going to have to use GROUP BY, and HAVING to keep the syntax correct. Try poking around here if you haven't already for ideas.
Posted by:
captain_planet
14 years ago
errrrm.....I don't use SCCM myself (so i don't know what v_MeteredProductRule.RuleID is....), but shouldn't the query look like this (note the second inner join)? Also, why are you ordering by the machine name?:
SELECT
v_R_System.Name0 AS MachineName,
DATEDIFF(day, v_MonthlyUsageSummary.LastUsage, GETDATE()) AS DaysSinceLastUsed,
v_MeteredProductRule.RuleID,
v_MeteredFiles.FileName
FROM v_R_System
INNER JOIN v_MonthlyUsageSummary ON v_R_System.ResourceID = v_MonthlyUsageSummary.ResourceID
INNER JOIN v_MeteredFiles ON v_MonthlyUsageSummary.FileID = v_MeteredFiles.MeteredFileID
INNER JOIN v_MeteredProductRule ON v_MeteredFiles.RuleID = v_MeteredProductRule.RuleID
WHERE (v_MeteredProductRule.RuleID = 173)
ORDER BY DaysSinceLastUsed
Posted by:
Jsaylor
14 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.