Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have using in this query..

how to use top 1 decending order in

pls help me any body


SQL
select distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=1 union
select distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id,   b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=2   union
select  distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id,  b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=3  union

select distinct  top 1  a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=4 union
select distinct  top 1 a.product_price, a.product_id,  a.product_name,b.category_id,  b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=5 union
select  distinct  top 1 a.product_price, a.product_id, a.product_name, b.category_id, b.category_name,a.small_image,a.product_image from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=6
Posted

First of all get rid of all those unions - replace them with an IN clause[^]
Next - the DISTINCT needs to come after the TOP 1
Finally ORDER BY DESC[^] goes at the end.
If you were still using UNION the order by would still go right at the end. Your query becomes
SQL
select top 1 distinct  a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image
from tbl_products_web as a
inner join tbl_category_web  as b on a.category_id=b.category_id
where b.category_id IN (1,2,3,4,5,6)
ORDER BY [whichever column here]


[EDIT - solution corrected based on OP comment]
There are a few ways you can achieve this

Option 1 - using Common Table Expressions
-- NOTE ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
-- so the select from C1, C2 etc redefines the list of columns
with C1 as (select distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image,a.image_updated 
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=1 order by a.image_updated desc),
C2 as (select distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id,   b.category_name,a.small_image,a.product_image,a.image_updated  
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=2 order by a.image_updated desc),
C3 as (select  distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id,  b.category_name,a.small_image,a.product_image,a.image_updated  
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=3 order by a.image_updated desc),
C4 as (select distinct  top 1  a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image,a.image_updated  
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=4 order by a.image_updated desc),
C5 as (select distinct  top 1 a.product_price, a.product_id,  a.product_name,b.category_id,  b.category_name,a.small_image,a.product_image,a.image_updated  
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=5 order by a.image_updated desc),
C6 as (select  distinct  top 1 a.product_price, a.product_id, a.product_name, b.category_id, b.category_name,a.small_image,a.product_image,a.image_updated  
	from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=6 order by a.image_updated desc)
select product_price, product_id, product_name,category_id, category_name,small_image,product_image from C1
UNION
SELECT product_price, product_id, product_name,category_id, category_name,small_image,product_image FROM C2
UNION
SELECT product_price, product_id, product_name,category_id, category_name,small_image,product_image FROM C3
UNION
SELECT product_price, product_id, product_name,category_id, category_name,small_image,product_image FROM C4
UNION
SELECT product_price, product_id, product_name,category_id, category_name,small_image,product_image FROM C5
UNION
SELECT product_price, product_id, product_name,category_id, category_name,small_image,product_image FROM C6
Option 2 - using a temporary table and a loop
create table #temp
(
	product_price float,
	product_id int,
	product_name varchar(255),
	category_id int,
	category_name varchar(255),
	small_image varchar(max),
	product_image varchar(max),
	image_updated datetime
)

declare @i int = 1
while @i <= 6
begin
	insert into #temp select distinct  top 1 a.product_price, a.product_id, a.product_name,b.category_id, b.category_name,a.small_image,a.product_image,a.image_updated 
		from tbl_products_web as a inner join tbl_category_web  as b on a.category_id=b.category_id where b.category_id=@i order by a.image_updated desc
	set @i = @i + 1
end
select product_price, product_id, product_name,category_id, category_name,small_image,product_image 
from #temp
 
Share this answer
 
v2
Comments
MohamedEliyas 23-Jun-15 6:45am    
THANKS SIR..

i need to 6 top category select a top 1 using decending order

for example
category 1 : top image (desc order image)
.
.
.
.
category 6 :top image (desc order)

i need top 6 images each category in 1 image not a one sir
CHill60 23-Jun-15 6:48am    
Descending order of what?
MohamedEliyas 23-Jun-15 7:11am    
last update image i need
CHill60 23-Jun-15 7:46am    
I've updated my solution - apologies for misunderstanding the requirement.
MohamedEliyas 23-Jun-15 8:41am    
thanks sir
If you are using SQL Server 2005 or newer you can use the analytic function RANK() to achieve what you want.
The best part is that it's generic and you don't need to know the categories in beforehand
SQL
WITH CTE AS (
    SELECT  a.product_price
           ,a.product_id
           ,a.product_name
           ,b.category_id
           ,b.category_name
           ,a.small_image
           ,a.product_image
           ,RANK() OVER (PARTITION BY b.category_id ORDER BY a.image_updated DESC) AS Rank
    FROM    tbl_products_web as a 
    INNER JOIN tbl_category_web as b ON a.category_id = b.category_id
    )
SELECT  a.product_price
       ,a.product_id
       ,a.product_name
       ,b.category_id
       ,b.category_name
       ,a.small_image
       ,a.product_image
FROM    CTE
WHERE   Rank = 1


Note that if you have two images with the same last image_updated for a category you'll get both with this query.
If this is a problem you can exchange the RANK() for a ROW_NUMBER().
But you won't know which of the row that get selected.
 
Share this answer
 
v2
SQL
SELECT DISTINCT TOP 1...
FROM ...

ORDER BY MyColumn DESC
 
Share this answer
 
SQL
SELECT DISTINCT  TOP 1 * FROM TBL_PRODUCTS_WEB ORDER BY sortColumn  DESC 


//see here
http://sqlfiddle.com/#!15/d3e5e/12[^]
 
Share this answer
 

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