0
I am doing this query, and I need to query the cost value of a product using a subquery, because the note table is the same as input and output. The products do not repeat. What differentiates sale and cost. It is the series of the note. It is returning the cost as null. But if I include a product code manually. It works.
Dunghill:
mercadoria = varchar(12)
SELECT *,
(select valornota from nota as u inner
join itemnota as j
on u.id_nfcapa = j.id_nfcapa
where j.mercadoria = i.mercadoria and u.serienf = '001') as custo
FROM nota as n
inner join itemnota as i on n.id_nfcapa = i.id_nfcapa
WHERE n.datanf BETWEEN '2017-08-01' AND '2017-08-31'
AND n.serie = '1'
That’s how it works:
SELECT *,
(select valornota from nota as u inner
join itemnota as j
on u.id_nfcapa = j.id_nfcapa
where j.mercadoria = 'PROD1234' and u.serienf = '001') as custo
FROM nota as n
inner join itemnota as i on n.id_nfcapa = i.id_nfcapa
WHERE n.datanf BETWEEN '2017-08-01' AND '2017-08-31'
AND n.serie = '1'
Ran exactly your SQL, just took out the cost field. This field does not exist in the table. It is an alias of the subquery. Returned this error: A subquery has returned not Exactly one Row.
– Eduardo Santos
Actually the cost field does not exist in the table. It is only an alias. You need to enter the name of the corresponding field in the itemnota table. What is the structure of the tables ?
– Caiuby Freitas
commodity = Varchar (12)
– Eduardo Santos
Eduardo, to clarify your doubt it is important to have information about the structure of fields of the tables involved, so it becomes possible to reproduce the situation you found.
– Caiuby Freitas
I managed to solve friend, I took the nickname from the tables. I used in the format TABLE.FIELD without using Inner Join. It worked perfectly. Thanks for the help.
– Eduardo Santos
I’m glad to hear that, Eduardo. Happy Programming!
– Caiuby Freitas