I need to join three tables(from a MySQL database) where the second one is the link between the others.
The problem is tables 1 and 2 have multiple values (by Id or FeeId) and I cannot group in order to create a table like that:
EXPECTED RESULT TABLE
PaymentId
Amount
FeeCost
001
50
6
002
100
10
003
85
8
T1
PaymentId
Amount
0001
50
0002
75
0002
25
0003
20
0003
15
0003
50
T2
PaymentId
FeeId
001
AAA
002
AAB
003
AAC
T3
FeeId
FeeCost
AAA
2
AAB
4
AAC
3
AAC
2
AAC
3
AAA
2
AAB
4
AAA
2
AAB
2
I tried with multiple codes like that but I cannot join in a correct way
SELECT PaymentId, SUM(Amount), SUM(FeeCost)
FROM T2
INNER JOIN T1 ON T1.paymentId = T2.paymentId
INNER JOIN T3 ON T3.FeeId = T2.FeeId
GROUP BY T2.paymentId
Related
Sorry I don't know how to word the question's title.
I have a table like this
Prod Part Number
0001 101 3
0001 102 2
0001 103 1
0002 101 3
0002 102 2
0002 103 4
0003 101 2
0003 102 3
0003 103 6
0004 101 3
0004 102 2
0004 103 1
I want to find the product that has the correct number per part for all parts.
So something like
SELECT Prod from table
WHERE (Number of Part(101)) = 3
AND (Number of Part(102)) = 2
AND (Number of Part(103)) = 1
Output would be:
Prod
0001
0004
How can I achieve that?
You could use some inner join
select t1.Prod from
( select Prod
from my_table
where part = 101
and Number = 3 ) t1
inner join
( select Prod
from my_table
where part = 102
and Number = 2 ) t2 on t1.Prod = t2.Prod
inner join
( select Prod
from my_table
where part = 103
and Number = 1 ) t3 on t1.Prod = t3.Prod
http://sqlfiddle.com/#!9/014fe4/8
Through conditional aggregation:
SELECT Prod,
SUM(CASE WHEN Part = 101 AND Number = 3 OR
Part = 102 AND Number = 2 OR
Part = 103 AND Number = 1
THEN 1 ELSE 0 END) sum
FROM tbl
GROUP BY Prod
HAVING sum = 3
This will give you an extra column. If it's a problem then wrap everything into another SELECT.
I am stuck with below logic in sql server.
Table 1:
ID Requestid
1 0001
2 0004
3 0004
1 0005
Table 2
parentID Requestid Age
1 0001 29
2 0004 30
3 0004 34
1 0005 27
query:
select * from table1 t1
join table t2
on t2.parentid =t1.id
When I join these tables, I am getting below result
ID requestid age
1 0001 29
1 0005 29
2 0004 30
3 0004 34
1 0001 27
1 0005 27
I want below result:
ID requestid age
1 0001 29
1 0005 27
2 0004 30
3 0004 34
I know it is simple and i am missing something.
Any help is appreciated!
select ID, requestid, age from table1 t1
inner join table t2
on t2.parentid =t1.id AND t2.requestId = t1.requestId
ORDER BY ID
OR
select ID, requestid, age from table1 t1,table t2
where t2.parentid =t1.id AND t2.requestId = t1.requestId
ORDER BY ID
Say for example, I have a table that looks like this
ID NAME YREF UREF
1 ADAM BLAISE 0001 0007
2 JAMES HARDY 0002 0005
3 PHILIP HENRY 0003 0002
4 PETER SCHWARTZ 0004 0001
5 WILL MADISON 0005 0002
6 JEREMY PINOT 0006 0002
7 JOHN WILLIARD 0007 0007
8 MARK WILLIARD 0008 0005
9 JOHN VAUGHN 0009 0002
10 DAVID JAMES 0010 0002
I want to be able to get a count for each row how many times each user sent their YREF to another user so that the said user referenced it when submitting their own names, so that a table that looks like this can be got:
ID NAME YREF COUNT (i.e. how many times their YREF appeared in the UREF column of other users)
1 ADAM BLAISE 0001 1
2 JAMES HARDY 0002 5
3 PHILIP HENRY 0003 0
4 PETER SCHWARTZ 0004 0
5 WILL MADISON 0005 2
6 JEREMY PINOT 0006 0
7 JOHN WILLIARD 0007 2
8 MARK WILLIARD 0008 0
9 JOHN VAUGHN 0009 0
10 DAVID JAMES 0010 0
select t1.id, t1.name, t1.yref, coalesce(t2.cnt, 0) as count
from your_table t1
left join
(
select uref, count(*) as cnt
from your_table
group by uref
) t2 on t1.yref = t2.uref
Generate a set of data with counts and join it back to the base set. other database engines would allow windowed sets using OVER syntax, but mySQL doesn't support these.
SELECT A.ID, A.Name, A.YREF, coalesce(B.MyCount,0)
FROM tableName A
LEFT JOIN (SELECT count(*) myCount, UREF
FROM tableName
GROUP BY UREF) B
on A.YREF = B.UREF
I want to be able to get all the data from table 1 and table 3 below but in addition to this I also want to get the latest application stage from table 2. The latest application stage is determined by getting the max stage_date for each application.
Table 1: applications
id | applicant_id | col_x | col_y | col_z
-----------------------------------------
10 300 a b c
11 310 a b c
12 320 a b c
13 330 a b c
14 340 a b c
Table 2: application_progress
id | application_id | application_stage | stage_date | stage_notes
------------------------------------------------------------------
1 10 DRAFT 2013-01-01 (NULL)
2 10 APPLICATION 2013-01-14 (NULL)
3 10 PHASE1 2013-01-30 (NULL)
4 11 DRAFT 2013-01-01 (NULL)
4 12 DRAFT 2013-01-01 (NULL)
5 13 DRAFT 2013-01-01 (NULL)
6 14 DRAFT 2013-01-01 (NULL)
7 14 APPLICATION 2013-01-14 (NULL)
EDIT: third table
Table 3: applicants
id | applicant_name | applicant_address | programme_id
------------------------------------------------------
300 Applicant 1 abc 1
310 Applicant 2 xyz 2
320 Applicant 3 xyz 2
330 Applicant 4 xyz 2
340 Applicant 5 xyz 2
Returned data set
applicant_id | applicant_name | current_stage
---------------------------------------------------------
300 Applicant 1 PHASE1
310 Applicant 2 DRAFT
320 Applicant 3 DRAFT
330 Applicant 4 DRAFT
340 Applicant 5 APPLICATION
Am struggling with this one and would appreciate any help.
PS. Tried to put an example of sqlfiddle but it's down at the minute. I'll update this with the sqlfiddle when it's back up if haven't had an answer before this.
You can do this with a correlated subquery:
select a.*,
(select application_stage
from application_progress ap
where ap.application_id = a.id
order by stage_date desc
limit 1
) MostRecentStage
from applications a;
EDIT:
You can joining in the applicant data with something like this::
select a.*, aa.*,
(select application_stage
from application_progress ap
where ap.application_id = a.id
order by stage_date desc
limit 1
) MostRecentStage
from applications a join
applicant aa
on a.applicant_id = aa.id;
I have two different tables:
checkin_out consists of two fields: emp_code, checked_date
temp_days consists of two fields: id, date_value
Table checkin_out has following data:
emp_code | checked_date
-----------------------
001 2012-11-01
001 2012-11-02
001 2012-11-03
002 2012-11-01
003 2012-11-01
003 2012-11-02
While table temp_days has following data:
id | date_value
-----------------
1 2012-11-01
2 2012-11-02
3 2012-11-03
4 2012-11-04
5 2012-11-05
From the above tables, I need to show the missing dates in the table temp_days; I need to query to get a result as follow:
emp_code | date_value
-----------------------
001 2012-11-04
001 2012-11-05
002 2012-11-02
002 2012-11-03
002 2012-11-04
002 2012-11-05
003 2012-11-03
003 2012-11-04
003 2012-11-05
If anyone could help, please! Thanks!
The below works for a more complex data set with multiple emp_codes.
SELECT alldays.emp_code, alldays.date_value
FROM (
SELECT date_value, emp_code
FROM temp_days
CROSS JOIN checkin_out
GROUP BY date_value, emp_code
) alldays
LEFT JOIN checkin_out C
ON alldays.date_value = C.checked_date
AND alldays.emp_code = C.emp_code
WHERE C.emp_code IS NULL
SELECT c.emp_code, a.date_value
FROM temp_days a
LEFT JOIN checkin_out b
ON a.date_value = b.checked_date
CROSS JOIN
(
SELECT emp_code
FROM checkin_out
GROUP BY emp_code
) c
WHERE b.emp_code IS NULL
SQLFiddle Demo