Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
USE [cms]
GO
/****** Object:  StoredProcedure [dbo].[SpCateProductsInfo]    Script Date: 10/14/2012 00:50:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SpCateProductsInfo](@cate_id int)
as
begin
select sb.subcate_name, sum(p.qty) as totalqty, 
(select top 1 pimg_mid1 from product join subcategory
on product.subcate_id=
(
	select top 1 subcate_id
	from subcategory
	where cate_id=@cate_id
	order by NEWID()
)
order by NEWID()) as pimg_mid1
from subcategory sb 
  join product p on p.subcate_id = sb.subcate_id
where sb.cate_id = @cate_id
group by sb.subcate_name


end


it is working f9, But the problem is that the pimg_mid1 column contains same entries (every row regarding the pimg_mid1 column contains same data)

can any 1 tell me abt this error and the solution ???
Posted
Comments
Maciej Los 14-Oct-12 17:12pm    
Please, provide more informations. We can't read in your mind...

1 solution

Your query is logically wrong:

If you need all pimg_mid1
SQL
select sb.subcate_name, sum(p.qty) as totalqty,
p.pimg_mid1
from subcategory sb
join product p on p.subcate_id = sb.subcate_id
where sb.cate_id = @cate_id
group by sb.subcate_name, p.pimg_mid1


If you need one(latest) - use Max
SQL
select sb.subcate_name, sum(p.qty) as totalqty,
MAX(p.pimg_mid1) AS pimg_mid1
from subcategory sb
join product p on p.subcate_id = sb.subcate_id
where sb.cate_id = @cate_id
group by sb.subcate_name
 
Share this answer
 
Comments
ridoy 15-Oct-12 0:34am    
+5
Herman<T>.Instance 15-Oct-12 4:40am    
I always leave MAX at home. It uses tablescan. Better create a desc index on pimg_mid1. Select top 1 pinmg_midl is than always the latest which can be not the maximum.
Furthermore the pimg_midl is chosen by ordering om NewID() so he uses a random pimg_midl

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

  Print Answers RSS


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