I have two tables A and B with values below
Table A
X
------
1
2
3
Table B
X Y
------ ------
1 A
2 A
3 A
1 B
2 B
1 C
3 D
I need to find only the Y values from Table B which match All of the values in Table A.
So for the above example the only Y value that matches is A (A has an X value of 1,2,and 3)
This is an example of a "set-within-sets" subquery. I like to approach this with aggregation and a having clause.
select b.y
from tableB b join
tableA a
on b.X = a.X
group by b.y
having count(distinct b.x) = (select count(*) from tableA);
Related
I have Table A which has a relation "OneToMany" to Table C.
Table B is also related to the Table C. What I want is to get all the data from Table A merged with all ids of Table B as a column name(1-3 in this example) filled with correct data of Table B from Table C from column "value".
This is the simplest scenario I can imagine right now, where (x, y, z, j, k, l, m) can be anything and where (a, b, c) are just column names of Table A
Table A:
ID - a - b - c
--------------
1 - x - y - z
2 - x - y - z
3 - x - y - z
Table B:
ID
--
1
2
3
Table C:
ID - Table A (FK) - Table B (FK) - value
------------------------------------------
1 - 1 - 1 - j
2 - 1 - 2 - k
3 - 1 - 3 - l
4 - 2 - 1 - m
Expected result
ID - a - b - c - B1 - B2 - B3 (B1 means Table B with id 1 which should be column name)
-------------------------------------
1 - x - y - z - j - k - l ("j" for example is Value from Table C where Table A is 1 and Table B is 1 as well)
2 - x - y - z - m - NULL - NULL
3 - x - y - z - NULL - NULL - NULL
My dream query is:
select A.*, GET_ALL_IDS_AS_COLUMN_NAME_FROM_TABLE(B) from A SOMEHOW FILL DOES COLUMNS FROM B BY C BY THIS <MAGIC> CONDITION WHERE C.A = A
You can use conditional aggregation:
select
a.id,
a.a,
a.b,
a.c,
max(case when b.id = 1 then c.value end) b1,
max(case when b.id = 2 then c.value end) b1,
max(case when b.id = 3 then c.value end) b1,
from a
cross join b
left join c on c.id_a = a.id and c.id_b = b.id
group by
a.id,
a.a,
a.b,
a.c
Note that it would be simpler to concatenate all values from table C instead of spreading them over several columns:
select
a.*,
(
select group_concat(c.value order by b.id delimiter '-' )
from b
inner join c on c.id_a = a.id and c.id_b = b.id
) b_values
from a
I think you just need a left join and aggregation:
select a.*, c.b_1, c.b_2, c.b_3
from a left join
(select c.id_a,
max(case when b_id = 1 then value end) as b_1,
max(case when b_id = 2 then value end) as b_2,
max(case when b_id = 3 then value end) as b_3
from c
group by c.id_a
) c
on c.id_a = a.id;
But, if you want this dynamic, then you will need to use dynamic SQL to construct the pivot.
I need to find distinct pairs(x,y) from a table consisting 2 columns(X, Y).
The numbers which are repeated in the column shouldn't be included in the result set.
Table:
X Y
1 2
2 3
3 4
4 4
5 2
5 6
7 9
Result Set:
X Y
2 3
7 9
5 repeated in X and 2,4 in Y, so they will not form pairs with the corresponding Y and X.
The question was asked to me in an interview. Not able to find a solution. Need a query for this. Please help!
SELECT X, Y
FROM yourTable
WHERE X IN (SELECT X FROM yourTable GROUP BY X HAVING COUNT(*) = 1) AND
Y IN (SELECT Y FROM yourTable GROUP BY Y HAVING COUNT(*) = 1)
#strawberry appears to have found a way to do this using joins, but the option which popped into my head was to simply use non-correlated subqueries to find the X and Y values which appear only once in each respective column. Then, just use a WHERE clause to check that each X and Y value falls in these sets.
E.g.:
SELECT DISTINCT a.*
FROM my_table a
LEFT
JOIN my_table b
ON b.x = a.x
AND b.y <> a.y
LEFT
JOIN my_table c
ON c.y=a.y
AND c.x <> a.x
WHERE b.x IS NULL
AND c.x IS NULL;
I have a table as below,
Product Promotion exists (Y/N) Week
A Y 1
B Y 1
C Y 1
A Y 2
B Y 2
C N 2
A Y 3
B Y 3
C Y 3
A Y 4
B Y 4
C N 4
I want to see an Promition exists combination Output on total table. Something like
A, B - 4
B,C - 2
A,C - 2
Since this is Just for 3 products looks simple.. I am looking at some thousands of records, and looking for same combinations where total count of occurrence is greater than some number. If taking the above example, if that count is 4.. then my output should be
A,B - 4
Try this:
SELECT p1, p2, COUNT(*) AS cnt
FROM (
SELECT t1.Product AS p1, t2.Product AS p2
FROM mytable AS t1
JOIN mytable AS t2
ON t1.Week = t2.Week AND
t1.Product < t2.Product AND
t1.Exists = 'Y' AND t2.Exists = 'Y') AS t
GROUP BY p1, p2
ORDER BY cnt DESC
To get only pairs exceeding a certain value, just wrap the above in a subquery and add a WHERE cnt >= someValue.
Demo here
I want to output two rows in a one entry but with two tables. here is my table:
table_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
table_B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
I want an output like this
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
you can use a UNION , first query will get all with status as V based on tableB and second one gets all rows from A
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
I think this does what you want:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B doesn't seem necessary at all.
Mysql database has a tableA which has a many columns . one of the columns is SIM1.
Another table is tableB which has many columns . one of the columns is SIM2
the requirement is to join all columns of tableA and tableB given that SIM1 = SIM2.
LIKE THIS
tableA
col1 col2 SIM1 ..........col24
a x 1 5
b y 1 3
c z 0 2
d g 2 1
tableB
colA colB SIM2
x g 1
y f 0
x s 0
y e 2
The result of Select query should be
col1 col2 SIM1............col24 colA colB SIM2
a x 1 ........... 5 x g 1
c z 0 ......... . 2 x s 0
d g 2 .......... 1 y e 2
is it possible?
select * from tableA inner join tableB on tableA.SIM1 = tableB.SIM2