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.
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 want to get records from table ps_stock_available without default "0" attribute if product has attribute. As you know in ps_stock_available table show's default attribute as 0
for example ...
id_product id_product_attribute
1 0
1 12
1 13
1 14
2 0
3 0
4 0
4 1
4 2
4 4
What i want is to get records by mysql query such as
id_product id_product_attribute
1 12
1 13
1 14
2 0
3 0
4 1
4 2
4 4
Let me know if someone can help.
on possible way to do this.
select * from tbl_name
where ((p_id in (205, 206) and att_id != 0)
or (att_id = 0 and !(p_id = 205 or p_id=206)));
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
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.