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.
Related
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
Assume that I have a table call client.
In this table I have 3 fields (A, B, C)
I would like to group each rows when values of A = B = C
ex:
A B C otherRow
1 2 3 x
2 1 x
4 2 y
I would like to get the folowing
(A,B,C) otherRow Count
1 x 2
2 x 2
2 y 1
3 x 1
4 y 1
Your query is UNION, not JOIN:
SELECT
`A,B,C`,
otherRow,
COUNT(`A,B,C`) AS `Count`
FROM
(SELECT a AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT b AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT c AS `A,B,C`, otherRow FROM t) AS u
GROUP BY
`A,B,C`,
otherRow
HAVING
`A,B,C` IS NOT NULL
check this fiddle. I've added NULL-check since it's not obvious what are your "empty" values. If you'll remove it, you'll get zero-count NULL-rows.
So I have two tables that I need to aggregate data by.
The first looks like this:
zip code | key
x 1
x 2
x 3
y 4
y 5
The second looks like this:
characteristics | key
a 1
b 1
c 1
d 2
e 2
f 3
g 4
and I need to join them to look like this...
zip code | key | characteristics
x 1 a
x 1 b
x 1 c
x 2 d
x 2 e
x 3 f
y 4 g
... ... ...
I can't quite think of what the correct subqueries / joins would be to make this happen. Any help is really appreciated.
Try this
select table1.zipCode, table1.key, table2.characteristics
from table1
inner join table2 on table1.key = table2.key
Ok...
Then try this.
select t1.zipcode, t1.keys, t2.character
from table_1 t1
full outer join Table_2 t2 on t1.keys = t2.keys
I need to obtain all elements m_id of Table A where m_active is = N and that the corresponding elements in Table B have ALL v_active = N.
m_id is foreign key in Table B.
In the example below, what I am looking for is m_id=2 and and m_id=4 as both satisfy the condition of being m_active=N and have ALL v_active = N.
How do I go about that?
Thanks
Table A example:
m_id m_active
1 Y
2 N
3 Y
4 N
Table B example:
v_id m_id v_active
1 1 N
2 1 Y
3 1 N
4 2 N
5 2 N
6 2 N
7 3 N
8 3 Y
9 3 Y
10 4 N
Try this:
SELECT * FROM A
WHERE m_active='N'
AND NOT EXISTS (
SELECT * FROM B
WHERE B.m_id=A.m_id
AND B.v_active<>'N'
);
SELECT *
FROM a
WHERE m_active = 'N'
AND m_id NOT IN
(
SELECT m_id
FROM b
WHERE v_active <> 'N'
)
This will also select all entries from a which have no corresponding entries in b (and hence all 0 of 0 entries are inactive). This may or may not be what you want.
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