Case when first instance of unique ID - snowflake-cloud-data-platform

I'm in Snowflake and am trying to mark the first occurrence of a unique ID in a column. I've been playing around with first_value but am not really getting anywhere.
So my data looks something like this:
ID Date
123 1/2019
123 2/2019
123 3/2019
234 2/2019
234 3/2019
And ideally I want something like this:
ID Date First?
123 1/2019 1
123 2/2019 0
123 3/2019 0
234 2/2019 1
234 3/2019 0
How do I accomplish this?

You want ROW_NUMBER:
SELECT
ID,
Date,
IFF(ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date) = 1, 1, 0) AS First
FROM
schema.table
ORDER BY ID, Date
;
This checks whether the current row is the first date for the ID, and if it is, assigns a value of 1 (otherwise 0).

LAG can also be used to solve this..
SELECT id
,date
,lag(id) over (partition by id order by date) is null as first
FROM table_name;
Which can be also done with FIRST_VALUE like
SELECT id
,date
,first_value(id) over (partition by id order by date) = date as first
FROM table_name;

If your intention is to retrieve the first occurrence of a unique ID in a column then the row_number() or the dense_rank() function can help.
with cte as
(
select ID, Date,
row_number() over (partition by ID order by date) as row_number
from table1
)
select * from cte where row_number = 1;
with cte as
(
select ID, Date,
dense_rank() over (partition by ID order by date) as rank
from stack1
)
select * from cte where rank = 1;

Related

UNION Returns NULL When First SELECT Returns Nothing

Hi I have a table: T1 that contains two columns Date and Price
T1
---------------------------
DATE | PRICE |
---------------------------
2018-07-25 |2.00 |
---------------------------
2018-06-20 |3.00 |
---------------------------
2017-05-10 |3.00 |
---------------------------
Here are my requirements:
If a user enters a date that is not in the DB I need to return the last price and date in the table T1.
If a user enters a date that is superior or inferior to one of the dates in the table T1 -- for example if a user enters '2017-05-09' which is not in the table; I have to return the next date above the given date. In this case'2017-05-10'
I am using UNION in my script but it returns empty when one of the SELECT statements returns empty.
I am using a CTE table:
DECLARE #DateEntered DATE
WITH HistoricalCTE (Date, Price, RowNumber) AS (
SELECT R.Date,
R.Price,
ROW_NUMBER() OVER (PARTITION BY R.Date, R.Price ORDER BY Date DESC)
FROM T1 R
WHERE Date = #DateEntered
UNION
SELECT R.Date,
R.Price,
ROW_NUMBER() OVER (PARTITION BY R.Date, R.Price ORDER BY Date DESC)
FROM T1 R
WHERE Date < #DateEntered
UNION
SELECT R.Date,
R.Price,
ROW_NUMBER() OVER (PARTITION BY R.Date, R.Price ORDER BY Date DESC)
FROM T1 R
WHERE Date > #DateEntered
)
The issue is when I enter superior to all the dates in the table T1, I get an empty result because the first select is returning empty. Any idea about how I would solve this?
You might be overcomplicating this. If I read your question correctly, we can just take the smallest value greater than the input, or if that doesn't exist, then just take the max of the table.
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY Date) rn
FROM T1
WHERE Date > #DateEntered
)
SELECT
CASE WHEN EXISTS (SELECT 1 FROM cte WHERE rn = 1)
THEN (SELECT Date FROM cte WHERE rn = 1)
ELSE (SELECT MAX(Date) FROM T1) END AS Date,
CASE WHEN EXISTS (SELECT 1 FROM cte WHERE rn = 1)
THEN (SELECT Price FROM cte WHERE rn = 1)
ELSE (SELECT Price FROM T1 WHERE Date = (SELECT MAX(Date) FROM T1)) END AS Price;
Demo
All the edge cases seem to be working in the above demo, and you may test any input date against your sample data.

how to select rows when minute value change

I have a table named
Ship(Date datetime,name varchar(50),Type char(1)).
In table Ship The "Date" Column is of datetime datatype. I want to select rows from ship table when minute value in Date column (of Datetime datatype) changes. For this i used the following query:
;WITH x AS
(
SELECT Name, Date,Type, rn = ROW_NUMBER() OVER
(PARTITION BY Date ORDER BY Date desc)
FROM Ship
)
SELECT * FROM x WHERE rn = 1
But the desired output is not coming. The Result coming is:
Date Name Type
2017-05-08 14:59:13.000 sumit A
2017-05-08 14:59:23.000 sumit B
2017-05-08 14:59:33.000 sumit A
2017-05-08 15:00:05.000 Ajay B
2017-05-08 15:00:13.000 Deep G
2017-05-08 15:01:03.000 Suri D
2017-05-08 15:01:13.000 Faiz E
Here in above output those rows are also coming when there is a change in second value of Date column. But i want to select rows when there is change in minute value of Date Column.Can anyone solve this?
You could use datediff minute on partition by clause
;WITH x AS
(
SELECT Name, Date,Type, rn = ROW_NUMBER() OVER
(PARTITION BY datediff(min,0,[Date]) ORDER BY Date desc)
FROM Ship
)
SELECT * FROM x WHERE rn = 1
Or it is shorter version
SELECT TOP 1 WITH TIES
Name, Date,Type
FROM Ship
ORDER BY ROW_NUMBER() OVER (PARTITION BY datediff(min,0,[Date]) ORDER BY [date] desc)
Can you add the DATEPART in the PARTITION BY to get the change in the minutes section only.
;WITH x AS
(
SELECT Name, Date,Type, rn = ROW_NUMBER() OVER
(PARTITION BY DATEPART(N, Date) ORDER BY Date desc)
FROM Ship
)
SELECT * FROM x WHERE rn = 1
Refer the Demo: http://rextester.com/YZW84894

SQL Server: selecting distinct values per one column

I was wandering if it's possible to filter select results removing values that partially overlap
For example below, i have thousands of records, but i need the 'week date' value to be unqiue, and in case of duplicates the one with the highest value should remain.
emplo project_id Value week_Date week_ActualStart week_ActualEnd
A0001 project001 100 2015-12-28 2015-12-28 2016-01-03
A0001 project001 60 2015-12-28 2016-01-01 2016-01-03
So only the first row should remain.
I could really use someone's advice
Try something like the following:
;WITH WeekDateCte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY emplno, week_Date ORDER BY Value DESC) RowNo
FROM employee
)
SELECT *
FROM WeekDateCte
WHERE RowNo = 1
For more information about ROW_NUMBER function, check here.
NOTE: ROW_NUMBER() returns BIGINT.
You can use ROW_NUMBER for this:
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd
FROM (
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd,
ROW_NUMBER() OVER (PARTITION BY emplno, week_Date
ORDER BY Value DESC) AS rn
FROM mytable) AS t
WHERE t.rn = 1
The query picks the row having the greatest Value per emplno, week_Date slice.

SQL Server query should return max value records

I have table like this:
id_Seq_No emp_name Current_Property_value
-----------------------------------------------
1 John 100
2 Peter 200
3 Pollard 50
4 John 500
I want the max record value of particular employee.
For example, John has 2 records seq_no 1, 4. I want 4th seq_no Current_Property_Value in single query.
Select
max(id_Seq_No)
from
t1
where
emp_name = 'John'
To get the Current_Property_value, just order the results by id_Seq_No and get the first one:
SELECT
TOP 1 Current_Property_value
FROM
table
WHERE
emp_name = 'John'
ORDER BY
id_Seq_No DESC
this will give highest for all tied employees
select top 1 with ties
id_Seq_No,emp_name,Current_Property_value
from
table
order by
row_number() over (partition by emp_name order by Current_Property_value desc)
You can use ROW_NUMBER with CTE.
Query
;WITH CTE AS(
SELECT rn = ROW_NUMBER() OVER(
PARTITION BY emp_name
ORDER BY id_Seq_No DESC
), *
FROM your_table_name
WHERE emp_name = 'John'
)
SELECT * FROM CTE
WHERE rn = 1;

How to select last id(where id >0) fod each group in SQL Server 2008

I want to select last record of price column of my table where my value of column is greater than zero. How can I do that?
My stored procedure is :
SELECT
id, name, price
FROM
messages
WHERE
id IN (SELECT MAX(id)
FROM messages
WHERE price > 0
GROUP BY name)
The problem is that this code select max id that price is greater than zero not last id. Means select id=2 and id=6
But in last id of group (frank) price is zero but this stored procedure select id=2 while I want stored procedure select only id =6
id name price
--------------
1 frank 1000
2 frank 500
3 frank 0
4 john 200
5 john 100
6 john 20
There are multiple ways to approach this. Following your method, though, you just need to move the price comparison to the outer query:
SELECT id, name, price
FROM messages
WHERE price > 0 AND
id IN (SELECT MAX(id)
FROM messages
GROUP BY name
);
I would be more inclined to write this as:
select m.*
from (select m.*,
row_number() over (partition by name order by id desc) as seqnum
from messages m
) m
where seqnum = 1 and price > 0;
with cte as (
select row_number() over(partition by id order by id desc) rn,
id,name,price
from messages
)
select id,name,price
from cte
where rn=1 and price>0
Something like this.

Resources