0% found this document useful (0 votes)
392 views

Other Functions

1) The MAX() and TOP functions can be used to retrieve the maximum or top values from a column. MAX() returns the top value based on grouping while TOP returns the top number of rows. 2) Aggregate functions like COUNT can identify duplicates by counting grouped values. The ROW_NUMBER function adds a sequential number to rows within partitions to identify duplicates. 3) Date functions like GETDATE(), DATEADD, and DATEDIFF allow transforming and calculating differences between dates. DATEADD increments dates while DATEDIFF calculates time differences between dates based on date parts.

Uploaded by

Joel Lindstrom
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
392 views

Other Functions

1) The MAX() and TOP functions can be used to retrieve the maximum or top values from a column. MAX() returns the top value based on grouping while TOP returns the top number of rows. 2) Aggregate functions like COUNT can identify duplicates by counting grouped values. The ROW_NUMBER function adds a sequential number to rows within partitions to identify duplicates. 3) Date functions like GETDATE(), DATEADD, and DATEDIFF allow transforming and calculating differences between dates. DATEADD increments dates while DATEDIFF calculates time differences between dates based on date parts.

Uploaded by

Joel Lindstrom
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MAX() vs Top

Use Select max() to retrieve the top value in a column based on a specific grouping
select max(createdon) as created, regardingobjectidname from filteredactivitypointer with (nolock) group by regardingobjectidname Use Select top to retrieve the top x number or percent of rows in a table select top 10 name, accountid, address1_city,revenue from filteredaccount with (nolock) order by revenue desc

Top returns data based on the order of the columns. If you want to get the bottom X rows, change the order from descending (DESC) to Ascending (ASC)

Using Aggregate Functions to Assess Data Quality


You can use aggregate functions such as count to identify duplicates in your database. For example, the following query will identify how many e-mail addresses are duplicated in your CRM contacts:
select count(*), emailaddress1 from filteredcontact with (nolock) group by emailaddress1 having count(*) >1 For a greater level of detail on duplicate records, use the row_number function select contactid, emailaddress1, row_number() over(partition by emailaddress1 order by createdon asc) as rownum from filteredcontact with (nolock) The row_number function will insert a row number in your result set. The partition by indicates which column should be used to group the results. At each new value in the partition column the row count will reset to 1. Order by indicates the column and sort order that should be used to order the rows within the group. Next, embed your row_number query in a derived query). By putting the original query in parenthesis and wrapping it inside of another query, we can filter the results to only records where the row count is greater than 1.

select * from (select contactid, emailaddress1, row_number() over(partition by emailaddress1 order by createdon asc) as rownum from filteredcontact with (nolock)) as der1 where rownum >1 When we execute this query we will be left with the duplicated instances of records, excluding the originally created versions. We can use this as the basis of a Scribe job that deletes duplicate records.

Working With Dates


When working with dates, the following functions are helpful for basic date value transformation: 1. Getdate() This function returns the current date. The following example returns accounts created before today.
select * from filteredaccount with (nolock) where createdon < getdate()

2. Dateadd This function increments or decrements a date value by a specified datepart. Available dateparts
Datepart

Year Quarter Month Day Week Weekday Hour Minute

Second Millisecond Microsecond Nanosecond


The syntax for the dateadd formula is dateadd(datepart, quantity, date expression) Date expression can be a date field reference (such as createdon), getdate(), or a hard-coded date value in single quotes (1/8/1975) This example filters the list of opportunities to those estimated to close after today and less than six months from now
select name, accountidname, estimatedvalue from filteredopportunity with (nolock) where estimatedclosedate > getdate() and estimatedclosedate < dateadd(month,6,getdate()) This example calculates a date five weeks from the date a case was created and a date five weeks prior to when the case was created. select title, createdon, dateadd(week,5,createdon)as futuredate, dateadd(week, -5, createdon) as pastdate from filteredincident with (nolock)

3.

Datediff This function calculates the time between two dates based on a specified datepart. It uses the same dateparts as dateadd. The syntax for the datediff formula is datediff(datepart, firstdate, lastdate) Date expression can be a date field reference (such as createdon), getdate(), or a hard-coded date value in single quotes (1/8/1975) This example show using datediff to calculate how many days until Christmas
select datediff(day,getdate(),'12/25/2012')

This example shows using datediff to calculate how long a case record has been open, sorted from longest to shortest. This is a common case aging report scenario.
select title, datediff(week,createdon, getdate()) as daysopen from filteredincident with (nolock) order by daysopen desc

Building Strings
To concatenate separate columns into one column, you can build a string by separating text values in your query with a +. In the following example, the address is concatenated into a string with spaces between the values.
select name, address1_line1 + ' ' + address1_city + ' ' + address1_stateorprovince + ' ' + address1_postalcode as fulladdress from filteredaccount with (nolock) If you separate two numerical type fields with a + sign, the query will add them together.

You might also like