Biguqery Row Number Perioding based on specific values - sql

So i have a raw data like this.
| User_id | Month | Device
| 001 | 01 | A
| 001 | 02 | A
| 001 | 03 | B (Base)
| 001 | 04 | A
| 002 | 01 | C
| 002 | 02 | C
| 002 | 03 | B (Base)
| 002 | 04 | B (Base)
| 003 | 01 | A
| 003 | 02 | B (Base)
| 003 | 03 | A
| 003 | 04 | B (Base)
I want to create period that split by B (Base) Device. I thinking about using Row Number but breaks only if meet B or several B's.
The result i want will be like this
| User_id | Month | Device | rn
| 001 | 01 | A | 1
| 001 | 02 | A | 1
| 001 | 03 | B (Base)| 2
| 001 | 04 | A | 3
| 002 | 01 | C | 1
| 002 | 02 | C | 1
| 002 | 03 | B (Base)| 2
| 002 | 04 | B (Base)| 2
| 003 | 01 | A | 1
| 003 | 02 | B (Base)| 2
| 003 | 03 | A | 3
| 003 | 04 | B (Base)| 4
Is there any way we can populate rn like that?

You might consider below query.
SELECT * EXCEPT(flag), COUNTIF(flag) OVER w + 1 AS rn FROM (
SELECT *,
(LAG(Device) OVER w <> 'B' AND Device = 'B') OR
(LAG(Device) OVER w = 'B' AND Device <> 'B') AS flag
FROM sample_table
WINDOW w AS (PARTITION BY User_id ORDER BY Month)
) WINDOW w AS (PARTITION BY User_id ORDER BY Month)
ORDER BY User_id, Month;
+---------+-------+--------+----+
| User_id | Month | Device | rn |
+---------+-------+--------+----+
| 001 | 01 | A | 1 |
| 001 | 02 | A | 1 |
| 001 | 03 | B | 2 |
| 001 | 04 | A | 3 |
| 002 | 01 | C | 1 |
| 002 | 02 | C | 1 |
| 002 | 03 | B | 2 |
| 002 | 04 | B | 2 |
| 003 | 01 | A | 1 |
| 003 | 02 | B | 2 |
| 003 | 03 | A | 3 |
| 003 | 04 | B | 4 |
+---------+-------+--------+----+

Related

How many different Product has one Market SQL

I try to get an output where there are the Market_id and the number of different Product_id of the market
Table1
| Market_id | Product_id |
| 01 | 105 |
| 01 | 12 |
| 01 | 105 |
| 02 | 34 |
| 02 | 34 |
| 03 | 22 |
| 03 | 22 |
| 03 | 22 |
| 03 | 18 |
output like this
|01 | 2 |
|02 | 1 |
|03 |2 |
and for example if i have a market_id has not Product_id how can i return
| 05 | 0 |
Thanks
Here is the Solution:
select market_id,count(distinct product_id) as count from TableName group by market_id

How sort data when use row_number() function?

I want to sort data when using the row_number() function or other way around.
I sort new "sort_type column" when "e_type column" changed last status (when s_type <> e_type).
Raw data:
req_no | seq | s_date | e_date | s_type | e_type |
---------+-------+------------+------------+-----------+----------+
001 | 1 | 2017-01-01 | 2017-01-02 | 01 | 01 |
001 | 2 | 2017-01-02 | 2017-01-02 | 01 | 02 |
001 | 3 | 2017-01-02 | 2017-01-02 | 02 | 02 |
001 | 4 | 2017-01-02 | 2017-01-02 | 02 | 01 |
001 | 5 | 2017-01-02 | 2017-01-02 | 01 | 01 |
001 | 6 | 2017-01-02 | 2017-01-02 | 01 | 01 |
001 | 14 | 2017-01-03 | 2017-01-03 | 04 | 03 |
001 | 15 | 2017-01-03 | 2017-01-03 | 03 | 03 |
001 | 16 | 2017-01-03 | 2017-01-03 | 03 | 03 |
001 | 17 | 2017-01-03 | 2017-01-03 | 03 | 03 |
I get this result from my query right now:
req_no | seq | s_date | e_date | s_type | e_type | sort_type
-------+------+------------+------------+--------+--------+----------
001 | 1 | 2017-01-01 | 2017-01-02 | 01 | 01 | 1
001 | 2 | 2017-01-02 | 2017-01-02 | 01 | 02 | 1
001 | 3 | 2017-01-02 | 2017-01-02 | 02 | 02 | 2
001 | 4 | 2017-01-02 | 2017-01-02 | 02 | 01 | 2
001 | 5 | 2017-01-02 | 2017-01-02 | 01 | 01 | 3
001 | 6 | 2017-01-02 | 2017-01-02 | 01 | 01 | 4
001 | 14 | 2017-01-03 | 2017-01-03 | 04 | 03 | 1
001 | 15 | 2017-01-03 | 2017-01-03 | 03 | 03 | 2
001 | 16 | 2017-01-03 | 2017-01-03 | 03 | 03 | 3
001 | 17 | 2017-01-03 | 2017-01-03 | 03 | 03 | 4
But I want the result to be like this:
req_no | seq | s_date | e_date | s_type | e_type | sort_type
-------+------+------------+------------+--------+--------+----------
001 | 1 | 2017-01-01 | 2017-01-02 | 01 | 01 | 1
001 | 2 | 2017-01-02 | 2017-01-02 | 01 | 02 | 1
001 | 3 | 2017-01-02 | 2017-01-02 | 02 | 02 | 2
001 | 4 | 2017-01-02 | 2017-01-02 | 02 | 01 | 2
001 | 5 | 2017-01-02 | 2017-01-02 | 01 | 01 | 3 (or not show)
001 | 6 | 2017-01-02 | 2017-01-02 | 01 | 01 | 4 (or not show)
001 | 14 | 2017-01-03 | 2017-01-03 | 04 | 03 | 5 (or 3)
001 | 15 | 2017-01-03 | 2017-01-03 | 03 | 03 | 6 (or not show)
001 | 16 | 2017-01-03 | 2017-01-03 | 03 | 03 | 7 (or not show)
001 | 17 | 2017-01-03 | 2017-01-03 | 03 | 03 | 8 (or not show)
002 | 1 | 2017-01-05 | 2017-01-05 | 01 | 02 | 1
002 | 2 | 2017-01-05 | 2017-01-05 | 03 | 03 | 2
002 | 3 | 2017-01-05 | 2017-01-05 | 03 | 03 | 2
002 | 4 | 2017-01-05 | 2017-01-05 | 03 | 04 | 2
002 | 5 | 2017-01-05 | 2017-01-05 | 04 | 04 | 3 (or not show)
This is my SQL Server query:
SELECT
a.*,
ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
FROM
tb_listtype a
Please help me. Thanks in advance ;)
Maybe help WITH
Try this
;With mytmpTable as
(
SELECT
a.*,
ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
FROM
tb_listtype a
)
select * from mytmpTable where e_type=3 and sort_type>3
Try this:
select req_no, seq, s_date, e_date, s_type, e_type,
sort_type - sort_type_temp [sort_type]
from (
select req_no, seq, s_date, e_date, s_type, e_type,
sum(sort_type) over (partition by req_no order by s_date rows between unbounded preceding and current row) sort_type,
case when s_type <> e_type then -1 else 0 end sort_type_temp
from (
select req_no, seq, s_date, e_date, s_type, e_type,
case when s_type <> e_type then 1 else 0 end sort_type
from MY_TABLE
) a
) a
Just use order by:
SELECT a.*,
ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
FROM tb_listtype a
ORDER BY req_no, sort_type;

T-SQL : partitioning using a case statement

I have the following table :
| RoomID | OrderID | Occupancy | Status |
+--------+---------+-----------+---------------+
| 01 | 101 | Vacant | inspection |
| 01 | 102 | Occupied | Preservation |
| 01 | 103 | Occupied | inspection |
| 01 | 104 | Vacant | inspection |
| 02 | 201 | Vacant | inspection |
| 02 | 202 | Occupied | inspection |
| 02 | 203 | Vacant | inspection |
| 03 | 301 | Vacant | inspection |
| 03 | 302 | Occupied | inspection |
| 03 | 303 | Occupied | Preservation |
| 03 | 304 | Occupied | Preservation |
| 04 | 401 | Occupied | inspection |
| 04 | 402 | Occupied | inspection |
| 04 | 403 | Vacant | Preservation |
| 04 | 404 | Occupied | inspection |
I need to pull my data on a RoomID level where the Occupancy = 'Occupied' and Status = 'Preservation' in any instance of a given RoomID.
The result should look like the following:
| RoomID | Flag |
+--------+---------+
| 01 | 1 |
| 02 | 0 |
| 03 | 1 |
| 04 | 0 |
I have an impression that this is easy but I cannot see it at the moment, thank you in advance for your help !
You can use conditional aggregation.
select roomid,
count(distinct case when Occupancy = 'Occupied' and Status = 'Preservation' then 1 end) flag
from tablename
group by roomid
You can also use the below query using UNION.
;with cte_1
AS
( SELECT DISTINCT RoomId
FROM YourTable
WHERE Occupancy='Occupied' AND Status='Predervation')
SELECT RoomId,1 Status
FROM cte_1
UNON
SELECT DISTINCT RoomId,0 Status
FROM YourTable t
WHERE NOT EXISTS(SELECT 1 FROM cte_1 c
WHERE t.RoomId=c.RoomId)

TSQL : conditional query

I am trying to find a way to get results where the Occupancy in the latest Inspection_date and the one before the last are not equal.
In this example only number RoomID 2 will be the only result because: the Occupancy for OrderID 201 = 'Vacant' <> to the Occupancy for OrderID 202 = 'Occupied'.
I have the beginning of the query but cannot seem to find a good logic to end the query.
| RoomID | OrderID | Occupancy | rn |
+--------+---------+-----------+----+
| 01 | 101 | Vacant | 1 |
| 01 | 102 | Vacant | 2 |
| 01 | 103 | Occupied | 3 |
| 01 | 104 | Vacant | 4 |
| 02 | 201 | Vacant | 1 |
| 02 | 202 | Occupied | 2 |
| 02 | 203 | Vacant | 3 |
| 03 | 301 | Occupied | 1 |
| 03 | 302 | Occupied | 2 |
| 03 | 303 | Occupied | 3 |
| 03 | 304 | Occupied | 4 |
| 04 | 401 | Occupied | 1 |
| 04 | 402 | Occupied | 2 |
| 04 | 403 | Vacant | 3 |
| 04 | 404 | Occupied | 4 |
SELECT i.room_number, order_number, Occupancy , row_number() OVER(PARTITION BY room_number ORDER BY Inspection_date DESC) rn
FROM #inspection_data i
In SQL Server 2012+, you can use lag(), so something like this:
SELECT i.*
FROM (SELECT i.room_number, order_number, Occupancy ,
ROW_NUMER() OVER (PARTITION BY room_number ORDER BY Inspection_date DESC) as seqnum,
LAG(Occupancy) OVER (PARTITION BY room_number ORDER BY Inspection_date) as prev_Occupancy
FROM #inspection_data i
) i
WHERE prev_Occupancy <> Occupancy AND seqnum = 1 ;

Access SQL : How to retrieve all records from 2 tables (no matter they do not have record in the other table)

I'm creating inventory report mainly to show current qty, sum of qty that was reserved from customers and Total Qty that available .
As I used LEFT JOIN and RIGHT JOIN but it couldn't work, so I'm thinking of UNION function but I could't make it work. Could you please help me. Thank you very much.
tbl_inventory
inv_id | pd_id | inv_qty_act | inv_date | inv_note
1 | 001 | 120 | 20-Sep-12|
2 | 003 | 387 | 1-Oct-12 |
tbl_reserve
res_id | cust_id | res_date | res_duedate | pd_id | res_qty | if_sent | res_note
3 | 10 | 01-Oct-12| 17-Oct-12 | 001 | 135 | |
4 | 9 | 01-Oct-12| 24-Oct-12 | 001 | 253 | |
5 | 22 | 01-Oct-12| 17-Oct-12 | 001 | 132 | |
6 | 2 | 01-Oct-12| 24-Oct-12 | 002 | 446 | |
tbl_product
pd_id | pd_name
001 | des1
002 | des2
003 | des3
tbl_pdtn_startup
pdtn_st_id | pd_id | pdtn_qty_est
2 | 002 | 200
3 | 003 | 100
Output that I want :
pd_id| pd_name| inv_qty_act|pdtn_qty_est| Sum(res_qty)| Total[(inv_qty_est) - Sum(res_qty)]
001 | des1 | 120 | 0 | 520 | -400 -->(120-520)
002 | des2 | 0 | 200 | 446 | -446 -->(0-446)
003 | des3 | 387 | 100 | 0 | 387
what about this?
SELECT
tbl_product.pd_id,
tbl_product.pd_name,
( SELECT Sum(inv_qty_act) FROM tbl_inventory AS t1
WHERE t1.pd_id=tbl_product.pd_id) AS SumOfinv_qty_act,
( SELECT Sum(pdtn_qty_est) FROM tbl_pdtn_startup AS t2
WHERE t2.pd_id =tbl_product.pd_id) AS SumOfpdtn_qty_est,
( SELECT Sum(res_qty) FROM tbl_reserve AS t3
WHERE t3.pd_id=tbl_product.pd_id) AS SumOfres_qty,
IIF(ISNULL([SumOfinv_qty_act]),0,[SumOfinv_qty_act])-
IIF(ISNULL([SumOfres_qty]),0,[SumOfres_qty]) AS Total
FROM
tbl_product;