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
Related
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
I'm looking for the nearest value to zero for a particular id
My input is
tech_Id time_diff
1000 100
1000 200
1000 -50
1000 -10
1001 100
1001 200
1001 50
1001 10
1002 -50
1002 -10
1003 130
1003 -140
1003 -2
expected output
tech_Id time_diff
1000 -10
1001 10
1002 -10
1003 -2
thanks in advance
You can find min of absolute value of time diff.
select t1.*
from your_table t1
join (
select tech_id, min(abs(time_diff)) as time_diff
from your_table
group by tech_id
) t2 on t1.tech_id = t2.tech_id and abs(t1.time_diff) = t2.time_diff;
I was asked to create a database for both students' tardiness and school policy's violations.
Now, I have three separate tables:
tbl_ClassList:
Student_ID Student_Name
1000 Lee, Jonder
1001 Chow, Stephen
1002 Kim, Martin
1003 Johns, Kevin
1004 Hearfield, Olivia
1005 Jarrs, Marlon
tbl_Tardy:
Record_No Student_ID
1 1001
2 1001
3 1000
4 1003
5 1002
6 1003
7 1001
8 1001
9 1002
10 1004
tbl_Violation:
Record_No Student_ID
1 1000
2 1000
3 1004
4 1005
5 1001
6 1002
7 1003
What I was asked to do is to generate a list that combines that contains information about the students including his/her ID, name, number of tardiness and the number of violations. Something like this:
Student_ID Student_Name No. of Tardy No. of Violation
1000 Lee, Jonder 1 2
1001 Chow, Stephen 4 1
1002 Kim, Martin 2 1
1003 Johns, Kevin 2 1
1004 Hearfield, Olivia 1 1
1005 Jarrs, Marlon 0 1
Is there any type of Joins I can use to achieve the output? Please help me.
You can find separate aggregates for tardy and violations inside subqueries and left join them with classlist table. Use coalesce to get zero in case there is no row for tardy/violation.
select c.*,
coalesce(t.count_tardy, 0) as no_of_tardy,
coalesce(v.count_violations, 0) as no_of_violations,
from tbl_classlist c
left join (
select student_id,
count(*) as count_tardy
from tbl_tardy
group by student_id
) t on c.student_id = t.student_id
left join (
select student_id,
count(*) as count_violations
from tbl_violation
group by student_id
) v on c.student_id = v.student_id;
Need to fetch data from master table based on child table condition.
Master Table:-
ID Name Address
1 abc xyz
2 abs txt
3 aui tre
4 pop top
5 the tre
6 pot tos
7 pog sop
8 pat top
9 bat cric
10 not kot
Child Table:-
chid shootid imagename IDFK
101 234 123ab.jpg 3
102 234 54abcab.jpg 3
103 235 123abc.jpg 3
104 236 12390acb.jpg Null
105 235 12332aab.jpg 8
106 234 123786ab.jpg 4
107 234 54789abcab.jpg 10
108 235 122343abc.jpg 10
109 235 122123acb.jpg 4
110 234 12123aab.jpg 9
111 234 1223ab.jpg Null
112 233 5432abcab.jpg Null
113 235 1239abc.jpg Null
114 236 1238acb.jpg 2
115 236 12356aab.jpg 2
116 236 1235ab.jpg 2
117 236 545abcab.jpg Null
118 237 1233abc.jpg 1
119 237 1223acb.jpg 1
120 237 1123aab.jpg 1
In Child table IDFK is the foreign key and ID in Master table is the primary key of that.
Now i want to show those name from master table that doesn't exist on child table filter on shootid like where childtable.Shootid=234. I tried but not find the desired output.Every time it just return's the same out for different shootid as well.
Please help me and show me the right query for that.
I don't know if I am understand you well or not but I think this is what you want,
Select * from [master] m
where m.ID not in (Select IDFK from detail where shootid=234)
I think this is what you are looking for.
Select distinct m.name from master m LEFT OUTER JOIN child c
ON m.id = c.id and
c.shootid=234
where
c.id is null
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