T-SQL QUERIES:
REPORT FUNDAMENTALS TO
ADVANCED CONCEPTSAndrew Craig
@mracraig, [email protected]
Senior Consultant
itnetX, Switzerland
Dieter Gasser
@DiGaBlog, [email protected]
Head of Product Management
itnetX, Switzerland
@mracraig
MMS
Senior Consultant @ itnetX
Beard Combs
@DiGaBlog
BSc BIT, Author
Product Management @ itnetX
Retro Games
Andrew CraigDieter Gasser
AGENDA
Advanced Topics
Grouping data + Aggregates
Common table expressions
Working with custom database objects
Working with the view designer
Troubleshooting queries and
performance
Demystifying the SCCM database
What is (T)-SQL?
SQL Server Management Studio
Basic Database Objects
SQL data types
SQL Queries
Basic SELECT Statements
Manipulating data
WHERE statements
SORTING, TOP x, and Duplicates
Querying multiple tables
1. WHAT IS (T)-SQL?
SQL = Structured Query Language
A domain-specific language for managing data stored in relational database mgmt. systems
Became an ANSI standard in 1986, and ISO in 1987 (ISO/IEC 9075)
Despite standard, most SQL code is not completely portable between different RDBMS
T-SQL (Transact-SQL): Extension to SQL to support procedural programming, flow control,
variables, and more. Implemented by Microsoft and Sybase
DDL = Data Definition Language: Creating and modifying database objectsCREATE/DROP/ALTER/…
DML = Data Manipulation Language: Creating, adding, altering, and deleting dataSELECT/INSERT/UPDATE/DELETE/…
DCL = Data Control Language: Authorize access to dataGRANT/REVOKE/…
2. SQL SERVER MANAGEMENT STUDIO
Integrated environment for:
Configuring, managing, and administering SQL
Server
Developing SQL databases
Accessing, and modifying SQL data
Separate download since SQL 2016
(no longer included with SQL binaries)
https://go.microsoft.com/fwlink/?LinkID=840946
(Supports SQL 2008 – 2016)
2. SQL SERVER MANAGEMENT STUDIO
Query Editor
Full Screen (Shift+Alt+Enter)
Comment/Uncomment (Ctrl+K+C/U)
Results to Text/Grid/File
Execution Plan
Scripting Objects
Generate Change Script
Script Templates
Script Projects
Tutorial:
https://docs.microsoft.com/en-us/sql/tools/sql-
server-management-studio/tutorial-sql-server-
management-studio
Connecting to Server
Object Explorer
Registered Servers
Object Explorer / Object Explorer Details (F7)
Startup Options
3. BASIC DATABASE OBJECTS
Schemas
Tables
Columns
Indexes
Views
Stored Procedures
User-defined Functions
Schema
Table
User
Column
View
4. SQL DATA TYPES
Data Type Category Data Types
Exact numerics bigint, bit, decimal, int, money, numeric, smallint, smallmoney, tinyint
Approximate numerics float, real
Date and time date, datetime, datetime2, datetimeoffset, smalldatetime, time
Character strings char, varchar, text
Unicode character strings nchar, nvarchar, ntext
Binary strings binary, image, varbinary
Other data types cursor, hierarchyid, sql_variant, table, timestamp, uniqueidentifier, xml
5. SQL QUERIES
CREATE = INSERT
READ = SELECT
UPDATE = UPDATE
DELETE = DELETE
Basic SELECT statements
Select all rows from a table
SELECT * FROM v_R_System
Define columns
SELECT ResourceID, Name0 FROM v_R_System
Define column names
SELECTResourceID AS [Identifier],Name0 AS [Computer Name]
FROM v_R_System
Literals
SELECTResourceID AS [Identifier],Name0 AS [Computer Name],'Great Computer' AS [Computer Type]
FROM v_R_System
5.1. MANIPULATING DATA: FUNCTIONS
Aggregate Functions
Arithmetic Operations
Math Functions
String Functions
Date and Time Functions
System Functions
CASE Expressionss
AVG(), COUNT(), MAX(), MIN(), SUM()
Add ”+”, Subtract “-”, Multiply “*”, Divide “/”, Modulo “%”
Scalar functions: SIN(), COS(), TAN(), LOG(), SQRT()
CHAR(), CONCAT(),LEFT(),REPLACE(),SUBSTRING(), string_split()* in SQL 2016 only
DATEADD(), DATEDIFF(), DAY(), MONTH()
ISNULL()
CASE input_expression
WHEN expression THEN result_ expression […n]
ELSE result_ expression
END
CASE
WHEN bool_expression THEN result_expression […n]
ELSE result_ expression
END
5.1. MANIPULATING DATA: FUNCTIONS
select COUNT(*) as [Systems Count] from v_R_System
select MIN(SourceSize) as [Min Content Source Size in bytes] from v_Content
select MAX(SourceSize) as [Max Content Source Size in bytes] from v_Content
select AVG(SourceSize) as [Average Content Source Size in bytes] from v_Content
select AssignmentId,Descript AS Software,(Unknown+Error+RequirementsNotMet) AS [Failed
Deployments] from v_AppDeploymentSummary
select
SUBSTRING(Build01,1,1) as [Major Build Version]
,SUBSTRING(Build01,3,1) as [Minor Build Version]
,SUBSTRING(Build01,5,4) as [Minor Build Release]
from v_R_System
where Build01 like '6%'
5.1. MANIPULATING DATA: FUNCTIONS
select
Descript as [Software]
,DeploymentTime
,MONTH(DeploymentTime) as [Month]
,DATENAME(MONTH,DeploymentTime) as [Month Name]
,EOMONTH(DeploymentTime,0) as [End Of Month]
,DATEADD(DAY,1,DATEADD(MONTH,-1,EOMONTH(DeploymentTime,0))) as [Start Of Month]
from v_AppDeploymentSummary
select ISNULL(Build01,0) from v_R_System
5.1. MANIPULATING DATA: FUNCTIONSselect --distinct
CASE Build01
WHEN '6.1.7601' THEN 'WINDOWS 7'
WHEN '6.3.9600' THEN 'WINDOWS 7'
WHEN '10.0.14393' THEN 'WINDOWS 10'
END as [OS Build]
from v_R_System
select --distinct
CASE WHEN Build01 IS NULL THEN 0
WHEN SUBSTRING(Build01,2,1) = '.' THEN SUBSTRING(Build01,1,1)
ELSE SUBSTRING(Build01,1,2)
END as [Major Build Version]
,CASE WHEN Build01 IS NULL THEN 0
WHEN SUBSTRING(Build01,2,1) = '.' THEN SUBSTRING(Build01,3,1)
ELSE SUBSTRING(Build01,4,1)
END as [Minor Build Version]
,CASE WHEN Build01 IS NULL THEN 0
WHEN SUBSTRING(Build01,2,1) = '.' THEN SUBSTRING(Build01,5,4)
ELSE SUBSTRING(Build01,6,5) END as [Minor Build Release]
from v_R_System
5.2. MANIPULATING DATA: CONVERSIONS
CAST
CONVERT
PARSE
CAST(expression AS data_type[(length)])
CONVERT(data_type[(length)], expression [, style])
PARSE(string AS data_type)
5.2. MANIPULATING DATA: CONVERSIONS
select MIN(SourceSize)/1000000 as [Min Content Source Size in MB] from v_Content
select CAST(MIN(SourceSize)/1000000 AS decimal(2,2)) as [Min Content Source Size
in MB] from v_Content
select (CAST(MIN(SourceSize) AS decimal))/1000000 as [Min Content Source Size in
MB] from v_Content
select CAST((CAST(MIN(SourceSize) AS decimal))/1000000 AS decimal(6,6)) as [Min
Content Source Size in MB] from v_Content
select MAX(SourceSize)/1000000 as [Max Content Source Size in MB] from v_Content
select AVG(SourceSize)/1000000 as [Average Content Source Size in MB] from
v_Content
select GETDATE()
select CONVERT(datetime2,'170422',104)
5.3. WHERE STATEMENTS
Comparisons
Ranges
Lists
String Matching
NULL values
Multiple WHERE statements
=, >, <, >=, <=, <>, !=, !>, !<
[NOT] BETWEEN … AND …
[NOT] IN (…, …, …)
[NOT] LIKE
% : any number of characters
_ : any single character
[…] : one character of many
[^…] : not one character of many
IS [NOT] NULL
… AND/OR …
5.3. WHERE STATEMENTS
SELECT Name0, Last_Logon_Timestamp0, Resource_Domain_OR_Workgr0FROM v_R_System WHERE…
… Name0 = 'C12-NB-0151'
… Last_Logon_Timestamp0 > DATEADD(MONTH, -1, GETDATE())
… Resource_Domain_OR_Workgr0 <> '4ITS'
… Last_Logon_Timestamp0 BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59'
… Resource_Domain_OR_Workgr0 IN ('4ITS', 'MBT')
… Name0 LIKE 'C10%'
… Name0 LIKE 'C__-NB-____'
… Name0 LIKE '[^CS]%'
… Last_Logon_Timestamp0 IS NULL
… Last_Logon_Timestamp0 IS NULL AND Resource_Domain_OR_Workgr0 = 'MBT’
… (Last_Logon_Timestamp0 IS NULL AND Resource_Domain_OR_Workgr0 = 'MBT') OR
… (Last_Logon_Timestamp0 IS NOT NULL AND Resource_Domain_OR_Workgr0 = '4ITS')
5.4. SORTING, TOP X, AND DUPLICATES
ORDER BY
TOP X
DISTINCT
Order by columns
Return only a specific subset, can be ordered
Do not return duplicate rows
5.5. QUERYING MULTIPLE TABLES
LEFT JOIN
RIGHT JOIN
INNER JOIN
CROSS JOIN (Cartesian Product)
UNION [ALL]
Nested Queries
SELECT S.Name0, C.Manufacturer0, C.Model0
FROM v_R_System S
INNER JOIN v_GS_COMPUTER_SYSTEM C ON S.ResourceID = C.ResourceID
SELECT CIT.TypeName, COUNT(*) AS NumberOfCIs
FROM v_ConfigurationItems CI
FULL OUTER JOIN v_CITypes CIT ON CI.CIType_ID = CIT.CIType_ID
GROUP BY CIT.TypeName ORDER BY 2 DESC
SELECT * FROM
(SELECT 'A' AS c1 UNION SELECT 'B' UNION SELECT 'C') t1
CROSS JOIN
(SELECT '1' AS c2 UNION SELECT '2' UNION SELECT '3') t2
5.5. QUERYING MULTIPLE TABLES
-- All distinct rows of the two queries
SELECT Publisher0, DisplayName0, Version0 FROM v_GS_ADD_REMOVE_PROGRAMS
WHERE ResourceID = 16777219
UNION
SELECT Publisher0, DisplayName0, Version0 FROM v_GS_ADD_REMOVE_PROGRAMS_64
WHERE ResourceID = 16777219
-- All rows of the two queries
SELECT Publisher0, DisplayName0, Version0 FROM v_GS_ADD_REMOVE_PROGRAMS
WHERE ResourceID = 16777219
UNION ALL
SELECT Publisher0, DisplayName0, Version0 FROM v_GS_ADD_REMOVE_PROGRAMS_64
WHERE ResourceID = 16777219
5.5. QUERYING MULTIPLE TABLES
SELECT S.Name0 FROM v_R_System S WHERE S.ResourceID IN
(
SELECT ResourceID FROM v_GS_ADD_REMOVE_PROGRAMS WHERE DisplayName0 LIKE
'Microsoft .NET Framework 4.6%’
UNION
SELECT ResourceID FROM v_GS_ADD_REMOVE_PROGRAMS_64 WHERE DisplayName0 LIKE
'Microsoft .NET Framework 4.6%’
)
SELECT S.Name0, U.TopConsoleUser0, U.TotalConsoleTime0,
(
SELECT TotalUserConsoleMinutes0 FROM v_GS_SYSTEM_CONSOLE_USER
WHERE ResourceID = S.ResourceID AND SystemConsoleUser0 = U.TopConsoleUser0
) AS TopConsoleUserMinutes0
FROM v_GS_SYSTEM_CONSOLE_USAGE U
INNER JOIN v_R_System S ON U.ResourceID = S.ResourceID
5.5. QUERYING MULTIPLE TABLES
6.1. GROUPING DATA + AGGREGATES
GROUP BY
Aggregates
HAVING
Groups the result into groups of rows, usually for the purpose of
performing one or more aggregations
AVG([ALL|DISTINCT] x) Average of all/distinct values
COUNT(*) Number of rows
COUNT([ALL|DISTINCT] x) Number of all or distinct values
MAX(x) Maximum value
MIN(x) Minimum value
SUM([ALL|DISTINCT] x) Sum of all/distinct values
STDEV(x) / STDEVP(x) Standard deviation (for population)
VAR(x) / VARP(x) Variance (for population)
Specifies a search condition for a group or an aggregate
6.1. GROUPING DATA + AGGREGATES
SELECT AVG(TotalVisibleMemorySize0) FROM v_GS_OPERATING_SYSTEM
SELECT MIN(TotalVisibleMemorySize0) FROM v_GS_OPERATING_SYSTEM
SELECT MAX(TotalVisibleMemorySize0) FROM v_GS_OPERATING_SYSTEM
SELECT SUM(TotalVisibleMemorySize0)/1024 AS [Total Memory in GB]
FROM v_GS_OPERATING_SYSTEM
SELECT AD_Site_Name0, COUNT(*) FROM v_R_System GROUP BY AD_Site_Name0
SELECT AD_Site_Name0, COUNT(*) FROM v_R_System GROUP BY AD_Site_Name0
HAVING COUNT(*) > 10
SELECT AD_Site_Name0, AVG(TotalVisibleMemorySize0) FROM v_R_System S
INNER JOIN v_GS_OPERATING_SYSTEM O ON S.ResourceID = O.ResourceID
GROUP BY AD_Site_Name0
6.2. COMMON TABLE EXPRESSIONS
CTE
Loops, Nested Queries, Parent/Child
Temp tables
A recursive query??
Use instead of a view or stored procedure
when the resultant set of data is not
required to be stored outside the process
6.3. WORKING WITH CUSTOM DATABASE OBJECTS
Linked servers
Databases
Tables
Views
Stored Procedures
User-defined functions
HIGH LEVEL DATA FLOW
Feeds Data To
Client Machine
WMI
Client Machine
WMI
Client Machine
WMI
SQL Tables
System_DISC
SoftwareFile
CollectionMembers
SQL Views
v_R_System
v_GS_SoftwareFile
_RES_COLL_<Coll ID>
Updates
Gets Data From
SCCM Database
WMI = SQL VIEWS
ConfigMgr collects WMI data into SQL tables and exposes them to the operator via SQL
tables and views
SQL Server view schema maps to the WMI & SMS Provider class schemas
ConfigMgr object types are WMI classes
ConfigMgr attributes are WMI properties
Although the ConfigMgr Table/View schema is not published, the WMI Class schema is
Handy because of the close WMI/View mapping
WMI = SQL VIEWS
Some examples
ConfigMgr Console Network Adapter
WMI Class Win32_NetworkAdapter
SMS Provider Class SMS_G_System_Network_Adapter
SQL View v_GS_NETWORK_ADAPTER
ConfigMgr Console Computer System
WMI Class Win32_ComputerSystem
SMS Provider Class SMS_G_System_Computer_System
SQL View v_GS_COMPUTER_SYSTEM
WMI = SQL VIEWS
Generally, there are three rules for converting an SMS Provider Class to a SQL View
Beginning of view name changed fromSMS_ to v_
Object names > 30 characters truncated
SQL View Column name = WMI property name
Though, these rules are not always followed.
Important: Not all WMI properties natively collected by SMS.
Extend collected properties by modifying hardware inventory (.mof files)