Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This one is my query

SQL
select distinct top(8)  sumit.CouponsID, sumit.Items_Desc_ThumbImageURl, sumit.CouponsShopCity,
sumit.Items_Desc_UpdatedON, sumit.Prod_Cat_Name,sumit.Prod_Sub_Cat_Name,
sumit.CouponsEndDate
from
(
select ROW_NUMBER() over(PARTITION BY  CouponsStock.CouponsShopCity
order by CouponsStock.CouponsShopCity desc)
as rowno,
CouponsStock.CouponsID,
CouponsStock.CouponsEndDate,
ProductCategory.Prod_Cat_Name, ProductSubCategory.Prod_Sub_Cat_Name,
CouponsStock.CouponsShopCity,
ProdSubCatItemsDescription.Items_Desc_UpdatedON ,
ProdSubCatItemsDescription.Items_Desc_ThumbImageURl
from ProductCategory inner join ProductSubCategory
on ProductCategory.Prod_Cat_ID=ProductSubCategory.Prod_Cat_ID
inner join ProductSubCatItmes on
ProductSubCategory.Prod_Sub_Cat_ID=ProductSubCatItmes.Prod_Sub_Cat_ID
inner join ProdSubCatItemsDescription  on
ProductSubCatItmes.Prod_Sub_Cat_Items_ID=ProdSubCatItemsDescription.Prod_Sub_Cat_Items_ID
inner join CouponsStock on
ProdSubCatItemsDescription.Prod_Sub_Cat_Items_Desc_ID=CouponsStock.Prod_Sub_Cat_Items_Desc_ID
where  CouponsStock.CouponsStatus='visible' and
CouponsStock.CouponsEndDate>=GETDATE() order by CouponsStock.CouponsEndDate desc
)
as sumit where sumit.rowno=1
order by sumit.Items_Desc_UpdatedON



After Executing Above query....it's throw following error..
Msg 1033, Level 15, State 1, Line 25
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.


Can any one guide me how can i execute above query............
Posted
Comments
[no name] 21-Jan-13 6:17am    
Change your code to :

select distinct top(8) sumit.CouponsID, sumit.Items_Desc_ThumbImageURl, sumit.CouponsShopCity,
sumit.Items_Desc_UpdatedON, sumit.Prod_Cat_Name,sumit.Prod_Sub_Cat_Name,
sumit.CouponsEndDate
from
(
select ROW_NUMBER() over(PARTITION BY CouponsStock.CouponsShopCity
order by CouponsStock.CouponsShopCity desc)
as rowno,
CouponsStock.CouponsID,
CouponsStock.CouponsEndDate,
ProductCategory.Prod_Cat_Name, ProductSubCategory.Prod_Sub_Cat_Name,
CouponsStock.CouponsShopCity,
ProdSubCatItemsDescription.Items_Desc_UpdatedON ,
ProdSubCatItemsDescription.Items_Desc_ThumbImageURl
from ProductCategory inner join ProductSubCategory
on ProductCategory.Prod_Cat_ID=ProductSubCategory.Prod_Cat_ID
inner join ProductSubCatItmes on
ProductSubCategory.Prod_Sub_Cat_ID=ProductSubCatItmes.Prod_Sub_Cat_ID
inner join ProdSubCatItemsDescription on
ProductSubCatItmes.Prod_Sub_Cat_Items_ID=ProdSubCatItemsDescription.Prod_Sub_Cat_Items_ID
inner join CouponsStock on
ProdSubCatItemsDescription.Prod_Sub_Cat_Items_Desc_ID=CouponsStock.Prod_Sub_Cat_Items_Desc_ID
where CouponsStock.CouponsStatus='visible' and
CouponsStock.CouponsEndDate>=GETDATE()
) as sumit where sumit.rowno=1
order by sumit.Items_Desc_UpdatedON

1 solution

You cannot have an ORDER BY inside a view[^]. Thus remove the ORDER BY from the view to fix the error.
 
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