How many different Product has one Market SQL - 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

Related

Biguqery Row Number Perioding based on specific values

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 |
+---------+-------+--------+----+

select all rows that match criteria if not get a random one

+----+---------------+--------------------+------------+----------+-----------------+
| id | restaurant_id | filename | is_profile | priority | show_in_profile |
+----+---------------+--------------------+------------+----------+-----------------+
| 40 | 20 | 1320849687_390.jpg | | | 1 |
| 60 | 24 | 1320853501_121.png | 1 | | 1 |
| 61 | 24 | 1320853504_847.png | | | 1 |
| 62 | 24 | 1320853505_732.png | | | 1 |
| 63 | 24 | 1320853505_865.png | | | 1 |
| 64 | 29 | 1320854617_311.png | 1 | | 1 |
| 65 | 29 | 1320854617_669.png | | | 1 |
| 66 | 29 | 1320854618_636.png | | | 1 |
| 67 | 29 | 1320854619_791.png | | | 1 |
| 74 | 154 | 1320922653_259.png | | | 1 |
| 76 | 154 | 1320922656_332.png | | | 1 |
| 77 | 154 | 1320922657_106.png | | | 1 |
| 84 | 130 | 1321269380_960.jpg | 1 | | 1 |
| 85 | 130 | 1321269383_555.jpg | | | 1 |
| 86 | 130 | 1321269384_251.jpg | | | 1 |
| 89 | 28 | 1321269714_303.jpg | | | 1 |
| 90 | 28 | 1321269716_938.jpg | 1 | | 1 |
| 91 | 28 | 1321269717_147.jpg | | | 1 |
| 92 | 28 | 1321269717_774.jpg | | | 1 |
| 93 | 28 | 1321269717_250.jpg | | | 1 |
| 94 | 28 | 1321269718_964.jpg | | | 1 |
| 95 | 28 | 1321269719_830.jpg | | | 1 |
| 96 | 43 | 1321270013_629.jpg | 1 | | 1 |
+----+---------------+--------------------+------------+----------+-----------------+
I have this table and I want to select the filename for a given list of restaurants ids.
For example for 24,29,154:
+----+---------------
| filename |
+----+---------------
1320853501_121.png (has is_profile 1)
1320854617_311.png (has is_profile 1)
1320922653_259.png (chosen as profile picture because restaurant doesn't have a profile pic but has pictures)
I tried group by and case statements but I got nowhere.Also if you use group by it should be a full group by.
You can do this with aggregation and some logic:
select restaurant_id,
coalesce(max(case when is_profile = 1 then filename end),
max(filename)
) as filename
from t
where restaurant_id in (24, 29, 154)
group by restaurant_id;
First look for the/a profile filename. Next just choose an arbitrary one.

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)

Split One Row in to Two Rows in SQL

I have a table with below columns
PAX_NO | AMOUNT_ONE | AMOUNT_TWO | PAX_NO_LASTYEAR | AMOUNT_ONE_LASTYEAR | AMOUNT_TWO_LASTYEAR
45 | 56.54 | 43.23 | 23 | 23.43 | 78.43
69 | 21.11 | 51.19 | 20 | 11.23 | 55.99
I need to get it as below
PAX_NO | AMOUNT_ONE | AMOUNT_TWO
45 | 56.54 | 43.23
23 | 23.43 | 78.43
69 | 21.11 | 51.19
20 | 11.23 | 55.99
You can just use UNION ALL:
SELECT pax_no, amount_one, amount_two FROM your_table
UNION ALL
SELECT PAX_NO_LASTYEAR, AMOUNT_ONE_LASTYEAR, AMOUNT_TWO_LASTYEAR FROM your_table;

SQL Oracle get data with more of one associated data

I've a table with some data like this:
| cod_1 | cod_2 |
-------------------------
| 1 | 03 |
| 2 | 07 |
| 2 | 09 |
| 3 | 09 |
| 4 | 01 |
| 4 | 02 |
| 4 | 08 |
In this case I need to get the cod_1 2 and 4 because they have more tahn one cod_2 associated.
You can do it like this:
SELECT cod_1
FROM tablename
GROUP BY cod_1
HAVING count(*) > 1