I have a following data :
Row Column 1 Column 2 batch_date
1 Account 1 zipcode 1 11/28/2020
2 Account 1 zipcode 1 11/29/2020
3 Account 1 zipcode 1 11/30/2020
4 Account 1 zipcode 2 12/1/2020
5 Account 1 zipcode 2 12/2/2020
6 Account 1 zipcode 2 12/3/2020
7 Account 1 zipcode 2 12/4/2020
8 Account 1 zipcode 2 12/5/2020
9 Account 1 zipcode 2 12/6/2020
10 Account 1 zipcode 2 12/7/2020
11 Account 1 zipcode 2 12/8/2020
12 Account 1 zipcode 2 12/9/2020
13 Account 1 zipcode 2 12/10/2020
14 Account 1 zipcode 3 12/11/2020
15 Account 1 zipcode 3 12/12/2020
I would like to fetch data for this account for dates when the column2 (zipcode) has been changed.
Output should be like below:
Row Column 1 Column 2 batch_date
1 Account 1 zipcode 1 11/28/2020
2 Account 1 zipcode 2 12/1/2020
3 Account 1 zipcode 3 12/11/2020
How can we do it in bigquery ?
I have already tried FIRST_VALUE() function but the query is resulting into "resources issue".
I also tried self join but that is not giving the desired output.
Can anybody help on this ?
Below is for BigQuery Standard SQL
#standardsql
select * except(changed) from (
select *, column_2 != ifnull(lag(column_2) over win, '') changed
from `project.dataset.table`
window win as (partition by column_1 order by parse_date('%m/%d/%Y', batch_date) asc)
)
where changed
If to apply to sample data from your question - output is
Note: above code assumes your batch_date column is of STRING data type - thus use of parse_date function. If this column is of DATE data type - you don't need this function and can use just batch_date instead of parse_date('%m/%d/%Y', batch_date)
I managed to do it with the help of navigation functions in BigQuery:
SELECT DISTINCT Row, c1, c2, FIRST_VALUE(batch_date)
OVER (PARTITION BY c2 ORDER BY batch_date ASC) AS batch_date
FROM table;
I replaced "Column 1" with "c1", "Column 2" with "c2" for purpose of the example.
Related
Goal:
If a person has two candy number, number 1 should always display first. No need to display candy number 2.
If a person does not have number 1, it should display number 2 instead.
Display all data
(int)(int) (nvarchar) (int)
Id fId Name Candy Number
---------------------------------
1 12 Kimn 1
2 12 Kimn 2
3 19 Lisa 1
4 15 John 2
5 16 Maria 2
6 16 Maria 1
7 17 Mao 2
Requested result:
Id fId Name Candy Number
---------------------------------
1 12 Kimn 1
3 19 Lisa 1
4 15 John 2
6 16 Maria 1
7 17 Mao 2
Problem:
It doesn't work so well for me to display it.
Tried using case and end in where statement but the code didn't fit to the purpose.
Any idea?
select *
from
table
where
candynumber =
CASE WHEN b.MatchType = 1
THEN 1
ELSE 2
END
Thank you!
This can be using row_number() window function:
select Id, fId, Name, Candy_Number from (
select your_table.*, row_number() over(partition by fId order by Candy_Number) as rn from your_table
) t
where rn = 1
order by id
This gives one row per fId, with lower Candy_Number.
You can try this :
SELECT candyWrapper.ID,
candyWrapper.FID,
outerHardCandy.Name,
outerHardCandy.Number
FROM (SELECT innerSoftCandy.Name,
CASE
WHEN (SUM(innerSoftCandy.Number) = 3) OR (SUM(innerSoftCandy.Number) = 1) THEN 1
WHEN (SUM(innerSoftCandy.Number) = 2) THEN 2
END AS Number
FROM Candy innerSoftCandy
GROUP BY innerSoftCandy.Name
) outerHardCandy
INNER JOIN Candy candyWrapper ON (outerHardCandy.Name = candyWrapper.Name AND outerHardCandy.Number = candyWrapper.Number)
ORDER BY candyWrapper.ID
You can see this here -> http://rextester.com/BBD89608
This is my table and data
id | owner | buyer
1 1 3
2 2 2
3 1 2
I want the result to be like this
user | totals
2 3
1 2
3 1
User field means owner and buyer.
Hope you all are understand.
Thanks ~
You can do this using union all and group by:
select user, count(*)
from ((select owner as user from t
) union all
(select buyer from t
)
) ob
group by user
order by user;
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)
I have one table like below format:
ID Date Frequency
1 01/06/2009 1
2 01/06/2009 1
3 01/06/2009 2
4 01/06/2009 1 *
5 01/06/2009 1
6 01/06/2009 2 *
I want result set like below:
Current : 6 01/06/2009 2
Previous : 4 01/06/2009 1
I am using SQL Server 2000 databse.Please provide solution for above resultset.
This is a pretty basic SQL statement
SELECT count(ID), date, Frequency
FROM yourTable
Group By Date, Frequency
ORDER BY Frequency DESC
But I'm not positve due to * and the current: Previous: statements
I am trying to wrap my head around a problem I hit exporting data from one system to another.
Let's say I have a table like:
id | item_num
1 1
2 1
3 2
4 3
5 3
6 3
I need to add a column to the table and update it to contain an incrementing product_num field based on item. This would be the end result given the above table.
id | item_num | product_num
1 1 1
2 1 2
3 2 1
4 3 1
5 3 2
6 3 3
Any ideas on going about this?
Edit: This is being done in Access 2010 from one system to another (sql server source, custom/unknown ODBC driven destination)
Perhaps you could create a view in your SQL Server database and then select from that in Access to insert into your destination.
Possible solutions in SQL Server:
-- Use row_number() to get product_num in SQL Server 2005+:
select id
, item_num
, row_number() over (partition by item_num order by id) as product_num
from MyTable;
-- Use a correlated subquery to get product_num in many databases:
select t.id
, t.item_num
, (select count(*) from MyTable where item_num = t.item_num and id <= t.id) as product_num
from MyTable t;
Same result:
id item_num product_num
----------- ----------- --------------------
1 1 1
2 1 2
3 2 1
4 3 1
5 3 2
6 3 3