query getting record with special flag from two queries - mysql

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;

Related

SQL/Presto: how to choose rows if the values match with another table's

I have 2 tables:
table 1:
task cnt
1 4
2 5
3 6
table 2:
task cnt2
1 7
2 5
3 6
4 3
I want to add a column for table 2 such that if cnt in table1 for a task is the same as cnt2 for a task in table2. If there is no match, mark it as 'no match'
desired result:
task cnt2 if_matched
1 7 'no match'
2 5 'yes'
3 6 'yes'
4 3 'no match'
I started from a query like the one below to pick tasks that have matched values
select task from table1 where table1.cnt = table2.cnt2
but I got an error for the where part.
Use left join and case expression to calculate matched:
select t2.task, t2.cnt,
case when t1.task is null then 'no match' else 'yes' end as matched
from table2 t2
left join table1 t1 on t1.task=t2.task and t1.cnt = t2.cnt2
I would recommend exists. Both Presto and MySQL support boolean expressions, so you can use:
select t2.*,
(exists (select 1 from table1 t1 where t1.task = t2.task and t1.cnt = t2.cnt)
) as t1_matches
from table2 t2;
You can use a case expression to convert this to a string, but I prefer a boolean flag.
Note: A left join can multiply the number of rows if there are multiple matches in table1. That is the reason why I recommend exists.

Using CASE in Mysql to set value to an Alias field

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

How to split a row into 2 based on an if condition

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

concatenate specific rows in one record and retrieve all data from first table

I need to concatenate two fields into a single row from two tables and retrieve all data from the first table and the concat specifications
I have two tables, something like this:
Table1
ID UserName Location Area
---------- --------- ----------- -------------
1 user1 MX area1
2 user2 US area2
3 user3 US area3
4 user4 MX area4
5 user5 US area5
and Table2:
ID UserNameBkp
---------- -----------
1 userA
2
3 userC
4 userD
5
and I want a query that shows the results like this:
ID UserName location Area
---------- ------------- ----------- -------------
1 user1, userA MX area1
2 user2 US area2
3 user3, userC US area3
4 user4, userD MX area4
5 user5 US area5
I'm trying with SELECT concat but I can't manage to retrieve all data like this.
We can do this by using CONCAT only using case for NULL handling as like below but above one concat_ws is more suitable than this, leaving this answer just for one alternative:
SELECT t1.*,
CONCAT(UserName,
CASE WHEN UserNameBkp IS NULL THEN
''
ELSE
CONCAT(', ',UserNameBkp)
END) AS UserName
FROM tblone t1
LEFT JOIN tbltwo t2 ON t1.id = t2.id;
This would seem to be a simple left join:
select t1.*,
concat_ws(', ', UserName, UserNameBkp) as UserName
from table1 t1 left join
table2 t2
on t1.id = t2.id;
Hi,You can use the below logic,
SELECT T1.ID,
T1.UserName,
CASE
WHEN T2.UserNameBkp IS NOT NULL THENT1.UserName||,||T2.UserNameBkp
WHEN T1.UserName IS NULL THEN T2.UserNameBkp
ELSE T1.UserName
END AS UserName,
T1.Location,T1.Area FROM table1 T1
INNER JOIN table2 T2
ON (T1.ID = T2.ID)

SQL count each distinct value in column and add name from another table where the ID matches

My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO