Data set looks like this:
ID Rank Case
1 1 1
1 2 0
1 3 0
2 1 0
2 2 1
2 3 0
3 1 1
3 2 0
3 3 0
I want to find all the IDs that has Rank=1 Case=0, Rank=2 Case=1, Rank=3 Case=0. In the above case, this would return ID2
select id
from your_table
group by id
having sum(rank=1 and `case`=0) > 0
and sum(rank=2 and `case`=1) > 0
and sum(rank=3 and `case`=0) > 0
Related
I have 2 tables, 1 first look this:
Table state_inventary
ID_STATE_INVENTARY, DESCRIPTION
0 STORE
1 TRANSIT
2 SOLD_STORE
3 STORAGE
Table article_stock
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
0 1 A 10 0
1 2 A 0 1
2 1 B 5 2
3 3 C 0 3
4 4 D 0 3
5 5 E 10 1
6 2 A 0 2
7 1 B 0 2
I need to find articles with ID_STATE_INVENTORY with value 0 or 2 or 3, I get it
But I need to find articles with the UNIT_SOLD the sum is zero, I don't know how do these
I want to find somthing like that
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
3 3 C 0 3
4 4 D 0 3
OR
ARTICLE
C
D
In my query I have next result
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
1 2 A 0 1
3 3 C 0 3
4 4 D 0 3
6 2 A 0 2
7 1 B 0 2
Anyone idea how can I do?
Try this.
SELECT SUM(`UNIT_SOLD`) AS `UNIT_SOLD`, `ARTICLE` FROM `table_name` GROUP BY `ARTICLE`;
Let's say I have a table:
ID A B
10 0 0
11 0 0
12 0 1
13 0 1
14 1 1
15 1 1
16 1 1
And I want my table output to be:
ID A B A_B_COUNT
10 0 0 2
11 0 0 2
12 0 1 2
13 0 1 2
14 1 1 3
15 1 1 3
16 1 1 3
but with the code I have here my output looks like this
SELECT ID, COUNT(*) AS A_B_COUNT
FROM table
GROUP BY A, B
ID A B A_B_COUNT
10 0 0 2
12 0 1 2
14 1 1 3
Any way I can create an sql query to is like my top table vs the one I make currently
Using: 10.5.5-MariaDB
Use window functions:
select t.*, count(*) over (partition by a, b) as a_b_count
from t;
You can do:
select e.A, e.B, c.cnt as A_B_COUNT
from entries e
join (
select A, B, count(*) as cnt
from entries
group by A, B
) as c
where e.A=c.A and e.B=c.B
See db-fiddle.
I have four tables,in each one I have two columns ID and Available. I need a Select Statement that finds the set of ID's that corresponds to available =0.
Table A: ID : 1 2 3 4 5 6
Available :1 1 0 0 0 0
Table B : ID 1 2 3 4 5 6
Available 1 1 1 0 0 0
Table C : ID 1 2 3 4 5 6
Available 0 1 0 0 1 0
Table D : ID 1 2 3 4 5 6
Available 1 1 0 0 1 0
If I understand correctly, you can use intersect if you want all the available values to be 0:
select id from a where available = 0
intersect
select id from b where available = 0
intersect
select id from c where available = 0
intersect
select id from d where available = 0;
If you want any of them to be 0, then use union instead.
I had a table with following details
cID sID Name pID childrenCount
1 1 Site 1 5
2 1 Safty 2 4
3 1 Archit 3 3
4 1 Civil 1 0
5 1 Concs 1 0
6 1 Pavm 1 0
7 1 Paint 3 0
8 1 Alum 3 0
9 1 Doors 3 0
10 1 Highw 1 0
11 1 Road 1 0
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beaco 2 0
What I want is to write a select query to order the above table first by their childrenCount values in descending order and the list the corresponding rows according as below
cID sID Name pid childrenCount
1 1 Site 1 5
4 1 Civil 1 0
5 1 Conc 1 0
6 1 Pavm 1 0
10 1 Highw 1 0
11 1 Road 1 0
2 1 Safty 2 4
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beacon 2 0
3 1 A WRK 3 3
7 1 Paint 3 0
8 1 Alumin 3 0
9 1 Doors 3 0
Thanks In Advance
Try this...first order by pid then childrenCount desc.
SELECT * FROM TABLENAME order by pid, childrenCount desc
Try this:
SELECT * FROM tableA ORDER BY pid, childrenCount DESC, cid
I have a table like this
ID Name Score_1 Score_2 Score_3
1 Abcd 4 5 5
2 Bdc 8 7 0
3 dcd 0 0 3
4 cdded 0 0 0
I need another column in the end which can count the number of non zero columns.Result should be like this
ID Name Score_1 Score_2 Score_3 Count
1 Abcd 4 5 5 3
2 Bdc 8 7 0 2
3 dcd 0 0 3 1
4 cdded 0 0 0 0
Thank you
select *,
if(score_1<>0,1,0)+if(score_2<>0,1,0)+if(score_3<>0,1,0) as `count`
from table
Use a select like this when you need instead of storing a calculated field.