Click here to Skip to main content
14,770,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to find the whose product items sold

output i need as

produtname    itemssold   (using product id)


for that how to write a query in sql server

What I have tried:

productid   prodcutname  count
 1             A           2
 2             B           3
 3             C           1
 4             D           2
 5             E           1


prdouctname     itemssold
  A                1
  B                2
  C                0
  D                1
  E                1
Posted
Updated 16-Mar-18 9:58am
v2

1 solution

That doesn't make a lot of sense: You don't have a ProductID in both tables, you have a ProductName - which is very inefficient as well as complicating what you are doing.
Try these tables:
ProductID	ProductName	Count
1	        A         	2
2	        B         	3
3	        C         	1
4	        D         	2
5	        E         	1

ProductID	ItemsSold
1	        1
2	        2
3	        0
4	        1
5	        1
And this query:
SELECT a.ProductName, b.ItemsSold FROM table1 a
JOIN table2 b ON a.ProductID = b.ProductID
WHERE b.ItemsSold > 0
That gives you what I think you are looking for:
ProductName	ItemsSold
A         	1
B         	2
D         	1
E         	1
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900