我正在处理一个大查询以重新检索SQL Express数据库中的某些数据,并且在合并两个查询时遇到了问题。
我已经尝试在全局第二个查询中进行内部联接,但是由于我使用某个GROUP BY作为总和,所以我没有工作。
这是我当前的查询:
SELECT
col.CO_No as nom_commercial,
det.DO_PIECE AS ref_document,
det.AB_NO AS numero_abonnement,
det.CA_NUM AS compte_analytique,
det.CBMODIFICATION AS modified_on,
det.CT_NUMPAYEUR AS ref_societe_payeur,
det.DO_CONTACT AS contact,
det.DO_COORD01 AS nom_client_final1,
det.DO_COORD02 AS nom_client_final2,
det.DO_DATE AS date_emission,
det.DO_DEBUTPERIOD AS debut_periode,
det.DO_FINPERIOD AS fin_periode,
det.DO_REF AS numero_piece_externe,
det.DO_TIERS AS ref_societe_sage,
det.DO_TotalTTC - det.DO_TotalHTNet AS total_tva,
det.DO_TotalHTNet AS total_ht,
det.DO_TotalTTC AS total_ttc,
det.DO_TYPE AS type_document,
DOC.prix_revient_total,
det.DO_TotalHT - DOC.prix_revient_total as total_marge
FROM F_DOCENTETE as det, F_COLLABORATEUR as col
INNER JOIN
(SELECT
lg.DO_Piece,
SUM((lg.DL_Qte)*(lg.DL_PrixRU)) as prix_revient_total
FROM F_DOCLIGNE as lg
GROUP BY lg.DO_Piece
) AS DOC
ON det.DO_Piece = DOC.DO_Piece
WHERE det.CO_NO = col.CO_NO AND(det.DO_PIECE LIKE 'BC%')
用这两个分开的查询组成: (1)
SELECT
lg.DO_Piece,
SUM((lg.DL_Qte)*(lg.DL_PrixRU)) as prix_revient_total
from F_DOCLIGNE as lg
JOIN F_DOCENTETE
ON ((lg.DO_Piece = F_DOCENTETE.DO_Piece) AND(F_DOCENTETE.DO_PIECE LIKE 'BC%'))
JOIN F_COLLABORATEUR
ON F_DOCENTETE.Co_No = F_COLLABORATEUR.CO_NO
GROUP BY lg.DO_Piece, F_DOCENTETE.DO_Piece
(2)
SELECT
F_COLLABORATEUR.CO_NOM AS nom_commercial,
F_DOCENTETE.DO_PIECE AS ref_document,
F_DOCENTETE.AB_NO AS numero_abonnement,
F_DOCENTETE.CA_NUM AS compte_analytique,
F_DOCENTETE.CBMODIFICATION AS modified_on,
F_DOCENTETE.CT_NUMPAYEUR AS ref_societe_payeur,
F_DOCENTETE.DO_CONTACT AS contact,
F_DOCENTETE.DO_COORD01 AS nom_client_final1,
F_DOCENTETE.DO_COORD02 AS nom_client_final2,
F_DOCENTETE.DO_DATE AS date_emission,
F_DOCENTETE.DO_DEBUTPERIOD AS debut_periode,
F_DOCENTETE.DO_FINPERIOD AS fin_periode,
F_DOCENTETE.DO_REF AS numero_piece_externe,
F_DOCENTETE.DO_TIERS AS ref_societe_sage,
F_DOCENTETE.DO_TotalTTC - F_DOCENTETE.DO_TotalHTNet AS total_tva,
F_DOCENTETE.DO_TotalHTNet AS total_ht,
F_DOCENTETE.DO_TotalTTC AS total_ttc,
F_DOCENTETE.DO_TYPE AS type_document
FROM F_DOCENTETE, F_COLLABORATEUR
WHERE F_DOCENTETE.CO_NO = F_COLLABORATEUR.CO_NO AND(F_DOCENTETE.DO_PIECE LIKE 'BC%')
对我来说,它应该只合并两个表在'DO_Piece'值(两个表中都可用)上,但是它不起作用,并且我有此错误:
Msg 4104, Level 16, State 1, Line 30
L'identificateur en plusieurs parties "F_DOCENTETE.DO_Piece" ne peut pas être lié.
(我的Visual Studio是法语)
Translated in English :
Msg 4104, Level 16, State 1, Line 30.
The multi-part identifier "F_DOCENTETE.DO_Piece" could not be bound.
我想了解为什么以及如何纠正它。
感谢您的回答。
答案 0 :(得分:1)
一些别名和现代连接将文本墙变成可以更容易阅读的内容。
SELECT
c.CO_No as nom_commercial,
d.DO_PIECE AS ref_document,
d.AB_NO AS numero_abonnement,
d.CA_NUM AS compte_analytique,
d.CBMODIFICATION AS modified_on,
d.CT_NUMPAYEUR AS ref_societe_payeur,
d.DO_CONTACT AS contact,
d.DO_COORD01 AS nom_client_final1,
d.DO_COORD02 AS nom_client_final2,
d.DO_DATE AS date_emission,
d.DO_DEBUTPERIOD AS debut_periode,
d.DO_FINPERIOD AS fin_periode,
d.DO_REF AS numero_piece_externe,
d.DO_TIERS AS ref_societe_sage,
d.DO_TotalTTC - d.DO_TotalHTNet AS total_tva,
d.DO_TotalHTNet AS total_ht,
d.DO_TotalTTC AS total_ttc,
d.DO_TYPE AS type_document,
DOC.prix_revient_total,
d.DO_TotalHT - DOC.prix_revient_total as total_marge
FROM F_DOCENTETE d
join F_COLLABORATEUR c ON d.CO_NO = c.CO_NO
INNER JOIN
(
SELECT dl.DO_Piece
, SUM(dl.DL_Qte * dl.DL_PrixRU) as prix_revient_total
FROM F_DOCLIGNE dl
cross join F_DOCENTETE de
GROUP BY dl.DO_Piece
) AS DOC ON d.DO_Piece = DOC.DO_Piece
WHERE d.DO_PIECE LIKE 'BC%'
不确定为什么会收到错误消息。查看表定义真的很有帮助。
答案 1 :(得分:0)
感谢肖恩的回答,我认为我设法找到了解决方法。
不需要CROSS JOIN
,它工作正常。
这是查询:
SELECT
col.CO_No as nom_commercial,
det.DO_PIECE AS ref_document,
det.AB_NO AS numero_abonnement,
det.CA_NUM AS compte_analytique,
det.CBMODIFICATION AS modified_on,
det.CT_NUMPAYEUR AS ref_societe_payeur,
det.DO_CONTACT AS contact,
det.DO_COORD01 AS nom_client_final1,
det.DO_COORD02 AS nom_client_final2,
det.DO_DATE AS date_emission,
det.DO_DEBUTPERIOD AS debut_periode,
det.DO_FINPERIOD AS fin_periode,
det.DO_REF AS numero_piece_externe,
det.DO_TIERS AS ref_societe_sage,
det.DO_TotalTTC - det.DO_TotalHTNet AS total_tva,
det.DO_TotalHTNet AS total_ht,
det.DO_TotalTTC AS total_ttc,
det.DO_TYPE AS type_document,
DOC.prix_revient_total,
det.DO_TotalHTNet - DOC.prix_revient_total as total_marge
FROM F_DOCENTETE as det
join F_COLLABORATEUR col on det.Co_No = col.Co_No
INNER JOIN
(
SELECT lg.DO_Piece,
SUM((lg.DL_Qte)*(lg.DL_PrixRU)) as prix_revient_total
FROM F_DOCLIGNE as lg
GROUP BY lg.DO_Piece
) AS DOC ON det.DO_Piece = DOC.DO_Piece
WHERE (det.DO_PIECE LIKE 'BC%')