I have 4 tables as a bellow
table : user
id_tkn | nm_user |
-----------------------------------------
1 | belva |
2 | nanda |
3 | maya |
-----------------------------------------
table : maintenance
id_mntnc|id_tkn | sts | date |
-----------------------------------------------------------------
1 | 2 | 1 |2016-03-03 |
2 | 2 | 2 |2016-03-03 |
3 | 1 | 1 |2016-03-03 |
4 | 2 | 0 |2016-03-03 |
5 | 2 | 1 |2016-03-03 |
-----------------------------------------------------------------
table : Installasi
id_istlsi|id_tkn| sts | date |
-----------------------------------------------------------------
1 | 2 | 1 |2016-03-03 |
2 | 1 | 1 |2016-03-03 |
3 | 1 | 1 |2016-03-03 |
4 | 2 | 1 |2016-03-03 |
5 | 3 | 1 |2016-03-03 |
-----------------------------------------------------------------
table : visit
id_vst |id_tkn | sts | date |
-----------------------------------------------------------------
1 | 2 | 1 |2016-03-03 |
2 | 2 | 0 |2016-03-03 |
3 | 1 | 1 |2016-03-03 |
-----------------------------------------------------------------
information about 'sts' column
0 --> Pending
1 --> Success
2 --> Fail.
from the table above,I want to add up by status(sts) where id_tkn = 2,the result like table below,how to generate SQL command to produce the table below ?
id_tkn | nm_usr | maintenance_suc | maintenance_fail | installasi_suc| installasi_fail | visit_suc| visit_fail
-----------------------------------------------------------------------------------------------------------------------
2 | nanda | 2 | 1 | 2 | 0 | 1 | 0
This should work right, but probably need to be optimized:
select
u.id_tkn,
u.nm_user,
sum ( case when stat.sts = 1 and stat.typ = 'm' then 1 else 0 end ) as maintenance_suc,
sum ( case when stat.sts = 2 and stat.typ = 'm' then 1 else 0 end ) as maintenance_fail,
sum ( case when stat.sts = 1 and stat.typ = 'i' then 1 else 0 end ) as installasi_suc,
sum ( case when stat.sts = 2 and stat.typ = 'i' then 1 else 0 end ) as installasi_fail,
sum ( case when stat.sts = 1 and stat.typ = 'v' then 1 else 0 end ) as visit_suc,
sum ( case when stat.sts = 2 and stat.typ = 'v' then 1 else 0 end ) as visit_fail
from
user u
left join
(
select sts, 'm', id_tkn
from
maintenance
union all
select sts, 'i', id_tkn
from
Installasi
union all
select sts, 'v', id_tkn
from
visit
) stat on u.id_tkn = stat.id_tkn
where u.id_tkn = 2
group by u.id_tkn, u.nm_user
Related
I have the following table. I would like to add 2 new columns with a select query that will show the total based on the flag type.
Table:
tt | company | count | flag
--------------------------------------------
123 | adeco | 5 | 1
123 | mic | 4 | 2
333 | manpower | 88 | 2
444 | linar | 2 | 2
555 | dlank | 3 | 1
Desired:
tt | company | total | flag | total_flag1 | total_flag2
-------------------------------------------------------------------
123 | adeco | 5 | 1 | 5 | 0
123 | mic | 4 | 2 | 0 | 4
333 | manpower | 88 | 2 | 0 | 88
444 | linar | 2 | 2 | 0 | 2
555 | dlank | 3 | 1 | 3 | 0
By your desired result, you should use case when or if syntax to to this:
select
yourtable.*,
case when flag = 1 then `count` else 0 end as total_flag1,
case when flag = 2 then `count` else 0 end as total_flag2
from yourtable
Or
select
yourtable.*,
if(flag = 1, `count`, 0) as total_flag1,
if(flag = 2, `count`, 0) as total_flag2
from yourtable
I think you can do what you want using correlated subqueries or join:
select t.*, tsum.total_flag1, tsum.total_flag2
from t join
(select t.tt,
sum(case when flag = 1 then total else 0 end) as total_flag1,
sum(case when flag = 2 then total else 0 end) as total_flag2
from t
group by t.tt
) tsum
on t.tt = tsum.tt;
I have two tables.
TABLE A
ID | LABEL|PARENT_ID| VISIBLE | HIDEABLE
1 |SPORTS| 1 | 1 | 1
2 | FOOD | 1 | 1 | 0
3 | CARS | 1 | 0 | 1
4 |TRAVEL| 1 | 1 | 1
5 | LOVE | 1 | 1 | 1
6 | OTHER| 1 | 1 | 0
7 | SHOES| 3 | 0 | 1
8 |TRAVEL| 3 | 1 | 1
9 |NATURE| 3 | 1 | 1
10 |FRIEND| 3 | 1 | 0
11 | CARS | 3 | 0 | 1
12 | LOVE | 3 | 1 | 1
TABLE B
ID | LABEL|PARENT_ID| DISPLAY_POST | DISPLAY_COMMENTS
1 |SPORTS| 1 | 1 | 1
2 | FOOD | 1 | 1 | 0
3 | CARS | 1 | 0 | 1
4 |TRAVEL| 3 | 1 | 1
I want to insert data into TABLE B from TABLE A with these checks:
A.LABEL <> B.LABEL AND A.VISIBLE=1 AND A.HIDEABLE=1
How can I do this?
Everything I try it returns duplicates or missing rows.
Just use insert . . . select with the right conditions:
insert into b( . . .)
select . . .
from a
where a.visible = 1 and a.hideable = 1 and
not exists (select 1 from b2 where a.label = b2.label);
The . . . are just the list of columns for inserting.
Can anyone help me to group 2 tables and show like below...
My first table is :
user_id | department
-----------------------
1 | user1
----------------------
2 | user2
----------------------
and table 2 is :
reserve_id | user_id | reserve_status
--------------------------------------
1 | 1 | 1
--------------------------------------
2 | 1 | 1
--------------------------------------
3 | 1 | 2
--------------------------------------
4 | 2 | 2
--------------------------------------
5 | 2 | 0
--------------------------------------
6 | 2 | 1
*reserve_status 0 uncheck, 1 = approve , 2 = disapproval
I want product like this :
department | sumreserve | sumreservapprove | sumreserveunapprove | uncheck
----------------------------------------------------------------------------
user1 | 3 | 2 | 1 | 0
----------------------------------------------------------------------------
user2 | 3 | 1 | 1 | 1
Sorry for poor in English
You require a pivot solution.
Demo:
Assuming so_q27895250_users and so_q27895250_reserve as table names
mysql>
mysql> select u.department, count(r.reserve_status) as total_reserves,
-> count( case when r.reserve_status = 0
-> then r.reserve_status else null end ) as unchecked,
-> count( case when r.reserve_status = 1
-> then r.reserve_status else null end ) as approved,
-> count( case when r.reserve_status = 2
-> then r.reserve_status else null end ) as unapproved
-> from so_q27895250_reserve r
-> left join so_q27895250_users u
-> on r.user_id = u.user_id
-> group by r.user_id
-> ;
+------------+----------------+-----------+----------+------------+
| department | total_reserves | unchecked | approved | unapproved |
+------------+----------------+-----------+----------+------------+
| user1 | 3 | 0 | 2 | 1 |
| user2 | 3 | 1 | 1 | 1 |
+------------+----------------+-----------+----------+------------+
2 rows in set (0.00 sec)
I have two table and first named table1:
ID | Name | Type | isActive | isDeleted |
-----------------------------------------------
1 | item 1 | 4 | 1 | 0 |
2 | item 2 | 2 | 1 | 0 |
3 | item 3 | 1 | 1 | 1 |
4 | item 4 | 1 | 1 | 0 |
5 | item 5 | 1 | 1 | 0 |
6 | item 6 | 3 | 1 | 0 |
7 | item 7 | 1 | 1 | 0 |
8 | item 8 | 2 | 1 | 0 |
9 | item 9 | 1 | 1 | 0 |
10 | item 10 | 1 | 1 | 0 |
AND second named table1_meta:
ID | table1_id | options | value
------------------------------------
1 | 1 | dont_ask | 1
2 | 2 | dont_ask | 1
3 | 5 | dont_ask | 1
4 | 6 | dont_ask | 1
5 | 8 | alwasys_ask| 1
6 | 9 | alwasys_ask| 1
7 | 1 | is_flagged | 1
8 | 2 | is_flagged | 0
9 | 3 | is_flagged | 0
10 | 4 | is_flagged | 0
11 | 5 | is_flagged | 0
12 | 6 | is_flagged | 1
13 | 7 | is_flagged | 0
14 | 8 | is_flagged | 0
15 | 9 | is_flagged | 0
16 | 10 | is_flagged | 0
I'm trying to count rows in table1 where certain specific criteria is met, some of these conditionals.
The WHERE condition must contain these criteria:
table1.type = 1 and table1.isActive = 1 and table1.isDeleted = 0 and table1_meta.options = 'is_flagged' and table1_meta.value = 0
and this:
table1_meta.options = 'dont_ask' and table1_meta.value = 1
and this:
table1_meta.options = 'always_ask' and table1_meta.value = 1
so, how can I do that?
SQLFiddle link: http://sqlfiddle.com/#!2/2eb27b
Thanks.
I assume you are trying to count rows in the first table. Here is one way using subqueries:
select count(*)
from table1 t1
where t1.type = 1 and t1.isActive = 1 and t1.IsDeleted = 0 and
exists (select 1
from table1_meta tm
where t1.id = tm.table1_id and tm.options = 'is_flagged' and tm.value = 0
) and
not exists (select 1
from table1_meta tm
where t1.id = tm.table1_id and
tm.options = 'dont_ask' and tm.value = 1
) and
exists (select 1
from table1_meta tm
where t1.id = tm.table1_id and
tm.options = 'always_ask' and tm.value = 1
);
This has a separate subquery for each condition on the meta table.
I think I found the answer of my question.
the query is:
SELECT Count(*) AS total FROM
(SELECT Count(*)
FROM table1 t1,
table1_meta t1meta
WHERE t1.type = 1
AND t1.isactive = 1
AND t1.isdeleted = 0
AND t1meta.options = 'is_flagged'
AND t1meta.value = 0
AND t1.id NOT IN (SELECT table1_id
FROM table1_meta tm
WHERE tm.options = 'dont_ask'
AND tm.value = 1)
UNION
SELECT Count(*)
FROM table1_meta tm,
table1 t1
WHERE t1.id = tm.table1_id
AND tm.options = 'always_ask'
AND tm.value = 1) x
Thanks anyway, Gordon.
My scenario is... I have 2 type of judges who will give different set of points to members (eg. Judge Type A(55%) and Judge Type B(45%)) Both set of points will sum separately and averaged and then will sum together (which is 100% in total of 2 judges' points). I want to union both results into expected output below.
Table Member
-----------------------------
ID | Name
1 | John
2 | Doe
-----------------------------
Table Judge
-----------------------------
ID | Type | Name
1 | 1 | Judge A
2 | 1 | Judge B
3 | 2 | Judge C
4 | 1 | Judge D
-----------------------------
Table Point
-----------------------------
ID | FK | Judge | Type | Point
1 | 1 | 1 | 1 | 10
2 | 1 | 1 | 2 | 15
3 | 1 | 1 | 3 | 15
4 | 2 | 2 | 1 | 8
5 | 2 | 2 | 2 | 6
6 | 2 | 2 | 3 | 5
7 | 2 | 3 | 4 | 3
8 | 2 | 3 | 5 | 2
9 | 2 | 3 | 6 | 1
10 | 2 | 4 | 1 | 3
11 | 2 | 4 | 2 | 6
12 | 2 | 4 | 3 | 12
-----------------------------
Output Total Points
-----------------------------
ID | Name | Type1 | Type2 | Type3 | Total1 | CountJudge1 | Type4 | Type5 | Type6 | Total2 | CountJudge2
1 | John | 10 | 15 | 15 | 40 | 1 | 0 | 0 | 0 | 0 | 0
2 | Doe | 11 | 12 | 17 | 40 | 2 | 3 | 2 | 1 | 6 | 1
-----------------------------
Expected Output
-----------------------------
ID | Name | Type1 | Type2 | Type3 | Total1 | CountJudge1 | Type4 | Type5 | Type6 | Total2 | CountJudge2 | TotalPoint
1 | John | 10 | 15 | 15 | 40 | 1 | 0 | 0 | 0 | 0 | 0 | 40
2 | Doe | 5.5 | 6 | 8.5 | 20 | 2 | 3 | 2 | 1 | 6 | 1 | 26
-----------------------------
The sql (Output Total Points) that I could do at this moment which is still wrong is below and I can't figure out how to average all types of point and total points.
SELECT m.Name,
COUNT( CASE WHEN j.Type=1 THEN j.ID ELSE 0 END ) AS Judge1,
SUM( CASE WHEN p.Type=1 THEN p.Point ELSE 0 END ) AS Type1,
SUM( CASE WHEN p.Type=2 THEN p.Point ELSE 0 END ) AS Type2,
SUM( CASE WHEN p.Type=3 THEN p.Point ELSE 0 END ) AS Type3,
SUM( p.Score ) AS Total1,
COUNT( CASE WHEN j.Type=2 THEN j.ID ELSE 0 END ) AS Judge2,
SUM( CASE WHEN p.Type=4 THEN p.Point ELSE 0 END ) AS Type4,
SUM( CASE WHEN p.Type=5 THEN p.Point ELSE 0 END ) AS Type5,
SUM( CASE WHEN p.Type=6 THEN p.Point ELSE 0 END ) AS Type6,
SUM( p.Score ) AS Total2
FROM table_point AS p
LEFT JOIN table_member AS m ON ( m.ID = p.FK)
LEFT JOIN table_judge AS j ON ( j.ID = p.Judge )
GROUP BY m.ID
* UPDATE 1 *
I figured out the statement for expected result. However, as data grows bigger, my statement get slower. (250 members, 13 judges, 9750 scores (3 scores per judge per student) takes bout +-6 seconds). When I doubled members and scores, the execution time also doubled. Any idea to optimize this query?
SELECT MemberID, MemberName,
Judge1, Score1, Score2, Score3,
Judge2, Score4, Score5, Score6,
(((Score1 + Score2 + Score3) / (Judge1 / 3)) + ((Score4 + Score5 + Score6) / (Judge2 / 3))) AS TotalScore
FROM
(
SELECT m.ID AS MemberID, m.Name AS MemberName,
COUNT(j1.ID) AS Judge1,
SUM(CASE WHEN p.TypeID=1 THEN p.Point ELSE 0 END) AS Score1,
SUM(CASE WHEN p.TypeID=2 THEN p.Point ELSE 0 END) AS Score2,
SUM(CASE WHEN p.TypeID=3 THEN p.Point ELSE 0 END) AS Score3,
COUNT(j2.ID) AS Judge2,
SUM(CASE WHEN p.TypeID=4 THEN p.Point ELSE 0 END) AS Score4,
SUM(CASE WHEN p.TypeID=5 THEN p.Point ELSE 0 END) AS Score5,
SUM(CASE WHEN p.TypeID=6 THEN p.Point ELSE 0 END) AS Score6
FROM table_point AS p
LEFT JOIN table_member AS m ON ( m.ID = p.FK)
LEFT JOIN table_judge AS j1 ON ( j1.ID = p.Judge AND j1.Type=1 )
LEFT JOIN table_judge AS j2 ON ( j2.ID = p.Judge AND j2.Type=2 )
GROUP BY m.ID
) as tbl1
ORDER BY TotalScore DESC