Queryin One-to-Many same table in Oracle - sql

I have a below table with sample data as below:
prod seq catid
-------------------
prod1 0 10
prod1 1 20
prod1 2 30
prod1 3 40
prod2 0 10
prod3 0 10
prod3 1 20
prod3 2 30
prod4 0 10
I need to query above table based on catid column.
Ex: If i query with catid - 10 then i need to get all products(here prod2, prod3, prod4) which has only catid as 10 all the other should be excluded.
Same way if i query with catid = 10 20 30 then i need to get output as prod3, if i query with catid as 10 20 30 40 then my output should be prod1.
SELECT * FROM table WHERE catid = ALL (1,2,3,4,5)
I tried using ALL in my query but i am not able to get desired output, please help.

You can get the expected prod value using the query:
SELECT prod
FROM mytable
GROUP BY prod
HAVING COUNT(DISTINCT catId) = 5 AND
COUNT(CASE WHEN catId NOT IN (1,2,3,4,5) THEN 1 END) = 0
The above query returns products having 5 distinct catId values. None of these values doesn't belong to (1,2,3,4,5) set of values.

One way to do this is using conditional aggregation:
SELECT prod
FROM yourTable
GROUP BY prod
HAVING SUM(CASE WHEN catid = 1 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 2 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 3 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 4 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 5 THEN 1 ELSE 0 END) > 0

Here's a way that is more generalised - it relies on you passing in a string to the query consisting of a comma separated list (no spaces) into the query:
WITH your_table AS (SELECT 'prod1' prod, 0 seq, 10 catid FROM dual UNION ALL
SELECT 'prod1' prod, 0 seq, 20 catid FROM dual UNION ALL
SELECT 'prod1' prod, 0 seq, 30 catid FROM dual UNION ALL
SELECT 'prod1' prod, 0 seq, 40 catid FROM dual UNION ALL
SELECT 'prod2' prod, 0 seq, 10 catid FROM dual UNION ALL
SELECT 'prod3' prod, 0 seq, 10 catid FROM dual UNION ALL
SELECT 'prod3' prod, 0 seq, 20 catid FROM dual UNION ALL
SELECT 'prod3' prod, 0 seq, 30 catid FROM dual UNION ALL
SELECT 'prod4' prod, 0 seq, 10 catid FROM dual)
-- end of mimicking your table with sample data in it
-- see sql below:
SELECT yt.prod
FROM your_table yt
GROUP BY yt.prod
HAVING min(CASE WHEN ','||:p_string_to_compare||',' LIKE '%,'||yt.catid||',%' THEN 'Y'
ELSE 'N'
END) = 'Y'
AND count(*) = regexp_count(:p_string_to_compare, ',') + 1;
With :p_string = '10,40,30,20':
PROD
-----
prod1
With :p_string = '10,20,30':
PROD
-----
prod3
With :p_string = '10, 20':
no rows returned
With :p_string = '10':
PROD
-----
prod4
prod2

Related

how to sum with case with oracle sql

I m having data in columns as:
item_id
month_in
amount
1
1
1500
1
1
1000
2
1
2500
3
1
2600
3
1
1000
4
1
2700
4
1
1000
1
2
1500
1
2
2000
2
2
1000
3
3
2500
3
3
2500
4
3
1000
4
3
2500
I want to have like this result
item_id
januari
februari
maret
1
2500
3500
0
2
2500
1000
0
3
3600
0
0
4
3700
0
3500
in oracle sql query how to solve this.
please help me
I have try this
select
item_id,
(case month_in=1 then sum(amout) end) AS januari
from table
group by item_id, month_in
order by item_id asc
but not working as I expected
We can try a pivot query here:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
FROM yourTable
GROUP BY
item_id
ORDER BY
item_id;
You almost got it. This is the simplest solution, without extra hassle:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
from table
group by item_id
order by item_id asc
You don't need to define the months as a separate rowset, because the months are defined as values 1, 2, 3, ... as columns. Similarly, the items are defined by the group by function.

count zeros between 1s in same column

I've data like this.
ID IND
1 0
2 0
3 1
4 0
5 1
6 0
7 0
I want to count the zeros before the value 1. So that, the output will be like below.
ID IND OUT
1 0 0
2 0 0
3 1 2
4 0 0
5 1 1
6 0 0
7 0 2
Is it possible without pl/sql? I tried to find the differences between row numbers but couldn't achieve it.
The match_recognize clause, introduced in Oracle 12.1, can do quick work of such "row pattern recognition" problems. The solution is just a bit complex due to the special treatment of a "last row" with ID = 0, but it is straightforward otherwise.
As usual, the with clause is not part of the solution; I include it to test the query. Remove it and use your actual table and column names.
with
inputs (id, ind) as (
select 1, 0 from dual union all
select 2, 0 from dual union all
select 3, 1 from dual union all
select 4, 0 from dual union all
select 5, 1 from dual union all
select 6, 0 from dual union all
select 7, 0 from dual
)
select id, ind, out
from inputs
match_recognize(
order by id
measures case classifier() when 'Z' then 0
when 'O' then count(*) - 1
else count(*) end as out
all rows per match
pattern ( Z* ( O | X ) )
define Z as ind = 0, O as ind != 0
);
ID IND OUT
---------- ---------- ----------
1 0 0
2 0 0
3 1 2
4 0 0
5 1 1
6 0 0
7 0 2
You can treat this as a gaps-and-islands problem. You can define the "islands" by the number of "1"s one or after each row. Then use a window function:
select t.*,
(case when ind = 1 or row_number() over (order by id desc) = 1
then sum(1 - ind) over (partition by grp)
else 0
end) as num_zeros
from (select t.*,
sum(ind) over (order by id desc) as grp
from t
) t;
If id is sequential with no gaps, you can do this without a subquery:
select t.*,
(case when ind = 1 or row_number() over (order by id desc) = 1
then id - coalesce(lag(case when ind = 1 then id end ignore nulls) over (order by id), min(id) over () - 1)
else 0
end)
from t;
I would suggest removing the case conditions and just using the then clause for the expression, so the value is on all rows.

how to get the count of data based on status id in sql

i have following table
id statusid
100 1
100 2
100 3
101 1
101 3
i am getting the result like following
id data1 data2 data3
100 1 1 1
101 1 0 1
but i want the result like following
id data1 data2+data3
100 1 2
101 1 1
i am using the following query:
select id, SUM(CASE WHEN statusid=1 THEN 1 ELSE 0 END) AS data1,
SUM(CASE WHEN statusid=2 THEN 1 ELSE 0 END) AS data2,
SUM(CASE WHEN statusid=3 THEN 1 ELSE 0 END) AS data3
from employee
group by id
any help.
thank you.
Sudha.
You can count statuses 2 and 3 together like so:
select id,
sum(case when statusid = 1 then 1 else 0 end) as data1,
sum(case when statusid in (2, 3) then 1 else 0 end) as data2_3
from employee
group by id
You may use PIVOT clause to get the required result.
Use DECODE to map the status so that the 2 and 3 produce the same value
with tab2 as (select
id, decode(status_id,3,2,status_id) status_id2
from tab)
select * from tab2
PIVOT (count(*) "CNT" for status_id2 in
(1 as "DATA_1",
2 as "DATA_2_3")
)
ID DATA_1_CNT DATA_2_3_CNT
---------- ---------- ------------
100 1 2
101 1 1

SQL query sum of total corresponding rows

I have two tables as below. Caseid from first table is referenced in second table along with accidents. What I am trying to get total different accidents for a case type. Below two tables I documented sample data and expected result.
Table case:
caseId CaseType
1 AB
2 AB
3 AB
4 CD
5 CD
6 DE
Table CaseAccidents:
AccidentId caseID AccidentRating
1 1 High
2 1 High
3 1 Medium
4 1 LOW
5 2 High
6 2 Medium
7 2 LOW
8 5 High
9 5 High
10 5 Medium
11 5 LOW
Result should look like:
CaseType TotalHIghrating TotalMediumRating TotalLOWRating
AB 3 2 2
CD 2 1 1
DE 0 0 0
To get the sum of every rating, you can Use a SUM(CASE WHEN) clause, adding 1 by every record that match the rating.
In your question, you have pointed out that you want to see all distinct CaseType, you can get it by using a RIGHT JOIN, this will include all records of case table.
select case.CaseType,
sum(case when caseAccidents.AccidentRating = 'High' then 1 else 0 end) as TotalHighRating,
sum(case when caseAccidents.AccidentRating = 'Medium' then 1 else 0 end) as TotalMediumRating,
sum(case when caseAccidents.AccidentRating = 'LOW' then 1 else 0 end) as TotalLowRating
from caseAccidents
right join case on case.caseId = caseAccidents.caseID
group by case.CaseType;
+----------+-----------------+-------------------+----------------+
| CaseType | TotalHighRating | TotalMediumRating | TotalLowRating |
+----------+-----------------+-------------------+----------------+
| AB | 3 | 2 | 2 |
+----------+-----------------+-------------------+----------------+
| CD | 2 | 1 | 1 |
+----------+-----------------+-------------------+----------------+
| DE | 0 | 0 | 0 |
+----------+-----------------+-------------------+----------------+
Check it: http://rextester.com/MCGJA9193
Have you use case in a select clause before?
select C.CaseType,
sum(case when CA.AccidentRating = 'High' then 1 else 0 end)
from Case C join CaseAccidents CA on C.CaseId = CA.CaseId
group by C.CaseType
Please see this. Sample query of the table and also that result
create table #case(caseid int,casetype varchar(5))
insert into #case (caseid,casetype)
select 1,'AB' union all
select 2,'AB' union all
select 3,'AB' union all
select 4,'CD' union all
select 5,'CD' union all
select 6,'DE'
create table #CaseAccidents(AccidentId int, CaseId int,AccidentRating varchar(10))
insert into #CaseAccidents(AccidentId, CaseId, AccidentRating)
select 1,1,'High' union all
select 2,1,'High' union all
select 3,1,'Medium' union all
select 4,1,'Low' union all
select 5,2,'High' union all
select 6,2,'Medium' union all
select 7,2,'Low' union all
select 8,5,'High' union all
select 9,5,'High' union all
select 10,5,'Medium' union all
select 11,5,'Low'
My script
select c.casetype,
sum(case when ca.AccidentRating='High' then 1 else 0 end) as TotalHighRating,
sum(case when ca.AccidentRating='Medium' then 1 else 0 end) as TotalMediumRating,
sum(case when ca.AccidentRating='Low' then 1 else 0 end) as TotalLowRating
from #case c
Left join #CaseAccidents ca
on c.Caseid=ca.Caseid
group by c.casetype
Hope This could help!
Another approach using Pivot operator
SELECT casetype,
[High],
[Medium],
[Low]
FROM (SELECT c.casetype,
AccidentRating
FROM case c
LEFT JOIN CaseAccidents ca
ON ca.CaseId = c.caseid)a
PIVOT (Count(AccidentRating)
FOR AccidentRating IN ([High],
[Medium],
[Low]) ) p
Try This code once.
select casetype,
sum(case when ca.AccidentRating='High' then 1 else 0 end ) as TotalHIghrating,
sum(case when ca.AccidentRating='Medium' then 1 else 0 end ) as TotalMediumRating ,
sum(case when ca.AccidentRating='Low' then 1 else 0 end ) as TotalLOWRating
from #case c
left join #CaseAccidents ca on c.caseid=ca.CaseId
group by casetype

Count in sql with group by

Table name: mytable
Id username pizza-id pizza-size Quantity order-time
--------------------------------------------------------------
1 xyz 2 9 2 09:00 10/08/2014
2 abc 1 11 3 17:45 13/07/2014
This is mytable which has 6 columns. Id is int, username is varchar, order-time is datetime and rest are of integer datatype.
How to count the number of orders with the following pizza quantities: 1, 2, 3, 4, 5, 6,7 and above 7?
Using a T-SQL query.
It would be very helpful If any one could help to me find the solution.
Try this !
SELECT COUNT(ID),CASE WHEN QUANTITY<7 THEN QUANTITY ELSE 'ABOVE7' END AS QUANTITIES
FROM mytable
GROUP BY CASE WHEN QUANTITY<7 THEN QUANTITY ELSE 'ABOVE7' END
Try
Select CASE WHEN Quantity > 7 THEN 'OVER7' ELSE Cast(quantity as varchar) END Quantity,
COUNT(ID) NoofOrders
from mytable
GROUP BY CASE WHEN Quantity > 7 THEN 'OVER7' ELSE Cast(quantity as varchar) END
or
Select
SUM(Case when Quantity = 1 then 1 else 0 end) Orders1,
SUM(Case when Quantity = 2 then 1 else 0 end) Orders2,
SUM(Case when Quantity = 3 then 1 else 0 end) Orders3,
SUM(Case when Quantity = 4 then 1 else 0 end) Orders4,
SUM(Case when Quantity = 5 then 1 else 0 end) Orders5,
SUM(Case when Quantity = 6 then 1 else 0 end) Orders6,
SUM(Case when Quantity = 7 then 1 else 0 end) Orders7,
SUM(Case when Quantity > 7 then 1 else 0 end) OrdersAbove7
from mytable
If the requirement is like count the number of orders with different pizza quantities and represent count of orders as : 1, 2, 3, 4, 5, 6,7 and consider all above order counts in new category : 'above 7' then you can use window function as:
select case when totalorders < = 7 then cast(totalorders as varchar(10))
else 'Above 7' end as totalorders
, Quantity
from
(
select distinct count(*) over (partition by Quantity order by Quantity asc)
as totalorders,
Quantity
from mytable
) T
order by Quantity
DEMO
Edit: if the requirement is like count the number of orders with pizza quantities: 1, 2, 3, 4, 5, 6,7 and consider all other pizza quantities in new category : 'above 7' then you can write as:
select distinct
count(*) over (
partition by Quantity order by Quantity asc
) as totalorders,
Quantity
from (
select
case when Quantity < = 7 then cast(Quantity as varchar(20))
else 'Above 7' end as Quantity, id
from mytable ) T
order by Quantity
DEMO