Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Issue with a corelated subquery

$
0
0

Hi guys, just an advice. I'm not able to understand this SQL behavior:

TBL Products ( id, name(varchar), description(varchar)

TBL ProductSales ( id, productid (foreign keys), unitprice (int), quantity (int).

Let's go: 

A simple:

select * from tbl_productSales a inner join tbl_products on a.ProductId=tbl_products.id

returns

id    prodid upr   qty   id     name       description

1 3 45053           desktophp high performance

22 250  7 2         laptopvery thin black color acer laptop
33450 4       3  desktophp high performance
43450 9       3  desktophp high performance



Note that id 1 , tvlcd is not present because it wasn't sold so far. It's ok.

Corelated subquery:

select name,
(select sum(quantitysold) from tbl_productSales a where a.ProductId=tbl_products.id ) as qtysold
from tbl_products 

returns

nome      qtysold

tv           NULL
laptop7
desktop   18

Now I'm wondering why returns a value not in a.ProductId=tbl_products.id?

In a.productId the number 1 doesn't exist. I should think that a corelated subquery returns all the value from the main query? And in this case, how can I exclude the value not in a.productId?

Thanks a lot as always



 

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>