Académique Documents
Professionnel Documents
Culture Documents
About Me
Melissa is a Business Intelligence
Melissa Coates
Blog:
http://www.sqlchick.com
Twitter: @sqlchick
About Mariner
http://www.mariner-usa.com
Topic
Time
Bill Pearson
9:45 am
Bill Pearson
1:15 pm
Melissa Coates
1:15 pm
Wayne Snyder
2:30 pm
Javier Guillen
3:45 pm
Agenda
Overview: Terms & Types of Functions
Compare several functions between T-SQL and SSRS
Syntax
Report impact
Differences in functionality
Differences in results returned
Scope of Discussion
Source query: SQL Server relational data sources
Examples
Most examples use AdventureWorksDW2008R2
A few use AdventureWorks2008R2
Concerns of a
Report Developer
Performance
Maintenance
Reusability
Standardization
Security
Testing
Deployment
Documentation
....etc
Overview:
Terms & Types of Functions
Function
A portion of code which performs a specific task
In other languages, called a procedure, method, or subroutine
Functions can be built-in, or user-defined
In T-SQL, functions are usually used to query the data, or perform operations on data
MSDN:
Function
SSMS
query:
SSMS
object
explorer:
(contd)
Expression
In T-SQL, expressions are:
A combination of symbols and
operators which are evaluated
to return a single data value.
**In T-SQL: the expression is
evaluated individually for each row in
the result set.**
An expression may or may not
contain a function.
Simple expressions can be a
single constant, variable,
column, or scalar function.
Operator symbols can be used
to join 2 or more simple
expressions into a complex
expression.
Description
Examples
Rowset
OpenRowset, OpenQuery
Ranking
Aggregate
Scalar
Description
Examples
Aggregate
Text
Operates on a textual
(string) value
Misc
Miscellaneous
Financial
Math
Inspection
Conversion
Cheat Sheet:
Comparisons Between T-SQL Functions
and SSRS Functions
Cheat Sheet
Available at:
http://www.sqlchick.com Presentations page
Part I:
Aggregate
Functions
Running Value
Average
Count
Calculating
Running Values
SSRS:
No built-in function.
Syntax:
=RunningValue(expression, function, scope)
Example:
=RunningValue(Fields!SalesAmount.Value, Sum,
"ResellerSalesByYear")
Report body:
Report results:
Report body:
Report results:
Running Value
T-SQL:
Execution Log:
SSRS:
Execution Log:
SSRS
Average
SSRS:
SELECT AVG(Amount) or
SELECT AVG(ALL Amount)
=Avg(Fields!Amount.Value)
SSRS:
Not supported
Average: T-SQL
Dataset query:
Report body:
Report results:
Average: SSRS
Report body:
Dataset query:
Report results:
Without
doing
anything
special, we
see the
decimals
when the
averaging
occurs in
SSRS.
Averaged in T-SQL
Averaged in
SSRS: results in
an average of
an average
which returns a
very different
result than if all
detail rows were
averaged.
Incorrect
(was calculated from average)
Correct
(was calculated from detail)
Average
Usually we want to average in the source query to
avoid bringing back extra data we dont need in the
report.
T-SQL
SSRS
Counts
SSRS:
=Count(Fields!Size.Value)
SSRS:
SELECT COUNT(*)
=CountRows()
T-SQL:
SSRS:
=CountDistinct(Fields!Size.Value)
Count: T-SQL
Dataset query:
Report body:
Report results:
Count: SSRS
Dataset query:
Report body:
Report results:
Count
T-SQL:
Profiler:
Execution Log:
SSRS:
Profiler:
Execution Log:
T-SQL
If you need the detail in the report anyway (ex: a drilldown) then counting in the report is okay to do.
SSRS
Part II:
Text & Formatting Functions
Return part of string
Trim trailing spaces
String comparisons
Formatting
SSRS:
SUBSTRING(SalesOrderNumber, 3,5)
=Mid(Fields!SalesOrderNumber.Value, 3,5)
LEFT(SalesOrderNumber, 2)
=Left(Fields!SalesOrderNumber.Value, 2)
RIGHT(SalesOrderNumber, 5)
=Right(Fields!SalesOrderNumber.Value, 5)
SSRS:
LTRIM(SalesOrderNumber)
=LTrim(Fields!SalesOrderNumber.Value)
RTRIM(SalesOrderNumber)
=RTrim(Fields!SalesOrderNumber.Value)
RTRIM(LTRIM(SalesOrderNumber))
=Trim(Fields!SalesOrderNumber.Value)
T-SQL:
SSRS:
SELECT SOUNDEX(
EnglishCountryRegionName)
=StrComp(
Fields!EnglishCountryRegionName.Value,
Fields!SpanishCountryRegionName.Value, 1)
SELECT DIFFERENCE(
EnglishCountryRegionName,
SpanishCountryRegionName)
Results: 0: same
1: string 1 > string 2
-1: string 1 < string 2
SSRS:
CAST(SalesAmount AS Dec(8,2))
CONVERT(DEC(8,2),SalesAmount)
=Format(Fields!SalesAmount.Value, "F2")
=Format(Fields!SalesAmount.Value, "##.##")
SSRS:
=FormatNumber(Fields!SalesAmount.Value, 2)
=Format(Fields!SalesAmount.Value, "#,#.##")
SSRS:
=FormatCurrency(Fields!SalesAmount.Value, 2)
=Format(Fields!SalesAmount.Value, "C2")
SSRS:
=FormatPercent(Fields!SalesPct.Value)
DateTime:
SSRS:
SSRS
Part III:
DateTime Functions
DATEPART
DATEADD
Current Date and Time
SSRS:
SELECT DATEPART(YY,ModifiedDate)
=DatePart(DateInterval.Year,
Fields!ModifiedDate.Value)
SELECT DATEADD(DAY,2,OrderDate)
=DateAdd(DateInterval.Day, 2,
Fields!ModifiedDate.Value)
Year
Quarter
Month
Week
Day
Hour
Minute
Second
Millisecond
Microsecond
Nanosecond
SSRS: precision
down to the second
T-SQL: more precise
T-SQL Function
Returns
SELECT GETDATE()
2011-02-22 19:10:47.757
2011-02-22 19:10:47.7582404
SSRS Function
Returns
=Now()
2/22/2011 7:10:47 PM
=Globals!ExecutionTime
2/22/2011 7:10:47 PM
=Today()
2/22/2011 12:00:00AM
=DateString()
02-22-2011
=TimeString()
19:10:47
DateTime Functions
T-SQL
SSRS
Part IV:
Program Flow
Functions
CASE
IIF
Switch
Decoding Values
Decoding Values
Evaluates a list of conditions and returns one of multiple possible result expressions:
T-SQL:
SSRS:
=IIF(Fields!ProductLine.Value = "R","Road",
IIF(Fields!ProductLine.Value = "M","Mountain",
IIF(Fields!ProductLine.Value = "T","Touring",
IIF(Fields!ProductLine.Value = "S","Other",
Not For Sale"))))
SSRS:
Searched CASE:
CASE WHEN ProductLine = 'R' THEN 'Road'
WHEN ProductLine = 'M' THEN 'Mountain'
WHEN ProductLine = 'T' THEN 'Touring'
WHEN ProductLine = 'S' THEN 'Other'
ELSE 'Not for sale'
END AS Category
=Switch(
TRIM(Fields!ProductLine.Value) = "R","Road",
TRIM(Fields!ProductLine.Value) = "M","Mountain",
TRIM(Fields!ProductLine.Value) = "T","Touring",
TRIM(Fields!ProductLine.Value) = "S","Other",
IsNothing(Fields!ProductLine.Value),Not For Sale"
)
Decoding Values
T-SQL
SSRS
Part V:
Miscellaneous
Functions
Ranking
Row Number
Previous
Percent of Total
Scoping
Scoping in SSRS
Specifies what data to include in aggregate calculations.
Scoping in T-SQL
Specifies what field(s) to group by in aggregate calculations.
Scoping
T-SQL
SSRS
SSRS:
Not supported
Rank of each row within the partition, without any gaps in numbering:
T-SQL:
SSRS:
Not supported
SSRS:
Not supported
SSRS:
=RowNumber(Dset_YTDSales)
or
=RowNumber(Group_PostalCode)
RowNumber: SSRS
Report body:
Dataset query:
Report results:
Ranking
T-SQL
SSRS
Percent of Total
SSRS:
No built-in function.
Example:
=FormatPercent(
Sum(Fields!SalesAmount.Value)
/
Sum(Fields!SalesAmount.Value,"Tablix1")
,1)
scope
Report body:
Report results:
Report body:
Report results:
Percent of Total
T-SQL:
Execution Log:
SSRS:
Execution Log:
SSRS
Previous Row
SSRS:
No built-in function.
=Previous(Fields!SalesAmount.Value)
Report body:
Report results:
Report body:
Report results:
Previous Row
T-SQL:
Execution Log:
SSRS:
Execution Log:
SSRS
Conclusion
Recap
Aggregate Function
T-SQL
SSRS
Why?
Running Value
Built-in to SSRS
Depends on level of
detail
Text Function
T-SQL
SSRS
Why?
Same functionality
String comparisons
Formatting
Recap
(contd)
DateTime Function
T-SQL
SSRS
Why?
More precision
Current Date
Program Flow
Function
T-SQL
SSRS
Decoding Values
(CASE vs IIF vs Switch)
Misc. Function
Why?
Centralize logic
T-SQL
SSRS
Why?
Percent of Total
Previous Row
Built-in to SSRS
Questions &
Suggestions
References
Built-In Functions (Transact-SQL):
http://msdn.microsoft.com/en-us/library/ms174318.aspx
Expressions (Transaction-SQL):
http://msdn.microsoft.com/en-us/library/ms190286.aspx
Understanding Expression Scope for Totals, Aggregates, and Built-In Collections (SSRS):
http://technet.microsoft.com/en-us/library/dd255256.aspx
To register:
https://www.clicktoattend.com/
invitation.aspx?code=154140
or
http://www.mariner-usa.com
Link to the Event will be at
bottom right of home page