Subquery returning null value

Asked

Viewed 47 times

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'

1 answer

1

You must specify which fields of the resultset are. Try with the modification below:

SELECT i.mercadoria, custo 
 (select valornota from nota as u 
  inner join itemnota as j 
  on u.id_nfcapa = j.id_nfcapa 
  where j.mercadoria = u.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'
  • 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.

  • 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 ?

  • commodity = Varchar (12)

  • 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.

  • 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.

  • I’m glad to hear that, Eduardo. Happy Programming!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.