I am struck with following process. I have two tables named table1 & table2.
table1 consists total amount with current status. table2 consists filled amount.
I want to get the result like, if the table1.status = 'cancel' then I need to get 2 rows as showed in expected result. Is there any way to do that?
table1
unq_id | amount | status | type
1 4 'cancel' 'two'
2 2 'done' 'one'
table2
p_id | unq_id | filled
1 1 1
2 2 2
My query
SELECT
a.unq_id as id_val,
(IFNULL(b.filled,0)) as filled
FROM
table1 AS a
LEFT JOIN table2 AS b
ON a.unq_id = b.unq_id
WHERE
(a.status = "done"
OR a.status = "cancel")
current result
id_val | filled
1 1
2 2
expected result
id_val filled
1 3 (cancelled amount [ 4 - filled amount])
1 1 (filled amount)
2 2
you can use a query like below
SELECT
a.unq_id as id_val,
ABS(opt-IFNULL(b.filled,0)) as filled
FROM
(select *, 0 as opt from table1
union all
select *,4 as opt from table1 where status = "cancel")AS a
LEFT JOIN table2 AS b
ON a.unq_id = b.unq_id
WHERE
(a.status = "done"
OR a.status = "cancel")
You can use Union...
First part of query you already have, giving you part of your result. You just need to add second query to it to fetch rest of the result:
SELECT a.unq_id as id_val,(ISNULL(b.filled,0)) as filled FROM
Tab1 AS a LEFT JOIN Tab2 AS b ON a.unq_id = b.unq_id
WHERE (a.status = "done" OR a.status = "cancel")
UNION
SELECT a.unq_id as id_val, a.amount - b.filled as filled FROM
Tab1 AS a LEFT JOIN Tab2 AS b ON a.unq_id = b.unq_id
WHERE (a.status = 'cancel')
ORDER BY id_val
Related
I have an application where students do three choices 1st, 2nd and 3rd from a list.
Table1 is the list they can choose from:
id
day
1
Monday
2
Thusday
3
Wednesday
Table2 is they fill in their choices:
id
first_choice
second_choice
third_choice
12345
1
3
2
23456
3
2
1
34567
2
1
3
45678
1
2
3
What I'm struggling with is that I want to count choices per day and priority to get a list like this:
id
first_choice
second_choice
third_choice
Monday
2
1
1
Thusday
1
2
1
Wednesday
1
1
2
SELECT a.day, count(b.first_choice), count(c.second_choice), count(d.third_choice) FROM table1 a LEFT JOIN table2 b ON a.id = b.first_choice LEFT JOIN table2 c ON a.id = c.second_choice LEFT JOIN table2 d ON a.id = d.third_choice GROUP BY a.day
But, by doing so I end up with this
id
first_choice
second_choice
third_choice
Monday
2
2
2
Thusday
2
2
2
Wednesday
2
2
2
Could anyone help me with the query?
Thanks in advance
In those table structures, I normally use subquery instead of join.
SELECT
a.day,
(SELECT COUNT(*) FROM Table2 WHERE first_choice = a.id) AS first_choice,
(SELECT COUNT(*) FROM Table2 WHERE second_choice = a.id) AS second_choice,
(SELECT COUNT(*) FROM Table2 WHERE third_choice = a.id) AS third_choice
FROM Table1 a
You can use a single INNER JOIN and work it out with conditional IF statements:
SELECT
Table1.id,
Table1.day,
COUNT(IF(Table2.first_choice=Table1.id, Table2.first_choice, NULL)) AS first_choice,
COUNT(IF(Table2.second_choice=Table1.id, Table2.second_choice, NULL)) AS second_choice,
COUNT(IF(Table2.third_choice=Table1.id, Table2.third_choice, NULL)) AS third_choice
FROM
Table1
INNER JOIN
Table2
ON
Table1.id = Table2.first_choice
OR
Table1.id = Table2.second_choice
OR
Table1.id = Table2.third_choice
GROUP BY
Table1.id,
Table1.day
Refer to this fiddle: https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eea27fe9364c0fe4b96a846c9441f723
Tabel_A
---------
id name
1 Kursi
2 Roda
3 Gigi
Tabel_B
---------
id id_tabel_A
1 2
Result
--------
name Status
Kursi 0
Roda 1
Gigi 0
Query of the Result : …………………… ?
use left join and case when
select name, case when b.id_tabel_A is null then 0 else 1 end as status
from tableA a left join tableB b on a.id=b.id_tabel_A
You apply IF syntax
SELECT a.name,
IF(
(
SELECT count(b.id_tabel_A)
from Tabel_B as b
WHERE b.id_tabel_A = a.id -- connect
) > 0
, "YES", "NO") as status
from Tabel_A as a
I recommend using exists:
select a.name,
(exists (select 1
from tableB b
where a.id = b.id_tabel_A
)
) as status
from tableA a;
The reason I prefer exists is that it automatically handles duplicates in tableB. You don't have to worry about the query returning duplicate results.
I have 2 tables in Mysql which looks as follows
Table 1
ID YEARMONTH
------------------
1 201210
2 201211
3 201212
Table2
ID YEARMONTH GRADE DESIG
--------------------------------
1 201210 G1 NULL
2 201210 G1 D2
3 201212 G1 D1
I am trying to set a new field (named FLAG) in table1 which will be Y if rows exist for t1.YEARMONTH in T2 else N
I want the following Result in T1
ID YEARMONTH FLAG
---------------------
1 201210 Y
2 201211 N
3 201212 Y
I have tried the following Query
SELECT
T1.ID,
T1.YEARMONTH,
CASE COUNT(T2.ID)
WHEN (SELECT
COUNT(T2.ID)
FROM TABLE2 T2)
> 0 THEN 'Y'
ELSE 'N'
END AS flag
FROM TABLE1 T1,
table2 T2;
But it produces the following output
ID YEARMONTH FLAG
---------------------------
1 201210 N
I don't know where my mistake lies. Any help would be appreciated.
Try using the below query:
SELECT
T1.ID, T1.YEARMONTH,
(CASE
WHEN COUNT(T2.ID) > 0 THEN 'Y'
ELSE 'N'
END) AS FLAG
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T1.YEARMONTH = T2.YEARMONTH
GROUP BY T1.ID, T1.YEARMONTH
You can see the required output here http://sqlfiddle.com/#!9/162855/12
TRY THIS: Using CASE with LEFT JOIN you can check the existence of t1.yearmonth in t2.yearmonth and generate the flag column accordingly
SELECT DISTINCT t1.id,
t1.yearmonth,
(CASE WHEN t2.yearmonth IS NOT NULL THEN 'y' ELSE 'n' END) flag
FROM table1 t1
LEFT JOIN table2 t2 ON t1.yearmonth = t2.yearmonth
I have two tables as following .I need a result shown as below.
table1
id name
-----------------
1 john
2 raju
3 gopi
4 sarath
table2
userid status
------------------
1 E
3 E
I need a query to select record from table like following
id name flag
---------------------
1 john In
2 raju Out
3 gopi In
4 sarath Out
If user having status 'E' will show in the result set as 'In' and others as 'Out'
A simple join and a case
SELECT table1.*,
CASE WHEN table2.status='E' THEN 'In' ELSE 'Out' END
FROM table1 LEFT JOIN table2 on table1.id = table2.userid;
i think below SQL will useful to you.
select table1.id, table1.name, CASE WHEN table2.status='E' THEN 'In' ELSE 'Out' END
from table1 JOIN table2 on table1.id = table2.userid;
I have two tables:
table1:
id
--
1
2
3
table2: (left out primary index)
id2 | cond
----------
3 | 1
3 | 0
2 | 1
2 | 1
2 | 0
I would need to construct a query that implicitly calculates this intermediary table:
temp:
id | c1 | c2
------------
1 | 0 | 2
2 | 2 | 2
3 | 1 | 2
with c1 = countRows(id2 == id && cone == 1) and c2 == countRows(id2 = 2 && cond == 1).
and then selects SELECT id FROM temp ORDER BY ABS(c1 - c2)*RAND().
My current try is kind of like:
SELECT id, COUNT(t1.id2) AS c1, COUNT(t2.id2) AS c2
FROM table1 LEFT JOIN (table2 AS t1) ON id=t1.id2 LEFT JOIN (table2 AS t2) ON t2.id2=2
WHERE t1.cond=1 AND t2.cond=1
GROUP BY t1.id2
ORDER BY ABS(c1 - c2)*RAND()
LIMIT 1
Which has multiple problems:
It doesn't select rows with no entry in table2
It doesn't correctly count
There seems to be a problem with group columns (c1, c2) in the ORDER BY part 3.
Help would be appreciated.
Update:
table1 represents players for example
table2 would be rounds played, with cond indicating a win
c1 represents the rounds won by each player
c2 represents the rounds won by a reference player (player 2 in this case)
SELECT t1.id, SUM(IFNULL(t2.cond, 0) = 1) AS c1, (SELECT SUM(cond = 1) FROM table2 WHERE id2 = 2) AS c2
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id2
GROUP BY t1.id
ORDER BY ABS(SUM(IFNULL(t2.cond, 0) = 1) - c2) * RAND();
Let's see if the first part of your query is returning valid results..
SELECT
t1.id AS id
COUNT(t2.id2) AS c1
COUNT(CASE WHEN t3.id2 = 2 THEN t3.id2 ELSE NULL END) as c2
FROM
table1 AS t1
LEFT JOIN table2 as t2
ON t1.id = t2.id2 AND t2.cond = 1
LEFT JOIN table2 as t3
ON t3.id2 = 2 AND t3.cond = 1
GROUP BY
t2.id2,
t3.id2
Can you provide schema?