SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to select with ID ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

headshot
Starting Member

8 Posts

Posted - 04/21/2012 :  12:47:19  Show Profile  Reply with Quote
Hello every body.
I have a database with two table Product and DetailBill
Table Product
IDPro NamePro Descrypt ImagePro

Table DetailBill
IDBill IDPro Count Price

I have a procedure

ALTER proc [dbo].[sp_Product_2]
as	
select  top(9) IDPro,count(IDPro) as SumofPro
 from DetailBill
group by IDPro
order by SumofPro desc

I must select * from Product with IDPro bellow , but i can`t .
Can you help me ?
I try

ALTER proc [dbo].[sp_Product_2]
as	
Select * from Product where IDPro in (select  top(9) IDPro,count(IDPro) as SumofPro
 from DetailBill
group by IDPro
order by SumofPro desc)

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 04/21/2012 :  12:51:37  Show Profile  Reply with Quote
the count() within subquery doesnt make sense. it should be


ALTER proc [dbo].[sp_Product_2]
as	
Select * from Product 
where IDPro in (select  top(9) IDPro
 from DetailBill
group by IDPro
order by count(IDPro) desc)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

headshot
Starting Member

8 Posts

Posted - 04/21/2012 :  12:58:54  Show Profile  Reply with Quote
Thank for visakh16.
But you can tell me why not use "count(IDPro) as SumofPro"
I want to create one column in table then query and all column in Product, It`s name is SumofPro
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 04/21/2012 :  13:18:37  Show Profile  Reply with Quote
you cant have more than a single column in your subquery if you're linking it to main query using IN. If you really want to use count inside then you need to use join instead. like


SELECT p.*
FROM Product p
INNER JOIN (select  top(9) IDPro,count(IDPro) as SumofPro
 from DetailBill
group by IDPro
order by SumofPro desc)p1
ON p1.IDPro = pIDPro


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

headshot
Starting Member

8 Posts

Posted - 04/22/2012 :  06:02:04  Show Profile  Reply with Quote
Dear visakh16.
I think this is enough ,but anyway, thank you very much .

ALTER proc [dbo].[sp_Product_2]
as	
Select * from Product 
where IDPro in (select  top(9) IDPro
 from DetailBill
group by IDPro
order by count(IDPro) desc)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 04/22/2012 :  13:28:52  Show Profile  Reply with Quote
quote:
Originally posted by headshot

Dear visakh16.
I think this is enough ,but anyway, thank you very much .

ALTER proc [dbo].[sp_Product_2]
as	
Select * from Product 
where IDPro in (select  top(9) IDPro
 from DetailBill
group by IDPro
order by count(IDPro) desc)



yep...thats enough

I was just telling you how to use query in join
so far as you dont need to return count value you dont need it in select list

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000