temporay table update mysql - mysql

I am migrating my stored procedures from Sql Server to MySql.
Somewhere I am facing problem to transform update queryfrom MS sql server to Mysql for temporary variable. Here is my MS sql server query:
update sampleStoreTV set felony = c.fel,misdemeanor = c.mis,violation = c.vio
from (select srcCL.id as 'storeId',
case when SUM(case when srcCL.cl='FELONY' then 1 else 0 end)>0 then 1 else 0 end as 'fel',
case when SUM(case when srcCL.cl='MISDEMEANOR' then 1 else 0 end)>0 then 1 else 0 end as 'mis',
case when SUM(case when srcCL.cl='VIOLATION' then 1 else 0 end)>0 then 1 else 0 end as 'vio'
from
(select DM_Sample_Store.DM_Sample_Store_ID as 'id',
Charge_Level.Mapped_Charge_Level as 'cl'
from DM_Sample_Store
left join screening_result on Screening_Result.Screening_Result_id = DM_Sample_Store.Screening_Result_id
left join Crim_Case on Screening_Result.Screening_Result_id = Crim_Case.Screening_Result_id
left join Crim_Charge on Crim_Case.Crim_case_id = Crim_Charge.Crim_case_id
left join PDL_DataSource on DM_Sample_Store.DataSource_Id = PDL_DataSource.DataSource_Id
left join DM_Sample_Search_Param on DM_Sample_Store.DM_Sample_Store_ID = DM_Sample_Search_Param.DM_Sample_Store_ID
left join Charge_Level on Crim_Charge.Sanitized_Charge_Level_id = Charge_Level.Charge_Level_id
where PDL_DataSource.DataSource_Id = dataSourceId
and DM_Sample_Store.Active = 1
and DM_Sample_Search_Param.Global_Search_Parameter_ID in (sosGspId, freqGspId, clGspId, dtGspId)
and DM_Sample_Search_Param.Active = 1
-- and Crim_Charge.AOR_Days is not null
) srcCL
group by id
having SUM(case when cl='FELONY' then 1 else 0 end)>0
or SUM(case when cl='MISDEMEANOR' then 1 else 0 end)>0
or SUM(case when cl='VIOLATION' then 1 else 0 end)>0) c
where sampleId = c.storeId

I am not sure what youre trying to do but your query is completely wrong for many different reasons.
You need to join your sampleStoreTV with your other tables. I guess it might be something like that:
update sampleStoreTV
inner join (your query) as c on sampleStoreTV.sampleId = c.storeId
set sampleStoreTV.felony = c.fel,
sampleStoreTV.misdemeanor = c.mis,
sampleStoreTV.violation = c.vio
I hope it helps

Related

Please How to fix "Invalid use of group function"

I try to use nested CASE WHEN syntax in SUM() function but nothings works since 3 days
When I retrieve all sum and nested conditions the code work great
I explain:
Table contrat: Each client have a contract in which this two values: Prix_TTC (the price of the article), Qac_cont (The quantity of the article client must consume each month)
Table commande: Each client order article with quantity (Quantite_cmd)
So what I try to do is to get all financial loss for each client during the actual month and SUM all these amount
SELECT
SUM(pertes) AS pertes_reelles
FROM
(SELECT
c.ID_clt,
SUM(CASE
WHEN
((co.Qac_cont - SUM(CASE
WHEN Quantite_cmd IS NULL THEN 0
ELSE Quantite_cmd
END)) * - 1) < 0
THEN
((co.Qac_cont - SUM(CASE
WHEN Quantite_cmd IS NULL THEN 0
ELSE Quantite_cmd
END)) * - 1)
ELSE 0
END) * co.Prix_TTC AS pertes
FROM
clients c
LEFT JOIN contrat co ON co.clt_ID = c.ID_clt
LEFT JOIN commande cmd ON cmd.clt_ID = c.ID_clt
AND MONTH(DATE(cmd.date_livr_cmd)) = MONTH(DATE('2019-08-30 09:23:23'))
WHERE
c.Etat_clt <> 'D'
GROUP BY c.ID_clt) AS liste
Like #BobJarvis says, the problem was with the nested SUM so I have broken down nested processing and it WORKS Thanks ! the new request :
SELECT SUM(CASE
WHEN
perte < 0
THEN
perte * - 1
ELSE 0
END) AS pertes_reelles
FROM
(SELECT
c.ID_clt, co.Prix_TTC, ((co.Qac_cont - SUM(CASE
WHEN Quantite_cmd IS NULL THEN 0
ELSE Quantite_cmd
END)) * - 1) as ecart, co.Prix_TTC * ((co.Qac_cont - SUM(CASE
WHEN Quantite_cmd IS NULL THEN 0
ELSE Quantite_cmd
END)) * - 1) as perte
FROM
clients c
LEFT JOIN contrat co ON co.clt_ID = c.ID_clt
LEFT JOIN commande cmd ON cmd.clt_ID = c.ID_clt
AND MONTH(DATE(cmd.date_livr_cmd)) = MONTH(DATE('".$today."'))
WHERE
c.Etat_clt <> 'D'
GROUP BY c.ID_clt) AS liste

SQL query to DQL

I have a big query and I want to add a subquery to get the availability of accommodation-rooms.
This is the SQL subquery:
,(SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as available
FROM
(
SELECT count(rtd.room_type_id) as _days
FROM room_type_day as rtd
WHERE rtd.date IN ('2018-06-20', '2018-06-21', '2018-06-22')
GROUP BY rtd.room_type_id
HAVING COUNT(rtd.room_type_id) = 3
) as sub) as availability
Can anyone tell me how to convert this SQL in DQL?
Thak you
EDIT
I try with this changes but the response is null every time:
,(SELECT (CASE WHEN count(rtd.roomType) > 0 THEN 'yes' ELSE 'no' END)
FROM AppBundle:RoomTypeDayCancelationConditionAccommodation as RTDCCA2
LEFT JOIN RTDCCA2.roomTypeDay as rtd
WHERE rtd.date IN ('2018-06-20', '2018-06-21', '2018-06-22')
GROUP BY rtd.roomType
HAVING COUNT(rtd.roomType) = 3
) as disponible

joining 3 or more table and sum the fields

I would like to join 3 tables.
The result is one of the field - SUM from the other table like this image, please help
You don't need to join tblwork as you can get all required fields from other 2 tables.
Following query should work:
select t1.nmstudent,
sum(case when t2.idwork = 'w001' then t2.trprice else 0 end) as w001,
sum(case when t2.idwork = 'w002' then t2.trprice else 0 end) as w002,
sum(case when t2.idwork = 'w003' then t2.trprice else 0 end) as w003,
sum(case when t2.idwork = 'w004' then t2.trprice else 0 end) as w004
from tblstudent t1
inner join tblTrans t2
on t1.idstudent = t2.idstudent
group by t1.idstudent;
Hope it helps!

Appending data from different tables - mysql

I have the following tables Job description and Candidate.
Each job description can be related to n candidates. The candidates are obtained from different sources(a, b, c d) - candidate.source.
I want a single query to list me the JD ids, with count of candidates for each source for each JD, as shown below:
JD id | candidate name | count of candidates - source a | count of candidates - source b | count of candidates - source | c..............
Try using the query as a template:
select
JobDescriptionName,
SUM(ACount) CountOfCandidatesOfA ,
SUM(BCount) CountOfCandidatesOfB,
SUM(CCount) CountOfCandidatesOfC ,
SUM(DCount) CountOfCandidatesOfD
from
( select JobDescriptionID, (CASE WHEN Source = 'a' THEN 1 ELSE 0 END) AS ACount,
(CASE WHEN Source = 'b' THEN 1 ELSE 0 END) AS BCount,
(CASE WHEN Source = 'c' THEN 1 ELSE 0 END) AS CCount,
(CASE WHEN Source = 'd' THEN 1 ELSE 0 END) AS DCount
from Candidate) AS DerivedCandidate
inner join JobDescription ON JobDescription.JobDescriptionID = DerivedCandidate.JobDescriptionID group by JobDescriptionID;
I know there is already an accepted answer but in the effort of learning myself more about SQL here is an alternative way applying the case directly in the initial select without the sub-select:
Select
jd.id,
jd.name,
sum(case when c.source = 'a' then 1 else 0 end) as sourceA,
sum(case when c.source = 'b' then 1 else 0 end) as sourceB,
sum(case when c.source = 'c' then 1 else 0 end) as sourceC,
sum(case when c.source = 'd' then 1 else 0 end) as sourceD
from JobDescription as jd
join Candidate as c on c.jobId = jd.id
group by jd.id, jd.name
SQL Fiddle Demo
SELECT JD id, COUNT(Candidate), source
FROM table1
GROUP BY JD id,source

How to use user variable as counter with inner join queries that contains GROUP BY statement?

I have 2 tables odds and matches :
matches : has match_id and match_date
odds : has id, timestamp, result, odd_value, user_id, match_id
I had a query that get the following information from those tables for each user:
winnings : the winning bets for each user. (when odds.result = 1)
loses : the lost bets for each user.(when odds.result != 1)
points : the points of each user.(the sum of the odds.odd_value) for each user.
bonus : for each continuous 5 winnings i want to add extra bonus to this variable. (for each user)
How to calculate bonus?
I tried to use this query and I faced a problem : (you can check it here SQL Fiddle)
the calculated bonus are not right for all the users :
first user:(winnings:13, bonus=2).
second user:(winnings:8, bonus=2)bonus here should be 1.
third user:(winnings:14, bonus=3)bonus here should be 2.
why does the query not calculate the bonus correctly?
select d.user_id,
sum(case when d.result = 1 then 1 else 0 end) as winnings,
sum(case when d.result = 2 then 1 else 0 end) as loses,
sum(case when d.result = 1 then d.odd_value else 0 end) as points,
f.bonus
FROM odds d
INNER JOIN
(
SELECT
user_id,SUM(CASE WHEN F1=5 THEN 1 ELSE 0 END) AS bonus
FROM
(
SELECT
user_id,
CASE WHEN result=1 and #counter<5 THEN #counter:=#counter+1 WHEN result=1 and #counter=5 THEN #counter:=1 ELSE #counter:=0 END AS F1
FROM odds o
cross join (SELECT #counter:=0) AS t
INNER JOIN matches mc on mc.match_id = o.match_id
WHERE MONTH(STR_TO_DATE(mc.match_date, '%Y-%m-%d')) = 2 AND
YEAR(STR_TO_DATE(mc.match_date, '%Y-%m-%d')) = 2015 AND
(YEAR(o.timestamp)=2015 AND MONTH(o.timestamp) = 02)
) Temp
group by user_id
)as f on f.user_id = d.user_id
group by d.user_id
I am not sure how your result related to matches table,
you can add back WHERE / INNER JOIN clause if you need.
Here is link to fiddle
and the last iteration according to your comments:
And here is a query:
SET #user:=0;
select d.user_id,
sum(case when d.result = 1 then 1 else 0 end) as winnings,
sum(case when d.result = 2 then 1 else 0 end) as loses,
sum(case when d.result = 1 then d.odd_value else 0 end) as points,
f.bonus
FROM odds d
INNER JOIN
(
SELECT
user_id,SUM(bonus) AS bonus
FROM
(
SELECT
user_id,
CASE WHEN result=1 and #counter<5 AND #user=user_id THEN #counter:=#counter+1
WHEN result=1 and #counter=5 AND #user=user_id THEN #counter:=1
WHEN result=1 and #user<>user_id THEN #counter:=1
ELSE
#counter:=0
END AS F1,
#user:=user_id,
CASE WHEN #counter=5 THEN 1 ELSE 0 END AS bonus
FROM odds o
ORDER BY user_id , match_id
) Temp
group by user_id
)as f on f.user_id = d.user_id
group by d.user_id