I have a very nasty query. I try to get a classification table sorted by touchdowns... But before reaching that goal I have a problem with a sum... let me explain.
With this query:
SELECT tft.id_team
, z.id_partido
, y.td
FROM `team_faseTorneo` tft
LEFT
JOIN
( SELECT j.id_fase
, tp.id_team
, tp.id_partido
FROM jornadas j
LEFT
JOIN partidos USING(id_jornada)
JOIN team_partido tp USING(id_partido)
) z
ON z.id_fase = tft.id_fase
AND z.id_team=tft.id_team
LEFT
JOIN
( SELECT p.id_team
, pL.id_partido
, pL.td
FROM players p
JOIN playerLogros pL USING(id_player)
) y
ON y.id_team = tft.id_team
AND y.id_partido = z.id_partido
WHERE tft.id_fase = 4
The result is this one:
id_team id_partido td
6 69 0
6 69 0
6 69 0
6 69 0
6 81 0
6 81 0
6 81 0
6 81 1
6 84 1
6 84 0
6 98 NULL
6 99 NULL
6 114 NULL
6 116 NULL
6 129 NULL
6 133 NULL
6 144 NULL
6 150 NULL
6 159 NULL
6 167 NULL
19 72 0
19 72 0
19 77 2
19 77 0
19 77 1
19 77 0
19 77 0
19 89 0
19 89 0
19 92 NULL
NUll is for the matchs(partidos) not played yet. I need to sum all the touchdowns scored by match (partido), so I tried this:
SELECT tft.id_team,z.id_partido, y.td FROM `team_faseTorneo` tft
LEFT JOIN (SELECT j.id_fase, tp.id_team, tp.id_partido FROM jornadas j LEFT JOIN partidos USING(id_jornada) JOIN team_partido tp USING(id_partido)
) z ON(z.id_fase=tft.id_fase AND z.id_team=tft.id_team) LEFT JOIN(SELECT p.id_team, pL.id_partido, SUM(pL.td) td FROM players p JOIN playerLogros pL USING(id_player) GROUP BY p.id_team) y ON(y.id_team=tft.id_team AND y.id_partido=z.id_partido)
WHERE tft.id_fase=4
But the result is this:
id_team id_partido td
6 69 NULL
6 81 NULL
6 84 NULL
6 98 NULL
6 99 NULL
6 114 NULL
6 116 NULL
6 129 NULL
6 133 NULL
6 144 NULL
6 150 NULL
6 159 NULL
6 167 NULL
19 72 3
19 77 NULL
19 89 NULL
19 92 NULL
19 106 NULL
How Can I solve this?
Thanks in advance!! :)
If you need a sum, then use an aggregation query. Perhaps something like this:
SELECT tft.id_team, z.id_partido, SUM(y.td)
FROM `team_faseTorneo` tft LEFT JOIN
(SELECT j.id_fase, tp.id_team, tp.id_partido
FROM jornadas j LEFT JOIN
partidos
USING (id_jornada) JOIN
team_partido tp
USING (id_partido)
) z
ON z.id_fase=tft.id_fase AND z.id_team=tft.id_team LEFT JOIN
(SELECT p.id_team, pL.id_partido, pL.td td
FROM players p JOIN
playerLogros pL
USING (id_player)
) y
ON y.id_team=tft.id_team AND y.id_partido=z.id_partido
WHERE tft.id_fase = 4
GROUP BY tft.id_team, z.id_partido;
Your query seems overly complicated, but this should at least get you the sum of td.
I think that I got it...
SELECT tft.id_team,z.id_partido, sum(z.td) FROM `team_faseTorneo` tft
LEFT JOIN (SELECT j.id_fase, tp.id_team, tp.id_partido, y.td FROM jornadas j LEFT JOIN partidos p USING(id_jornada) JOIN team_partido tp USING(id_partido) LEFT JOIN(SELECT p.id_team, pL.id_partido, (pL.td) td FROM players p JOIN playerLogros pL USING(id_player) ) y ON(y.id_team=tp.id_team AND y.id_partido=tp.id_partido) WHERE p.status='Finalizado'
) z ON(z.id_fase=tft.id_fase AND z.id_team=tft.id_team)
WHERE tft.id_fase=4 GROUP BY tft.id_team, z.id_partido ORDER BY tft.id_team
Related
I have 2 tables called cash and check
CASH
cstm_id
cash_date
cash_amount
101
20220529
50
101
20220530
100
102
20220601
50
102
20220603
75
CHECK
cstm_id
check_date
check_amount
101
20210525
150
101
20210812
25
102
20220210
25
102
20220603
20
I want to join the tables so I have unique values in each row without any duplication
EXPECTED RESULT:
cstm_id
cash_date
cash_amount
check_date
check_amount
101
20220529
50
null
null
101
20220530
100
null
null
101
null
null
20210525
150
101
null
null
20210812
25
102
20220601
50
null
null
102
null
null
20210110
25
102
20220603
75
20220603
20
What would be the most appropriate sql script for this output
One approach can be via full outer join -
select distinct * from (
select c.*, c1.check_date, c1.check_amount
from cash c left join check1 c1
on c.cstm_id=c1.cstm_id
and c.cash_date = c1.check_date
union all
select c1.cstm_id,c.cash_date,c.cash_amount, c1.check_date, c1.check_amount
from cash c right join check1 c1
on c.cstm_id=c1.cstm_id
and c.cash_date = c1.check_date
) t;
Fiddle.
I want to join two tables in a database. My first table looks like
deviceId rssi date timestamp f po pps counter b Id f
fd00::212:4b00:1957:ce2f 60 10-12-2019 00:00:08 0 100 26.36000061 3348 B2W 39 L1
fd00::212:4b00:1957:ce2f 60 10-12-2019 00:00:18 0 100 25.62000084 3349 B2W 41 L1
fd00::212:4b00:1957:ce2f 60 10-12-2019 00:00:28 0 0 24.29999924 3350 B2W 46 L1
fd00::212:4b00:1957:ce2f 60 10-12-2019 00:00:38 0 100 24.48999977 3351 B2W 48 L1
and my second table looks like
id date timestamp T hum temperature co r b block f location
fd00::212:4b00:1957:d625 10-12-2019 08:24.6 0 6618 2648 1454 99 null B2E L1 11
fd00::212:4b00:1957:d626 10-12-2019 13:23.2 0 6671 2630 1455 73 null B2E L1 12
fd00::212:4b00:1957:d627 10-12-2019 18:25.5 0 6681 2627 1456 73 null B2E L1 13
fd00::212:4b00:1957:d628 10-12-2019 23:23.3 0 6691 2626 1457 73 null B2E L1 14
fd00::212:4b00:1957:d625 10-12-2019 28:24.1 0 6701 2626 1458 73 null B2E L1 11
fd00::212:4b00:1957:d626 10-12-2019 33:24.1 0 6701 2626 1459 73 null B2E L1 12
There are the two tables which i want to join on Id from first table and location from second table. There is seperate mapping between Id and location. Like
Id location
39 11
41 12
46 13
48 14
I have this mapping separately in an excel file. I could use a inner join if the columns from both the tables are same.How should I join these tables when there is an external mapping between the columns to join. Can some one help me with this.
Thanks
id serviceid name cost date
201 15 X 50 25.12.2016
201 15 Y 55 29.11.2016
201 120 Z 50 27.11.2016
201 19 w 50 22 .11.2016
201 158 p 50 23.11.2016
201 18 q 50 21.11.2016
201 16 rs 50 24.11.2016
201 81 rs 50 2.11.2016
202 18 X 50 25.12.2016
202 18 Y 55 29.11.2016
202 15 Z 50 27.11.2016
202 19 w 50 22 .11.2016
203 15 p 50 23.11.2016
203 18 q 50 21.11.2016
203 16 rs 50 24.11.2016
0 81 rs 50 2.11.2016
Desire Output :
id serviceid name cost date
201 15 X 50 25.12.2016
201 15 Y 55 29.11.2016
201 120 Z 50 27.11.2016
201 16 rs 50 24.11.2016
202 18 X 50 25.12.2016
202 18 Y 55 29.11.2016
202 15 Z 50 27.11.2016
202 19 w 50 22 .11.2016
203 15 p 50 23.11.2016
203 18 q 50 21.11.2016
203 16 rs 50 24.11.2016
0 81 rs 50 2.11.2016
i want to display each record 4 - 4 record service for each id i am trying to apply using self Join but there is Problem coming please tell or suggest me how to ac-chive for this.
SELECT a.*
FROM mytable AS a
WHERE
(SELECT COUNT(*) FROM mytable AS b
WHERE b.id = a.id and b.serviceid >= a.serviceid) <= 4
ORDER BY a.id , a.date
this query am trying but i am unable to fetch it for each id there should top 4 service id based on date.
To make your desired result, I think you should use mysql variable like this:
select
t1.*
from mytable t1
join (
select
`id`,
`serviceid`,
`name`,
`cost`,
`date`,
#rowno := case when #grp = `id` then #rowno + 1 else 1 end as rowno,
#grp := `id`
from mytable
cross join (select #rowno:=0, #grp:=null) v
order by `id`, `date` desc) t2
on t1.id = t2.id
and t1.`date` = t2.`date`
and t2.rowno < 5
and this seems to be a classic top x record in each group issue, take a look of How to select the first/least/max row per group in SQL
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?
post_id organisation_id
1 5
2 57
3 57
4 57
5 58
6 59
7 60
8 61
8 62
9 59
11 57
12 57
4 62
is there anyway to select post_id, organisation_id1, organisation_id2 in a single row?
Since you didn't specify what your table looks like and what would be the outcome that you would like, this is just a guess.
SELECT
a.post_id,
a.organisation_id,
b.organisation_id
FROM your_table a
LEFT JOIN your_table b
ON a.post_id = b.post_id AND a.organisation_id < b.organisation_id
Is this what you're looking for?
select post_id, group_concat(organisation_id)
from YourTable
group by post_id;