Other Functions
Other Functions
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)
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.
2. Dateadd This function increments or decrements a date value by a specified datepart. Available dateparts
Datepart
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.