SQL Oracle get data with more of one associated data - sql

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

Related

How to update table 2 from the inserted data in table 1?

Can you help me on what query I to to update one table with data from another.
I have 2 tables for example:
tbl_med_take
| id | name | med | qty |
---------------------------------
| 1 | jayson | med2 | 3 |
| 2 | may | med2 | 4 |
| 3 | jenny. | med3 | 6 |
| 4 | joel. | med3 | 4 |
tbl_med
| id | med | stocks |
-----------------------------
| 1 | med1 | 20 |
| 2 | med2 |. 17 |
| 3 | med3 | 24 |
The output that I want in tbl_med:
tbl_med
| id | med | stocks |
-----------------------------
| 1 | med1 | 20 |
| 2 | med2 |. 10 |
| 3 | med3 | 14 |
First get the total consumed from med_tbl_take using
select med,sum(quantity) as total from tbl_med_take group by med
Then you can left join with your med_tbl and subtract.
select m.id,m.med,(m.stocks-ISNULL(n.total,0)) from tbl_med m
left join
(select med,sum(quantity) as total from tbl_med_take group by med) n
on m.med=n.med
CHECK DEMO HERE

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

Segregate rows according to their HEAD (parent) - sql

I have the following SQL table.
+----+--------+----------+--------+
| ID | TestNo | TestName | HeadID |
+----+--------+----------+--------+
| 1 | 21 | Comp-1 | null |
| 2 | 22 | C1 | 21 |
| 3 | 23 | C2 | 21 |
| 4 | 24 | C3 | 21 |
| 5 | 47 | Comp-2 | null |
| 6 | 25 | C4 | 47 |
| 7 | 26 | C1+ | 21 |
+----+--------+----------+--------+
I want to get all the child rows (according to their HeadID) below their head test.
select * from ranges order by HeadID
The ACTUAL OUPUT I get from the above query:
+----+--------+----------+--------+
| ID | TestNo | TestName | HeadID |
+----+--------+----------+--------+
| 1 | 21 | Comp-1 | null |
| 5 | 47 | Comp-2 | null |
| 2 | 22 | C1 | 21 |
| 3 | 23 | C2 | 21 |
| 4 | 24 | C3 | 21 |
| 7 | 26 | C1+ | 21 |
| 6 | 25 | C4 | 47 |
+----+--------+----------+--------+
but my DESIRED OUTPUT is:
+----+--------+----------+--------+
| ID | TestNo | TestName | HeadID |
+----+--------+----------+--------+
| 1 | 21 | Comp-1 | null |
| 2 | 22 | C1 | 21 |
| 3 | 23 | C2 | 21 |
| 4 | 24 | C3 | 21 |
| 7 | 26 | C1+ | 21 |
| 5 | 47 | Comp-2 | null |
| 6 | 25 | C4 | 47 |
+----+--------+----------+--------+
How can I achieve this?
If you have only one level of children, then you can achieve this ordering like this:
SELECT *
FROM Ranges
ORDER BY
CASE WHEN HeadID IS NULL THEN TestNo ELSE HeadID END
,HeadID
,ID
;

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)

Access/SQL - split multiple columns to multiple rows based on column ID

I've tried a few things but can't get this to work efficiently. Help!
Customer dataset example (basically a dump of over 100 sensor board readings with 42 sensors per board)
| BoardName | ReadingTime | Sensor1 | Sensor2 | Sensor3 | Sensor4 ... Sensor42 |
-------------------------------------------------------------------------------------
| BoardA | 1224201301 | 18 | 24 | 7 | etc etc for each column |
| BoardB | 1224201301 | 18 | 23 | 8 | etc etc for each column |
| BoardC | 1224201301 | 17 | 24 | 7 | etc etc for each column |
| BoardD | 1224201301 | 16 | 23 | 6 | etc etc for each column |
| BoardA | 1224201302 | 18 | 22 | 5 | etc etc for each column |
| BoardB | 1224201302 | 18 | 23 | 5 | etc etc for each column |
-------------------------------------------------------------------------------------
This seems like a pretty inefficient table design. I'd like to get it into SQL more like the following example, which makes the data a little more accessible.
| SensorID | ReadingTime | SensorValue |
----------------------------------------
| BrdASen1 | 1224201301 | 18 |
| BrdASen2 | 1224201301 | 24 |
| BrdASen3 | 1224201301 | 7 |
| BrdBSen1 | 1224201301 | 18 |
| BrdBSen1 | 1224201301 | 23 |
| BrdBSen1 | 1224201301 | 8 |
| etc etc |
----------------------------------------
So basically, I want to iterate through each row of imported data, split off the 42 columns into individual rows with a 3-column SensorID/Date/Value format.
I would use this table structure:
|BoardName|SensorId| ReadingTime | SensorValue |
------------------------------------------------
|BoardA | 1 | 1224201301 | 18 |
|BoardA | 2 | 1224201301 | 24 |
SensorId values would be from 1 - 42.
To populate the table, you'll need to use the UNPIVOT operator.