SQL Query Statements
SQL Query Statements
Aggregate Functions
Aggregate functions (such as SUM, AVG, COUNT, COUNT(*), MAX, and MIN) generate summary
values in query result sets. An aggregate function (with the exception of COUNT(*)) processes all
the selected values in a single column to produce a single result value. Aggregate functions can be
applied to all rows in a view, to a subset of the view specified by a WHERE clause, or to one or more
groups of rows in the view. When an aggregate function is applied, a single value is generated from
each set of rows.
Important
Be aware that NULL values are not included in aggregate results. For example, if you have 100
records and 8 of them have a NULL column value for the property that you are counting, the count
will return only 92 results.
An example of using the COUNT(*) aggregate function is displayed in the following query (from the
Count clients for each site SMS 2003 built-in report) and example result set.
SELECT v_Site.SiteCode, v_Site.SiteName, v_Site.ReportingSiteCode,
Count(SMS_Installed_Sites0) AS 'Count'
FROM v_Site, v_RA_System_SMSInstalledSites InsSite
WHERE v_Site.SiteCode = InsSite.SMS_Installed_Sites0
GROUP BY SiteCode, SiteName, ReportingSiteCode
ORDER BY SiteCode
GETDATE ()
The GETDATE function produces the current date and time in SQL Server internal format for
datetime values. GETDATE takes the NULL parameter ().
The following example results in the current system date and time:
SELECT GETDATE()
2005-05-29 10:10:03.001
2005-05-31 10:10:03.001
20118
2005-02-18 10:10:03.001
… … … …
Top of page
JOINS
To create effective reports in SMS, you need to understand how to join different views to get the
expected data. There are three types of joins: inner, outer, and cross. In addition, there are three
types of outer joins: left, right, and full. The self join utilizes any of the above joins, but joins
records from the same view.
Inner Joins
In an inner join, records from two views are combined and added to a query's results only if the
values of the joined fields meet certain specified criteria. If you use an inner join by using the
ResourceID to join the v_R_System and v_GS_WORKSTATION_STATUS views, the result would be a
list of all systems and their last hardware scan date.
SELECT v_R_System.Netbios_Name0 AS MachineName,
v_GS_WORKSTATION_STATUS.LastHWScan AS [Last HW Scan]
FROM v_R_System INNER JOIN v_GS_WORKSTATION_STATUS
ON v_R_System.ResourceID = v_GS_WORKSTATION_STATUS.ResourceID
Outer Joins
An outer join returns all rows from the joined views whether or not there's a matching row between
them. The ON clause supplements the data rather than filtering it. The three types of outer joins
(left, right, and full) indicate the main data's source. Outer joins can be particularly helpful when
you have NULL values in a view.
Client2 NULL
Cross Join
A cross join returns the product of two views, not the sum. Each row in the left view is matched up
with each row in the right view. It's the set of all possible row combinations, without any filtering.
However, if you add a WHERE clause, a cross join functions as an inner join—it uses the condition to
filter all possible row combinations down to the ones you want.
Self Join
A self join uses any of the above join types, but where a view is joined to itself. In database
diagrams, a self join is called a reflexive relationship.
Top of page
NOT IN Keyword Phrase
Subqueries with the keyword phrase NOT IN are very useful to find information about a set of data
that doesn’t meet a certain criteria. In the following example, the query returns the NetBIOS name
of all computers that do NOT have notepad.exe installed. You must first create a query that can
detect all computers that have the selected file installed as follows:
SELECT DISTINCT v_R_System.Netbios_Name0
FROM v_R_System INNER JOIN v_GS_SoftwareFile
ON (v_GS_SoftwareFile.ResourceID = v_R_System.ResourceId)
WHERE v_GS_SoftwareFile.FileName = 'Notepad.exe'
After confirming that the first query displays all of the computers that have Notepad.exe installed,
the following subquery statement will use the NOT IN keyword phrase to find all computer names
which do NOT have the Notepad.exe file installed:
SELECT DISTINCT Netbios_Name0
FROM v_R_System
WHERE Netbios_Name0 NOT IN
(SELECT DISTINCT v_R_System.Netbios_Name0
FROM v_R_System INNER JOIN v_GS_SoftwareFile
ON (v_GS_SoftwareFile.ResourceID = v_R_System.ResourceId)
WHERE v_GS_SoftwareFile.FileName = 'Notepad.exe')
ORDER by Netbios_Name0
Top of page
Joining to External Data
It is possible to retrieve data from two different databases in one query. The following example
query will join both the SMS Client Health and SMS Site databases to get information about the
state of the client and the status of a selected SMS Advertisement:
SELECT v_ClientAdvertisementStatus.AdvertisementID,
SMS_ClientHealth.dbo.ClientHealthResults.Name,
v_ClientAdvertisementStatus.LastStateName,
SMS_ClientHealth.dbo.ClientHealthResults.Classification,
v_ClientAdvertisementStatus.LastStatusTime,
v_ClientAdvertisementStatus.LastStatusMessageID
FROM v_ClientAdvertisementStatus INNER JOIN
SMS_ClientHealth.dbo.ClientHealthResults
ON v_ClientAdvertisementStatus.ResourceID =
SMS_ClientHealth.dbo.ClientHealthResults.ResourceID
WHERE (v_ClientAdvertisementStatus.AdvertisementID = 'ABC20001')
Note
This query uses SMS_ClientHealth as the SMS Client Health database name and ABC20001 as
the AdvertisementID and will need to be modified with values relevant to your site for it to run
successfully.
select DATENAME(DW,DATEADD(dd,-DATEPART(dd,GETDATE())
+1,GETDATE())) AS FirstDay;
2. how to find 6th highest salary from employee table
select TOP 1 salary FROM (SELECT DISTINCT TOP 6 salary FROM employee
ORDER BY salary DESC) a ORDER BY salary