/build/static/layout/Breadcrumb_cap_w.png

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

0 Comments   [ + ] Show comments

Answers (5)

Posted by: airwolf 14 years ago
Red Belt
0
The WHERE clause is your filter - this is where you need to limit your results. I'm not familiar with the SCCM database, so I can't give you code.
Posted by: lsiegel 14 years ago
Senior Yellow Belt
0
Thanks Andy! I'll play with that....
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
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.
Posted by: captain_planet 14 years ago
Black Belt
0
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
Second Degree Blue Belt
0
He's ordering by the machine name because .LastUsage returns multiple results for a given machine, this allows him to at least see each machine result in individual result blocks per machine name.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ