i'm working on one big query to retriview certain DATA in a SQL Express DB and i'm facing issues while merging two queries.
I already tried to make an inner join on the global second query but since i use a certain GROUP BY for my sum, i didn't work.
Here's my current query :
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%')
Made up with these two separated queries : (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%')
For me, it should just merge both tables on the 'DO_Piece' value (availible in both tables), but it doesn't work and i have this error :
Msg 4104, Level 16, State 1, Line 30
L'identificateur en plusieurs parties "F_DOCENTETE.DO_Piece" ne peut pas être lié.
(My Visual Studio is in French)
Translated in English :
Msg 4104, Level 16, State 1, Line 30.
The multi-part identifier "F_DOCENTETE.DO_Piece" could not be bound.
I'd like to understand why, and how to correct it.
Thanks for your answers.
Thanks to Sean's answer, i think i managed to find the solution.
No CROSS JOIN
needed, and it's working fine.
Here's the query :
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%')