感谢您的回答!
这是一个编辑:
SELECT 'SYSA',
t1.lieu_stkph_cd,
Sum (t1.mt_pnu_cpta_dev_rep),
Sum (t2.mt_util_dev_rep)
FROM (SELECT a.id_auto, a.dt_art, c.lieu_stkph_cd,
a.mt_pnu_cpta_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
WHERE c.pma_cd = '') AS t1
LEFT JOIN (SELECT a.id_auto, a.dt_art, c.lieu_stkph_cd,
b.mt_util_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
INNER JOIN db_ftg_srs_prod_v.v_utilisation_a b
ON a.dt_art = b.dt_art
AND a.id_auto = b.id_auto
WHERE c.pma_cd = '') AS t2
ON T1.id_auto = t2.id_auto
and T1.dt_art = T2.dt_art and t1.lieu_stkph_cd = t2.lieu_stkph_cd
GROUP BY 1,
2
这是此查询的结果:
LIEU_STKPH_CD PNU金额利用金额
1 200€
250€
这不准确,我解释说:
db_ftg_srs_prod_v.v_autorisation_a链接到db_ftg_srs_prod_v.v_utilisation_a和
- ID_AUTO
- DT_ART
但我可以为X UTILIZATION设置1个ID_AUTO,因此使用此查询我将乘以PN使用PNU量,这是不正确的
授权表
ID_AUTO PNU金额
1 100€
利用表
ID_AUTO ID_UTLIZATION利用金额
1 1
100€
1 2
150€
所以我必须将这些值分开:
预期结果
LIEU_STKPH_CD PNU金额利用金额
1 100€
250€
你有什么想法吗?
提前致谢
克里斯托夫
答案 0 :(得分:1)
在这里对你的数据关系进行大量猜测。您应该使用带有JOIN
的实际ON
子句来加入,以解释如何将这两个子查询组合在一起。类似的东西:
SELECT 'SYSA',
t1.lieu_stkph_cd,
Sum (t1.mt_pnu_cpta_dev_rep),
Sum (t2.mt_util_dev_rep)
FROM (SELECT c.lieu_stkph_cd,
a.mt_pnu_cpta_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
WHERE c.pma_cd = '') AS t1
INNER JOIN (SELECT c.lieu_stkph_cd,
b.mt_util_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
INNER JOIN db_ftg_srs_prod_v.v_utilisation_a b
ON a.dt_art = b.dt_art
AND a.id_auto = b.id_auto
WHERE c.pma_cd = '') AS t2
ON T1.lieu_stkph_cd = t2.lieu_stkph_cd
GROUP BY 1,
2
答案 1 :(得分:0)
你的第一个查询是好的,因为你做了
SELECT ( select ... ) as field 1,
( select ... ) as field 2,
但是你的第二个你正在进行交叉加入
SELECT *
FROM ( select ... ) as query1,
( select ... ) as query2
使用query1 x query2 rows
创建查询你想:
SELECT query.*
FROM ( SELECT ( select ... ) as field1,
( select ... ) as field2
.....
) as query
答案 2 :(得分:0)
有几种方法可以摆脱相乘的值,例如:在加入之前聚合
SELECT 'SYSA',
t1.lieu_stkph_cd,
t1.mt_pnu_cpta_dev_rep,
t2.mt_util_dev_rep
FROM (SELECT a.id_auto, a.dt_art, c.lieu_stkph_cd,
a.mt_pnu_cpta_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
WHERE c.pma_cd = '') AS t1
LEFT JOIN (SELECT a.id_auto, a.dt_art, c.lieu_stkph_cd,
Sum(b.mt_util_dev_rep) AS mt_util_dev_rep
FROM prod_v_ec_dossier_a_sysa c
INNER JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
INNER JOIN db_ftg_srs_prod_v.v_utilisation_a b
ON a.dt_art = b.dt_art
AND a.id_auto = b.id_auto
WHERE c.pma_cd = ''
GROUP BY 1,
2 ) AS t2
ON T1.id_auto = t2.id_auto
AND T1.dt_art = T2.dt_art AND t1.lieu_stkph_cd = t2.lieu_stkph_cd
但似乎您不需要加入两个派生表,这应该返回相同的结果:
SELECT 'SYSA',
t1.lieu_stkph_cd,
-- this value is multiplied by the number of rows
-- so simply divide by that number to revert the multiplication
Sum (a.mt_pnu_cpta_dev_rep) / Count(*),
Sum (b.mt_util_dev_rep)
FROM prod_v_ec_dossier_a_sysa c
JOIN db_ftg_srs_prod_v.v_autorisation_a a
ON a.id_doss = c.dosscta_no
AND a.cd_prd_cpta = c.prct_no
AND a.cd_entite_cpta = c.entite_cd
JOIN db_ftg_srs_prod_v.v_utilisation_a b
ON a.dt_art = b.dt_art
AND a.id_auto = b.id_auto
WHERE c.pma_cd = ''
GROUP BY 1,
2