Query didn't gave me the expected result - mysql

ap table
id tid code pid cl date
1 1 1002 17 DC 2011-04-05 16:14:37
2 1 1002 18 DC 2011-04-05 16:14:37
3 1 1002 19 DC 2011-04-05 16:14:37
4 2 1002 12 SC 2011-04-05 16:15:59
5 2 1002 9 SC 2011-04-05 16:15:59
6 2 1002 21 SC 2011-04-05 16:15:59
7 3 1003 20 DC 2011-04-07 14:57:40
8 3 1003 12 DC 2011-04-07 14:57:40
9 3 1003 9 DC 2011-04-07 14:57:40
10 3 1003 21 DC 2011-04-07 14:57:40
11 4 1002 18 SC 2011-04-08 18:10:04
12 4 1002 19 SC 2011-04-08 18:10:04
13 4 1002 22 SC 2011-04-08 18:10:04
14 5 1001 17 SC 2011-04-08 18:21:10
at table
tid totalamount code pstatus cdate
1 65.98 1002 R 2011-04-05 16:14:37
2 17.74 1002 R 2011-04-05 16:15:59
3 96.36 1003 R 2011-04-07 14:57:40
4 25.94 1002 S 2011-04-08 18:10:04
5 9 1001 R 2011-05-08 18:21:10
i tried like this but its not correct
SELECT at.totalamount, at.pstatus, ap.cl
FROM `at` AS at
INNER JOIN ap AS ap ON at.`code` = ap.`code`
WHERE ap.cl = 'SC'
AND at.`code` = '1002'
GROUP BY at.`code`
LIMIT 0 , 30
I expected Result
pstatus->S pstatus->R
code Stotalamount Rtotalamount Sstatus Rstatus cl
1002 25.94 17.74 S R SC
Please any body tell me the correct query

It seems you want to add the totalamount twice, once for the the R status and another for the S status.
But I don't understand how you are joining the ap table and why the 65.98 is not added. Should the join be done on the date columns as well?
This may work but you have to test it with different data to be sure. Or explain more of what the columns in the two tables represent and what you want the query to show.
SELECT at.code
, SUM(IF(at.pstatus='S', at.totalamount, 0))
AS Stotalamount
, SUM(IF(at.pstatus='R', at.totalamount, 0))
AS Rtotalamount
FROM at
WHERE EXISTS
( SELECT 1
FROM ap
WHERE at.code = ap.code
AND at.cdate = ap.date
AND ap.cl = 'SC'
)
AND at.code = 1002
GROUP BY at.code
LIMIT 0 , 30

Related

Mysql create columns from certain rows of tables

I have these three tables
Table #1: Ships IDs
Ship ID Ship Code
1 ALBE
2 AMBS
3 AMBR
Table #2 : Equipment Data
Ship ID Equipment_ID
1 1001
1 1002
1 1003
2 1004
2 1005
2 1006
3 1007
3 1008
3 1009
Table #3 : Counters
Equipment_ID Hours Date_Updated
1001 2 2018-01-01
1001 4 2018-05-01
1002 3 2018-01-01
1002 5 2018-05-01
1003 5 2018-01-01
1003 10 2018-05-01
1004 1 2018-01-01
1004 6 2018-05-01
1005 3 2018-03-01
1006 6 2018-03-01
1007 5 2018-01-01
1007 12 2018-05-01
1008 15 2018-01-01
1008 19 2018-05-01
1009 4 2018-01-01
1009 12 2018-06-01
I want the below result:
Per each Ship Code the last counter hours and equipment_id categorized as AE1,AE2,AE3 where AE1 are 1001,1004,1007 AE2 are 1002,1005,1008 and AE3 are 1003,1006,1009
Ship Code AE1 AE2 AE3
ALBE 4 5 10
AMBS 6 6 12
AMBR 12 19 12
How can I create this dynamic pivot table?
Use conditional aggregation:
select s.ship_code,
sum(case when e.equipment_id in (1001, 1004, 1007) then hours else 0 end) as ae1,
sum(case when e.equipment_id in (1001, 1004, 1007) then hours else 0 end) as ae2,
sum(case when e.equipment_id in (1003,1 006, 1009) then hours else 0 end) as ae3
from ships s left join
equipment e
on e.ship_id = s.ship_id left join
counters c
on c.equipment_id = e.equipment_id
group by s.ship_code;
You could approach the pivot table like so:
Ship Code Equipment ID Hour Type
ALBE 4 AE1
AMBS 6 AE1
AMBR 12 AE2
AMBR 12 AE3
That way you could query it more easily

Mysql rank with join table, result not true

I have 2 table name table 'hasil' and table 'kat_soal' to join and give rank on each 'KatID' field on 'hasil' table..
here is my hasil table :
HasilID KatID UserID JBenar JSalah Nilai
15 1 1000 2 1 66.66666666666666
16 3 1000 2 0 100
17 1 1001 1 2 33.33333333333333
18 3 1001 1 1 50
19 1 1002 3 0 90
20 3 1002 2 0 80
and there is my kat_soal table
KatID Kategori Lama
1 IPA 30
2 IPS 30
3 Matematika 30
4 Bahasa Indonesia 20
5 Bahasa Inggris 20
this my query generate rank:
SELECT a.KatID,a.UserID,b.Kategori,c.Nama,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
FIND_IN_SET( a.Nilai, l.list) AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
JOIN datauser c
ON a.UserID=c.UserID
CROSS JOIN
(SELECT GROUP_CONCAT( a2.Nilai ORDER BY a2.Nilai DESC ) as list
FROM hasil a2) l
WHERE a.KatID='1'
ORDER BY a.Nilai DESC;
my result
//FOR KatID=1
KatID UserID Kategori Nama JBenar JSalah Nilai rank
1 1002 IPA ratam 3 0 90.00 2
1 1000 IPA Tarsan 2 1 66.67 4
1 1001 IPA wisnu 1 2 33.33 6
//FOR KatID=3
3 1000 Matematika Tarsan 2 0 100.00 1
3 1002 Matematika ratam 2 0 80.00 3
3 1001 Matematika wisnu 1 1 50.00 5
My Expected Result
//FOR KatID=1
KatID UserID Kategori Nama JBenar JSalah Nilai rank
1 1002 IPA ratam 3 0 90.00 1
1 1000 IPA Tarsan 2 1 66.67 2
1 1001 IPA wisnu 1 2 33.33 3
//FOR KatID=3
3 1000 Matematika Tarsan 2 0 100.00 1
3 1002 Matematika ratam 2 0 80.00 2
3 1001 Matematika wisnu 1 1 50.00 3
anyone can help me ?
Good example to solve the issue is by looking at: http://www.fromdual.com/ranking-mysql-results .
You are making this a bit complicated: First You take the value, making the value a string, then "finding position in string".
From the example it should be completely ok if it is done as (untested):
SET #rank=0;
SELECT a.KatID,a.UserID,b.Kategori,c.Nama,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
#rank:=#rank+1 AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
JOIN datauser c
ON a.UserID=c.UserID
WHERE a.KatID='1'
ORDER BY rank;
EDIT: Changed ordering - You are expecting to by ordered by rank in the final.
Below is the script without using table datauser for any1 to test:
SET #rank=0;
SELECT a.KatID,a.UserID,b.Kategori,a.JBenar,a.JSalah,ROUND(a.Nilai,2) as Nilai,
#rank:=#rank+1 AS rank
from hasil a
JOIN kat_soal b
ON a.KatID = b.KatID
WHERE a.KatID='1'
ORDER BY rank;

Converting Rows in Datas to Columns for results obtained from multiple joins in Mysql

SELECT DataID.unique_id as unique_id, question.question_id
FROM DataID
RIGHT JOIN bookend
on DataID.user_id = bookend.user_id and DataID.client_id = bookend.client_id
LEFT JOIN question
on bookend.user_id = question.user_id
where type = 'test';
I am getting this value
unique_id question_id answer_id
2333 23 1
2333 24 5
2333 25 10
321 23 2
321 24 6
321 25 7
But i need to display it as
unique_id Q1 A1 Q2 A2 Q3 A3
2333 23 1 24 5 25 10
321 23 2 24 6 25 7
How Could i do this?

one is to many relationship, getting latest row

lead_sp_id sp_id phone
1 5 111
2 5 222
3 5 333
4 3 444
5 3 555
6 3 666
7 3 777
8 5 888
act_id lead_sp_id sp_id act_time
1 1 5 2012-12-31 14:20:49
2 1 5 2012-12-30 14:20:49
3 2 5 2012-12-29 14:20:49
4 2 5 2012-12-31 14:20:49
5 2 5 2012-12-28 14:20:49
6 4 3 2012-12-31 14:20:49
7 4 3 2012-12-28 14:20:49
8 4 3 2012-12-25 14:20:49
result I would like to get is
phone | lead_sp_id | sp_id | act_time
111 | 1 | 5 | 2012-12-31 14:20:49
222 | 2 | 5 | 2012-12-31 14:20:49
Now I know I have to use a join statement and using the regular join statement just gives me too many result including the old dates all I am looking to to do is to get the latest act_name and act_time for each phone number based on the sp_id.
Try this:
SELECT a.phone, b.lead_sp_id, b.sp_id, b.act_time
FROM leadsptable a
INNER JOIN (SELECT a.lead_sp_id, a.sp_id, a.act_time
FROM acttable a
INNER JOIN (SELECT lead_sp_id, MAX(act_time) act_time
FROM acttable GROUP BY lead_sp_id
) b ON a.lead_sp_id = b.lead_sp_id AND a.act_time = b.act_time
) b ON a.lead_sp_id = b.lead_sp_id AND a.sp_id = b.sp_id
Try this query
SELECT
*
FROM table1 as t1
INNER JOIN (
SELECT
*
FROM
table2
INNER JOIN (SELECT MAX(act_id) FROM table2 GROUP BY act_id)
as r on r.act_id = table2.act_id
) as t2 on t1.sp_id = t2.sp_id

count amount for sub code

tab1
id code name
1001 0 palani
1002 1001 shanker
1003 1002 raghu
1004 1003 kabhir
1005 1003 vani
1006 1002 priya
tab2
id code name amount tax
1 1002 b 100 1
2 1002 j 20 10
3 1003 jk 23 20
4 1004 jk 675 9
5 1005 o 67 3
6 1003 u 122 4
7 1003 o 98 1
8 1003 iu 98 1
9 1002 po 4 0.4
10 1005 pl 1 0.1
12 1005 tf 1 0.1
24 1006 e 23 2.3
id 1001 see code 1001 corresponding id 1002
id 1002 see code 1002 corresponding id 1003,1006
id 1003 see code 1003 corresponding id 1004,1005
others no need
Result need like this
code amount tax
1001 124 11.4
1002 364 28.3
1003 744 12.2
Please send the mysql query for this
GROUP BY is your friend:
select tab1.id, sum (tab2.amount) amount, sum (tab2.tax) tax
from
tab1, tab2
where tab1.code = tab2.id
and tab1.id in (1001, 1002, 1003)
group by tab1.id
This will get what you want from tab2
SELECT code, SUM(amount), SUM(tax) FROM tab GROUP BY code ORDER BY code
I can't see how tab1 fits into what you want
does it work (not tested)
select SUM(t2.amount) AS Amount,SUM(t2.tax) AS totalTax,t2.code FROM
table2 t2
INNER JOIN table1 t1 ON t1.code=t2.code