I have a query like this -
SELECT e.id,
( (SELECT ABS((SELECT YEAR(NOW()) - YEAR(ud.dob)
FROM user_detail ud
WHERE ud.userid = 49) - AVG(avgr.abc
+ (SELECT YEAR(NOW()) - YEAR(ud.dob)
FROM user_detail ud
WHERE ud.userid = 49))) AS VALUE
FROM (SELECT YEAR(NOW()) - YEAR(ud.dob) AS abc
FROM user_detail ud
WHERE ud.userid = (SELECT ei.interested_user
FROM event_interest ei
WHERE ei.eventid = e.id
AND ei.approvalstatus = 'Approve')
UNION
SELECT YEAR(NOW()) - YEAR(ud.dob) AS abc
FROM user_detail ud
WHERE ud.userid = (SELECT ei.invited_user
FROM event_invite ei
WHERE ei.eventid = e.id
AND ei.acceptance = 'Accept')) AS avgr)
+ (SELECT IFNULL(( 3959 * ACOS(COS(RADIANS(22.6979425)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(75.8597305)) + SIN(RADIANS(22.6979425)) * SIN(RADIANS(latitude))) ), 0)
FROM address a
WHERE a.latitude != ''
AND a.longitude != ''
AND e.event_address = a.id) ) AS dest
FROM event e
WHERE ( ( e.maximumattendeesallow > ( (SELECT COUNT(*) AS cnt
FROM event_interest
WHERE eventid = e.id
AND approvalstatus = 'Approve')
+ (SELECT COUNT(*) AS cnt
FROM event_invite
WHERE eventid = e.id
AND acceptance = 'Accept') ) )
OR ( e.maximumattendeesallow = 0 ) )
AND EXISTS (SELECT id
FROM address a
WHERE latitude != ''
AND longitude != ''
AND e.event_address = id
AND IFNULL(( 3959 * ACOS(COS(RADIANS(22.6979425)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(75.8597305)) + SIN(RADIANS(22.6979425)) * SIN(RADIANS(latitude))) ), 0) < 100)
AND NOT EXISTS (SELECT eventid
FROM event_interest
WHERE e.id = eventid
AND approvalstatus != 'InterestExpressed'
AND interested_user = 49)
AND NOT EXISTS (SELECT eventid
FROM event_invite
WHERE eventid = e.id
AND invited_user = 49);
This query giving me error -
Error Code: 1054
Unknown column 'e.id' in 'where clause'
Why its not allowing subquery to use e.id in order by clause.
In the query, you have used E.ID. change it to "e.id" and execute. This time you won't get that error.
In fourth select clause, that is after AND NOT EXISTS(Select.., . In where clause you mentioned as WHERE e.id =eventid , instead of this , write as WHERE eventid = e.id . This will resolve your error.
Related
I am doing substraction from TotalAmt to NetTotal but when TotalAmt is Less than NetTotal then Due is Showing Negative values.How do i display Due as zeros if TotalAmt is Less than NetTotal.My Query for retrieving Due amount is
SELECT DISTINCT id,
name,
TotalAmt,
NetTotal,
Due
FROM
(SELECT u.id,
u.name,
(SELECT SUM(amt)
FROM pay_master
WHERE refsid = u.id
AND typ IN ('std_deposit',
'sale_ind')
GROUP BY refsid) AS 'TotalAmt',
(SELECT IFNULL(SUM(net_total), 0)
FROM trans_master t
WHERE t.refid = u.id
GROUP BY t.refid) AS 'NetTotal',
IFNULL(
(SELECT IFNULL(SUM(amt), 0)
FROM pay_master p
WHERE p.refsid = u.id
AND typ IN ('std_deposit', 'sale_ind')
GROUP BY refsid) -
( SELECT IFNULL(SUM(net_total), 0)
FROM trans_master t
WHERE t.refid = u.id
GROUP BY t.refid), 0) AS 'Due'
FROM USER u
INNER
JOIN pay_master p ON p.refsid = u.id
AND u.typ = 'std')x
Are you looking for this??
SELECT DISTINCT id,
name,
TotalAmt,
NetTotal,
case when Due < 0 then 0 else Due end
FROM
(SELECT u.id,
u.name,
(SELECT SUM( amt )
FROM pay_master
WHERE refsid = u.id
AND typ IN ( 'std_deposit', 'sale_ind')
GROUP BY refsid
) AS 'TotalAmt',
(SELECT IFNULL( SUM( net_total ) , 0 )
FROM trans_master t
WHERE t.refid = u.id
GROUP BY t.refid
) AS 'NetTotal',
IFNULL(
(SELECT IFNULL( SUM( amt ) , 0 )
FROM pay_master p
WHERE p.refsid = u.id
AND typ IN ( 'std_deposit', 'sale_ind')
GROUP BY refsid
) -
(SELECT IFNULL( SUM( net_total ) , 0 )
FROM trans_master t
WHERE t.refid = u.id
GROUP BY t.refid
) , 0) AS 'Due'
FROM USER u
INNER JOIN pay_master p
ON p.refsid = u.id
AND u.typ = 'std'
)x
Use case to maintain for such of condition
SELECT o.* FROM ( SELECT
#rownum := #rownum + 1 RN,
i.* FROM<br>
(
SELECT
#rownum:=0
)
r,<br>
(
SELECT DISTINCT<br>
CG_MGR_USR.MANAGER_NAME,<br>
CG_MGR_USR.PARAM_NAME,<br>
USR_TR_DATA.USER_ID,<br>
CG_MGRS.FIRST_NAME,<br>
CG_MGRS.LAST_NAME,<br>
CC_GENOBJ.OBJNAME,<br>
USR_TR_DATA.RISK_ID,<br>
USR_TR_DATA.VERSION_ID,<br>
T_RISK.RISK_DESCR,<br>
CC_RISKT.RISK_ID CC_RISK,<br>
CC_RISKT.DESCN,<br>
USR_TR_DATA.SYSTEM_ID,<br>
T_STATUS.STATUS,<br>
USR_TR_DATA.COUNTS,<br>
SYSTEMS.SYSTEM_NAME<br>
FROM
TABLE_USR_TR_DATA USR_TR_DATA<br>
LEFT OUTER JOIN TABLE_CC_GENOBJ CC_GENOBJ<br>
ON
CC_GENOBJ.GENOBJTP = 1
AND CC_GENOBJ.SYSTEM_ID IN<br>
(
SELECT
sys_id
FROM
TABLE_CS_SYSTEM
WHERE
cs_sys_id =USR_TR_DATA.system_id
AND is_primary='Y'
)<br>
AND CC_GENOBJ.GENOBJID = USR_TR_DATA.USER_ID,<br>
TABLE_RISK T_RISK<br>
LEFT OUTER JOIN TABLE_CC_RISKT CC_RISKT
ON<br>
CC_RISKT.LANG = 'EN'
AND T_RISK.CC_RISK_ID = CC_RISKT.RISK_ID,<br>
TABLE_CG_MGR_USR CG_MGR_USR,<br>
TABLE_CG_MGRS CG_MGRS,<br>
TABLE_STATUS T_STATUS,<br>
TABLE_CS_SYSTEM CS_SYSTEMS,<br>
TABLE_SYSTEMS SYSTEMS<br>
WHERE
USR_TR_DATA.RISK_ID = T_RISK.RISK_ID<br>
AND USR_TR_DATA.VERSION_ID = T_RISK.VERSION_ID<br>
AND CG_MGR_USR.USER_NAME = USR_TR_DATA.USER_ID<br>
AND CG_MGR_USR.SYSTEM_ID = CS_SYSTEMS.SYS_ID<br>
AND CG_MGRS.MANAGER_NAME = CG_MGR_USR.MANAGER_NAME<br>
AND CG_MGRS.PARAM_NAME =CG_MGR_USR.PARAM_NAME<br>
AND CG_MGRS.SYSTEM_ID = CG_MGR_USR.SYSTEM_ID<br>
AND CS_SYSTEMS.CS_SYS_ID = USR_TR_DATA.SYSTEM_ID<br>
AND CS_SYSTEMS.IS_PRIMARY = 'Y'<br>
AND T_STATUS.SEQ_NO = USR_TR_DATA.STATUS_ID<br>
AND SYSTEMS.SYSTEMS_ID = USR_TR_DATA.SYSTEM_ID<br>
AND CG_MGR_USR.MANAGER_NAME IN( 'SAPUSER' )<br>
AND T_STATUS.STATUS IN( 'IN-PROCESS', 'OPEN' )<br>
AND USR_TR_DATA.COUNTS >= 1<br>
ORDER BY
USER_ID,
SYSTEM_ID,
RISK_ID,
VERSION_ID,
CC_RISK,
STATUS,
COUNTS ASC
)
i ) o <br>WHERE o.RN >= 1 AND o.RN <= 10
It is taking too much time for to fetch only 10 records.
I'm trying to execute the following query, Bu it gives a syntax error, Can some point me were the problem is?
select count(*) from (((
select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134) c
JOIN (select * from testlink1915.TL_nodes_heirachy) d
ON id = testlink1915.TL_nodes_heirachy.parent_id) a
JOIN (select * FROM testlink1915.TL_req_coverage where req_id = 67635) b
ON a.id = b.testcase_id);
Error appeared only when I added the following segment.
c JOIN (select * from testlink1915.TL_nodes_heirachy) d
ON id = testlink1915.TL_nodes_heirachy.parent_id
The error
Error Code: 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 'a JOIN (select * FROM testlink1915.TL_req_coverage where req_id = 67635) b ON a.' at line 1
Use:
c.id = d.parent_id
instead of
id = testlink1915.TL_nodes_heirachy.parent_id
Try this:
select count(*)
from (
select *
from (
select *
from (
select *
from testlink1915.TL_tcversions
where execution_type = 2
and id = 66134
) c join (
select *
from testlink1915.TL_nodes_heirachy
) d on c.id = d.parent_id
) a join (
select *
from testlink1915.TL_req_coverage
where req_id = 67635
) b on a.id = b.testcase_id
) t;
Please note that I use * everywhere. Replace it with the columns you need.
Please Try this
select count(1) from
(
select * from (
select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134 ) c
JOIN
(select * from testlink1915.TL_nodes_heirachy) d on a.id=d.parent_id
join
(select * FROM testlink1915.TL_req_coverage where req_id = 67635) a ON a.id = b.testcase_id
) as E join (select * from testlink1915.TL_nodes_heirachy) F ON E.id = F.parent_id
Try below query :
select count(*) from
(
(
select * from testlink1915.TL_tcversions
JOIN testlink1915.TL_nodes_heirachy ON id =
testlink1915.TL_nodes_heirachy.parent_id where execution_type = 2 and id
= 66134
) a
JOIN
(
select * FROM testlink1915.TL_req_coverage where req_id = 67635
) ON a.id = b.testcase_id
) ;
I'm not getting to use "ALIAS" in the "WHERE" MYSQL, in code below I need to do something like:
AND ( custo_percapita_regiao + comissao_representante ) < consig.valor_pago_comissao_representante
========================== code ===========================
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
(
( SELECT ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` )
FROM `regioes`
WHERE `id` = `consult`.`regiao`
) /
( SELECT COUNT(*)
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consultores`.`regiao` = `consult`.`regiao`
AND `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
)
) AS custo_percapita_regiao,
(`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
WHERE `representante` = 3
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`
Possibly like this:-
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
Sub1.RegionTot / Sub2.RegionCount AS custo_percapita_regiao,
(`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
LEFT OUTER JOIN
(
SELECT `id`, ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` ) AS RegionTot
FROM `regioes`
) Sub1 ON Sub1.id = `consult`.`regiao`
LEFT OUTER JOIN
(
SELECT `consultores`.`regiao`, COUNT(*) AS RegionCount
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
GROUP BY `consultores`.`regiao`
) Sub2 ON Sub2.`regiao` = `consult`.`regiao`
WHERE `representante` = 3
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
AND (Sub1.RegionTot / Sub2.RegionCount) < (`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 )
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`
(partly swapping the sub selects from correlated ones in the SELECT to joins so the values can easily be used)
Try using variables:
SET #custo_percapita_regiao = 0;
SET #comissao_representante = 0;
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
#custo_percapita_regiao :=
(
( SELECT ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` )
FROM `regioes`
WHERE `id` = `consult`.`regiao`
) /
( SELECT COUNT(*)
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consultores`.`regiao` = `consult`.`regiao`
AND `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
)
) AS custo_percapita_regiao,
#comissao_representante := (`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
WHERE `representante` = 3 AND (#custo_percapita_regiao + #comissao_representante ) < consig.valor_pago_comissao_representante
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`
I have a union query as follows:
(SELECT t.id, t.name, c.company AS owner, t.creation_date AS date, t.notes
FROM tool t, client c
WHERE t.id_customer = '15' AND t.trash_flag = '1')
UNION
(SELECT f.id, f.name, CONCAT(m.first_name, ' ', m.last_name) AS owner, f.date, f.notes
FROM file f, meta m
WHERE ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f.trash_flag = '1' AND m.user_id = f.id_user)
ORDER BY 'name' 'ASC' LIMIT 0,20
Everything works fine but I have two questions:
How do I add a column to the entire result set that gives the row number
Could I do this without using UNION e.g. an advanced join?
Thanks for your time MySQL gurus!
I can't test it righ now but from what I found, following might work:
Reference: Row Number Variable
SQL Statement
SELECT #rownum := #rownum + 1 rownum
, t.*
FROM (
(SELECT t.id
, t.name
, c.company AS owner
, t.creation_date AS date
, t.notes
FROM tool t
, client c
WHERE t.id_customer = '15'
AND t.trash_flag = '1'
) UNION (
SELECT f.id
, f.name
, CONCAT(m.first_name, ' ', m.last_name) AS owner
, f.date
, f.notes
FROM file f
, meta m
WHERE ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f.trash_flag = '1' AND m.user_id = f.id_user)
)
) t
, (SELECT #rownum := 0) r
ORDER BY
'name' ASC
LIMIT 0, 20