Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i use to write a subquery to collect all matching record but it gives error. what should i do???

What I have tried:

SQL
SELECT   DISTINCT  dbo.master_order.order_no, dbo.master_order.program_no, dbo.Setup_size.size_name, dbo.Setup_color.color_name, dbo.Setup_color.color_no, 
                      dbo.transaction_production.total_weight, dbo.Setup_yarn.yarn_count, dbo.Setup_article_order.article_name, dbo.master_order.shipment_date, @from as reprt,@to as reprt1,
                      dbo.transaction_order.quantity, dbo.transaction_order.gsm
                       FROM         dbo.master_order INNER JOIN
                      dbo.transaction_order ON dbo.master_order.order_id = dbo.transaction_order.order_id INNER JOIN
                      dbo.transaction_production ON dbo.transaction_order.trans_id = dbo.transaction_production.trans_id INNER JOIN
                      dbo.Setup_size ON dbo.transaction_order.size_id = dbo.Setup_size.size_id INNER JOIN
                      dbo.Setup_yarn ON dbo.transaction_order.yarn_id = dbo.Setup_yarn.yarn_id INNER JOIN
                      dbo.Setup_article_order ON dbo.transaction_order.article_id = dbo.Setup_article_order.article_id INNER JOIN
                      dbo.Setup_color ON dbo.transaction_order.color_id = dbo.Setup_color.color_id AND dbo.Setup_yarn.color_id = dbo.Setup_color.color_id 
                      where dbo.setup_color.color_id=(select color_no from dbo.setup_color)
                      where master_order.shipment_date>=@from and master_order.shipment_date<=@to
Posted
Updated 3-Nov-17 1:56am
v2

1 solution

Why do you have two WHERE clauses in the same query?
SQL
where dbo.setup_color.color_id=(select color_no from dbo.setup_color)
where master_order.shipment_date>=@from and master_order.shipment_date<=@to

Did you mean:
SQL
where dbo.setup_color.color_id=(select color_no from dbo.setup_color
                                where master_order.shipment_date>=@from and 
                                      master_order.shipment_date<=@to)
Or
SQL
where dbo.setup_color.color_id=(select color_no from dbo.setup_color)
  AND master_order.shipment_date>=@from and master_order.shipment_date<=@to
 
Share this answer
 
Comments
Ali Khan 3-Nov-17 2:58am    
i just wanted to see complete data from setup color with other tables data that is why i use subquery. any other option??
OriginalGriff 3-Nov-17 3:05am    
We can't tell: we don't have access to your database schema, or any idea what results you are trying to get. Hence the two options above...

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