Declaring a variable in MySQL with multiple JOIN tables - mysql

I am trying to get a resulting column of the initial month an ID was created where multiple table JOINs are needed in MySQL Workbench.
SET #in_month = '0';
SELECT
ca.id
FROM capital.user ca
JOIN
capital.user_account cd on ca.id = cd.user_id
JOIN
capital.transaction ct on cd.user_id = ct.user_account_id
JOIN
capital.transaction_event ce on ct.id = ce.auth_entry_id
#in_month = month(ce.created) WHERE ce.message = 'Approved'
Group by id;
I get Syntax error: '#in_month' (at text suffix) is not valid input at this position on line 17, any ideas of what I might be doing wrong? I don't have a lot of experience with SQL

you are missing semicolon after variable declaration & and on join condition
SET #in_month = '0';
SELECT
ca.id
FROM capital.user ca
JOIN capital.user_account cd on ca.id = cd.user_id
JOIN capital.transaction ct on cd.user_id = ct.user_account_id
JOIN capital.transaction_event ce on ct.id = ce.auth_entry_id and
#in_month = month(ce.created)
WHERE ce.message = 'Approved'
Group by id;

Related

how to join 10 table by using inner joins and group by condition

i want to convert this query into joins , this is a task which i got from my manger that Please convert the query below into Join format.
SELECT
capder.cder_id,
capman.cman_name AS Manufacturer,
caprange.cran_name AS Range1,
capmod.cmod_name AS Model,
capmod.cmod_code AS ModelCode,
capder.cder_doors AS Doors,
nvdbodystyle.bs_description AS BodyStyle,
captrim.ctrim_name AS Trim1,
capder.cder_name as Trim2,
capder.cder_drivetrain AS DriveTrain,
nvdmodelyear.MY_ref AS DerivativeModelYear,
group_concat(distinct(nvddictonaryoption.DO_Description) order by nvddictonaryoption.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman,
caprange,
capmod,
nvdbodystyle,
capder,
nvdmodelyear,
captrim,
nvdoptions,
nvddictonaryoption,
nvddictionarycategory
WHERE capman.cman_code = caprange.cran_mantextcode
AND caprange.cran_code = capmod.cmod_rancode
AND nvdbodystyle.bc_code = capmod.cmod_bodystyle
AND capmod.cmod_code = capder.cder_modcode
AND capder.cder_id = nvdmodelyear.my_id
AND captrim.ctrim_code = capder.cder_trimcode
AND nvdoptions.OPT_Id = capder.cder_id
AND nvdoptions.opt_optioncode = nvddictonaryoption.DO_OptionCode
AND nvddictionarycategory.dc_catcode = nvddictonaryoption.do_catcode
AND nvddictionarycategory.dc_cth_type = 'C'
AND SUBSTR(nvdmodelyear.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nvdmodelyear.MY_ref,1,4),captrim.ctrim_name
MySQL workbench 6.3 CE.
i tried this way
SELECT
cd.cder_id,
cm.cman_name AS Manufacturer,
cr.cran_name AS Range1,
cmd.cmod_name AS Model,
cmd.cmod_code AS ModelCode,
cd.cder_doors AS Doors,
nb.bs_description AS BodyStyle,
ct.ctrim_name AS Trim1,
cd.cder_name as Trim2,
cd.cder_drivetrain AS DriveTrain,
nmy.MY_ref AS DerivativeModelYear,
group_concat(distinct(ndo.DO_Description) order by ndo.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman cm
inner join caprange cr on cm.cman_code = cr.cran_mantextcode
inner join capmod cmd on cr.cran_code = cmd.cmod_rancode
inner join nvdbodystyle nb on nb.bc_code = cmd.cmod_bodystyle
inner join capder cd on cmd.cmod_code = cd.cder_modcode
inner join nvdmodelyear nmy on cd.cder_id = nmy.my_id
inner join captrim ct on ct.ctrim_code = cd.cder_trimcode
inner join nvdoptions nop on nop.OPT_Id = cd.cder_id
inner join nvddictonaryoption ndo on nop.opt_optioncode = ndo.DO_OptionCode
inner join nvddictionarycategory ndc on ndc.dc_catcode = ndo.do_catcode AND ndc.dc_cth_type = 'C' AND SUBSTR(nmy.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nmy.MY_ref,1,4),ct.ctrim_name;
am i did correct? it is giving correct result but it is taking too long to execute and get results. is there any better way to execute this query only with joins syntax and i don't have access to put index to the table

SSRS: Parameter not filtering MySQL query

I have a Date/Time parameter to my report:
But when I run my query, I get no results:
SELECT HD_QUEUE.NAME as qname, HD_TICKET.ID, HD_TICKET.CREATED, HD_TICKET.TIME_CLOSED, CUSTOMER.FULL_NAME as custfullname,
HD_STATUS.NAME as statname, HD_TICKET.TITLE, left(ASSIGNEE.FULL_NAME, 40) as assignee,
HD_PRIORITY.NAME as pname, HD_CATEGORY.NAME as catname
FROM HD_TICKET
INNER JOIN HD_QUEUE
ON HD_TICKET.HD_QUEUE_ID = HD_QUEUE.ID
INNER JOIN USER CUSTOMER
ON HD_TICKET.SUBMITTER_ID=CUSTOMER.ID
INNER JOIN USER ASSIGNEE
ON HD_TICKET.OWNER_ID=ASSIGNEE.ID
INNER JOIN HD_STATUS
ON (HD_TICKET.HD_STATUS_ID=HD_STATUS.ID)
AND (HD_TICKET.HD_QUEUE_ID=HD_STATUS.HD_QUEUE_ID)
INNER JOIN HD_PRIORITY
ON HD_TICKET.HD_PRIORITY_ID = HD_PRIORITY.ID
and HD_TICKET.HD_QUEUE_ID = HD_PRIORITY.HD_QUEUE_ID
INNER JOIN HD_CATEGORY
ON HD_TICKET.HD_CATEGORY_ID = HD_CATEGORY.ID
and HD_TICKET.HD_QUEUE_ID = HD_CATEGORY.HD_QUEUE_ID
left join ASSET on ASSET.ID = HD_TICKET.ASSET_ID
left join ASSET_DATA_6 on ASSET.ASSET_DATA_ID = ASSET_DATA_6.ID
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < #date_param);
What am I doing wrong?
MySQL does not allow named parameters. Use '?' instead of '#date_param' in the query.
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < ?;
Then check the Dataset Properties and make sure the '?' is associated with the value of your parameter:

How can I update filed value from a select with inner join?

I am trying to update a field value in a mysql database using a select query with inner join
I currently get
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS cc WHERE cc.account_id = na.account_id' at line 5
UPDATE accounts AS na
SET na.pdm_id = (
SELECT cp.person_id FROM `temp_accounts` AS ta INNER JOIN call_managment_system.accounts AS a ON ta.company_code = a.company_code
INNER JOIN contact_personal AS cp ON cp.name = ta.FSM AND contact_link = 'PDM'
)
WHERE a.account_id = na.account_id
How can I fix this query to work? I want to update the field called pdm_id to set it equal to cp.person_id
Thanks
UPDATE accounts na
INNER JOIN call_managment_system.accounts a
ON a.account_id = na.account_id
INNER JOIN temp_accounts ta
ON ta.company_code = a.company_code
INNER JOIN contact_personal cp
ON cp.name = ta.FSM
SET na.pdm_id = cp.person_id
WHERE contact_link = 'PDM'

Unknown Column id in MySQL Query

Can someone help me I've got 2 MySQL queries that get unknown column id when I try to run them. I might add that I am converting this database from SQLServer 2005 to MySQL and they run fine in SQL Server 2005.
Here's 1 of them:
SELECT DISTINCT g.id AS `genre`
FROM media_playlist_sequence MPS
INNER JOIN media M ON M.`key` = MPS.media_key
INNER JOIN media_playlists MP ON MP.`key` = MPS.playlist_key
INNER JOIN node_media_playlist NMP ON NMP.playlist_key = MP.`key`
INNER JOIN nodes N ON N.`key` = NMP.node_key
INNER JOIN media_files MF ON MF.media_key = M.`key`
INNER JOIN media_locations ML ON ML.media_file_key = MF.media_file_key
AND ML.node_key = n.`key`
INNER JOIN media_genres MG ON MG.media_key = M.`key`
INNER JOIN genres G ON G.`key` = MG.genre_key
WHERE M.is_ready = 1
AND MP.id = 'Channels'
AND N.id = 'VIC-WIN7'
AND mf.is_quad_image = 0
My guess is that it's a case sensitivity issue. MySQL can be case sensitive by default whereas SQL Server is not.

SQL calculating time between assignments

I have to write an SQL statement which contain a field that contain two different values consecutively but in the way I have wrote it, it return always null because it is interpreted as having the two value in the same time!
My conditions should be : (ci.field = 'Group' and ci.oldString = 'Triage' ) and (ci.field='assignee' and ci.newString is not NULL)
That means calculate time between: when the issue is assigned to group named Triage and when the issue is assigned to a person.
How can I fix it?
My SQL statement:
select TIMEDIFF(a.created,b.created)
from
(select g.created, g.issueid as groupid1
from changegroup g
join changeitem ci on (ci.groupid = g.id)
join jiraissue ji on (ji.id = g.issueid)
join project p on (p.id = ji.project)
join priority pr on (pr.id = ji.priority)
where ci.field = 'Group'
and ci.oldString = 'Triage'
and ci.field='assignee'
and ci.newString is not NULL
and p.pname = 'Test'
and pr.pname='P1'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) a
left join (
select ji.created, ji.id as groupid2
from jiraissue ji
join changegroup g on (g.issueid = ji.id)
join project p on (p.id = ji.project)
where p.pname = 'Test'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) b ON (a.groupid1 = b.groupid2);
This is the table from which I should retrieve data
See my comment about the quality of your question but a hint at how to solve this goes like (assuming you can make sure this doesn't create 1-n joins)
select groupid_orsomething_else, TIMEDIFF(a.created, b.created)
from yourtable
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'Group' and oldstring is 'Triage'
) a
on a.groupid_orsomething_else = yourtable.groupid_orsomething_else
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'assignee' and oldstring is null) b
on b.groupid_orsomething_else = yourtable.groupid_orsomething_else