Skip to content

Tag: sql

SQL CASE that matches value or uses LIKE ‘%’

I’m trying to do a wildcard retrieve if a defined variable is blank. But I can’t figure out the correct syntax. Answer It is as simple as this: The expression NULLIF(x, ”) will convert ” to null. Then we take advantage of how null comparison works: If @customerID is not null then first…

Pivot and Sum in Amazon Redshift

I have a following tables table1 table2 first I’d like to join table2 as follows and then join table2 with id and pivot and sum up them I guess it is very complicated, are there any way to achieve this? In Redshift it seems that there is no way to pivot them. Thanks. Answer This looks to be a pivot

Ensuring no dupe ids in query return

So for the following schema: You can see that there are 3 activities and each contact has two. What i am searching for is the number of activities per account in the previous two months. So I have the following query This returns: However this is incorrect. There are only 3 activities, its just that each cont…

Omit rows where multiple columns contain NULL

I’m trying to figure out how to build a query that omits rows where both columns of interest are NULL. If only one or the other is NULL, then I would like to include that data. In this example table, I would like to have a query that includes everything except row 3. My thought would be to build a

JSON Blob into SQL Server

I have data from a web API that I want to store in a SQL database. Each record in the web API looks like this: Answer That query is invalid. Should be of the form INSERT INTO TableName(Column1, Column2) VALUES (?,?), so something like: