SQL Sorted Count - sql

I have the following table sorted by date:
date
id
9/1/20
1
9/1/20
2
9/3/20
1
9/4/20
3
9/4/20
2
9/6/20
1
I'd like to add a count column for each id so that the first count for each id is the earliest date and latest date would receive the highest count for each id:
date
id
count
9/1/20
1
1
9/1/20
2
1
9/3/20
1
2
9/4/20
3
1
9/4/20
2
2
9/6/20
1
3
How can I structure my Postgresql query to assemble this count column?

This looks like row_number():
select t.*,
row_number() over (partition by id order by date) as seqnum
from t
order by date, id;

Related

How to select row based on max column and latest in postgresql

Here is my table in PostgreSQL:
id
name
description
account_id
total_sales
create_at
1
Playstation 4
Console Game
1
21
2021-03-26
2
Playstation 2
Console Game
1
21
2021-03-27
3
Playstation 3
Console Game
1
20
2021-03-27
I would like to select row based on max(total_sales). If there are two rows with same total_sales, it will select the latest.
So, the result should be like this:
id
name
description
account_id
total_sales
create_at
2
Playstation 2
Console Game
1
21
2021-03-27
If there is only one row with max(total_sales) and there is no same value, it will return this row as the result.
select *
from your_table
order by toal_sales desc, created_at desc
limit 1

GBQ SQL: How to find first instance of X value and pull a corresponding row

I have a table that records the history of each ID per LOCATION. This table is updated each day to keep track of the history of any change in a certain row(ID). Note: The date field is not in chronological order.
ID Count Date (datetime type)
1 20 2020-01-15T12:00:00.000
1 16 2020-03-15T12:00:00.000
1 13 2020-04-15T12:00:00.000
1 4 2020-05-15T12:00:00.000
1 0 2020-06-15T12:00:00.000
2 20 2020-01-15T12:00:00.000
2 10 2020-02-15T12:00:00.000
3 12 2020-01-15T12:00:00.000
3 10 2020-02-15T12:00:00.000
3 0 2020-03-15T12:00:00.000
For each unique ID, I need to pull the first instance (oldest date) when the Count value is zero. If a unique ID does not have an instance where it Count value is zero, I need to pull the most current Count value.
Here's what my results should look like below:
ID Count Date (datetime type)
1 0 2020-06-15T12:00:00.000
2 10 2020-02-15T12:00:00.000
3 0 2020-03-15T12:00:00.000
I can't seem to wrap my head around how to code this in Google BigQuery.
Below is for BigQuery Standard SQL
#standardSQL
SELECT AS VALUE
CASE COUNTIF(count = 0)
WHEN 0 THEN ARRAY_AGG(t ORDER BY date DESC LIMIT 1)[OFFSET(0)]
ELSE ARRAY_AGG(t ORDER BY count, date LIMIT 1)[OFFSET(0)]
END
FROM `project.dataset.table` t
GROUP BY id
if to apply to sample data in your question - output is
Row id count date
1 1 0 2020-05-15 12:00:00 UTC
2 2 10 2020-03-15 12:00:00 UTC
3 3 0 2020-06-15 12:00:00 UTC
Do you just want the last row for each id?
One method is row_number():
select t.*
from (select t.*,
row_number() over (partition by id
order by case when count = 0 then date end nulls last,
date desc
) as seqnum
from t
) t
where seqnum = 1;
But I also like using aggregation in BigQuery:
select (array_agg(t order by date desc limit 1))[ordinal(1)]
from t
group by id;

How to get MAX Hike in Min month?

below is table:
Name | Hike% | Month
------------------------
A 7 1
A 6 2
A 8 3
b 4 1
b 7 2
b 7 3
Result should be:
Name | Hike% | Month
------------------------
A 8 3
b 7 2
Here is one way of doing this:
SELECT Name, [Hike%], Month
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [Hike%] DESC, Month) rn
FROM yourTable
) t
WHERE rn = 1
ORDER BY Name;
If you instead want to return multiple records per name, in the case where two or more records might be tied for having the greatest hike%, then replace ROW_NUMBER with RANK.
use correlated subquery
select Name,min(Hike) as Hike,min(Month) as Month
from
(
select * from tablename a
where Hike in (select max(Hike) from tablename b where a.name=b.name)
)A group by Name
You can use something similar to the below:
SELECT Name, MAX(Hike), Month
FROM table
GROUP BY Name, Month
Hope this helps :)

Select Query to Get Unique Cells in Two Columns

I have an SQL Server database, that logs weather device sensor data.
The table looks like this:
Id DeviceId SensorId Value
1 1 1 42
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
6 3 1 26
7 3 1 23
8 3 2 1
In return the query should return the following:
Id DeviceId SensorId Value
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
7 3 1 23
8 3 2 1
For each device the sensor should be unique. i.e. Values in Columns DeviceId and SensorId should be unique (row-wise).
Apologies if I'm not clear enough.
If you don't want to sum Value as your desired result suggest, so you just want to take an "arbitrary" row of each "DeviceId + SensorId"-group:
WITH CTE AS
(
SELECT Id, DeviceId, SensorId, Value,
RN = ROW_NUMBER() OVER (PARTITION BY DeviceId, SensorId ORDER BY ID DESC)
FROM dbo.TableName
)
SELECT Id, DeviceId, SensorId, Value
FROM CTE
WHERE RN = 1
ORDER BY ID
This returns the row with the highest ID per group. You need to change ORDER BY ID DESC if you want a different result. Demo: http://sqlfiddle.com/#!6/8e31b/2/0 (your result)

Find the Product Number

I am trying to find the product number/product name based on the following set of conditions:
Select top 1 productnumber in xyz table
where product number in (1,2,3) order by filingdate --only if the last filing date has this product number
If product number in (4,5,6) for the last filing date nothing should be selected
If product number not in (4,5,6) for the last filing, then select the next top 1 productnumber
where prodcutnumber in (1,2,3) order by filingdate
how can i achieve this in a single query, i tried case statement bu i am stuck with it.
Sample data:
pnumber fnumber fdate
1 1 12/31
2 1 12/10
1 2 12/10
4 2 12/11
5 2 12/12
7 3 12/12
1 3 12/11
the results should be
pnumber fnumber fdate
1 1 12/31
1 2 12/10
1 3 12/11
Try by giving datatype as varchar for fdate.
SELECT id,
funum,
fdate
FROM (SELECT Row_number()
OVER(
partition BY funum
ORDER BY id) rn,
*
FROM t)p
WHERE rn = 1