我正在处理一个大查询来检索SQL Express DB中的某些数据,并且在合并两个查询时遇到问题。
我已经尝试在全局第二个查询上进行内部连接,但由于我对总和使用了某个 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
(二(
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.
我想了解为什么,以及如何纠正它。
感谢您的回答。
一些别名和现代联接将这堵文本墙变成了您可以更轻松地阅读的内容。
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%'
不知道为什么会收到错误消息。查看表定义将非常有帮助。
多亏了肖恩的回答,我想我设法找到了解决方案。
不需要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%')