DotDragnet
May 23, 2012, 02:53:07 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: (SQL) Order By Two Columns as One  (Read 1394 times)
MattC
Hero Member
*****
Posts: 825



mattc_stfc
View Profile WWW Awards
« on: July 18, 2008, 03:43:44 PM »

I need to pull prices from two columns and order them as if they where one.

Example Data.

tbl_products As in DB:
ID|Name|Price|Sale_Price
1|Prod1|£10.00|£0.00
2|Prod2|£20.00|£5.00
3|Prod3|£30.00|£25.00
4|Prod4|£40.00|£0.00
5|Prod5|£50.00|£35.00

Needs to be ordered as:
ID|Name|Price|Sale_Price
2|Prod2|£20.00|£5.00
1|Prod1|£10.00|£0.00
3|Prod3|£30.00|£25.00
5|Prod5|£50.00|£35.00
4|Prod4|£40.00|£0.00

So basically it needs to show the lowest price regardless of whether the lowest is in the column "Price" or "Sale Price". Generally if a product is not sold at sale price then it will have a sale price of £0.00, at this stage I can not change this.


« Last Edit: July 18, 2008, 03:47:15 PM by MattC » Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #1 on: July 18, 2008, 04:47:37 PM »

ORDER BY IF(Sale_Price > 0 AND Sale_Price < Price, Sale_Price, Price)
Logged
MattC
Hero Member
*****
Posts: 825



mattc_stfc
View Profile WWW Awards
« Reply #2 on: July 19, 2008, 10:14:11 AM »

Thanks Jason but I get a syntax error by the keyword IF

M$ SQL Server

Actual SQL Statement
Quote
SELECT     name, price, saleprice
FROM         products
WHERE     catcode = 8
ORDER BY IF (saleprice > 0 AND saleprice < price, saleprice, price)
« Last Edit: July 19, 2008, 12:16:03 PM by MattC » Logged

MattC
Hero Member
*****
Posts: 825



mattc_stfc
View Profile WWW Awards
« Reply #3 on: July 19, 2008, 02:20:04 PM »

Right, I've got close to sorting this.

Quote
SELECT products.productid, products.Price,
ProdPrice = CASE products.saleprice 
when 0 THEN products.price
else products.saleprice
end, products.saleprice
FROM products
where catcode=61
order by prodprice desc

Works fine apart from when I try to use CASE within the sproc to allow for choice of ORDER BY

Quote
order by
CASE WHEN @orderb = '0' THEN productid
END desc,
CASE WHEN @orderb = '1' THEN productid
END asc,
CASE WHEN @orderb = '2' THEN ProdPrice
END asc,
CASE WHEN @orderb = '3' THEN name
END asc,
CASE WHEN @orderb = '4' THEN ProdPrice
END desc,
CASE WHEN @orderb = '5' THEN name
END desc
 
It returns an error stating that ProdPrice is an invalid column. It works fine with price or saleprice so I can only guess it has something to do with the fact that the ProdPrice column doesn't exsist in the DB and is created in the SQL statement/sproc.

Scratching my head a little on this one.

tia

Matt
« Last Edit: July 19, 2008, 03:32:16 PM by MattC » Logged

MattC
Hero Member
*****
Posts: 825



mattc_stfc
View Profile WWW Awards
« Reply #4 on: July 21, 2008, 09:19:25 AM »

For anybody that's interested I resolved this via
Quote
order by coalesce(nullif(SalePrice,0.00),Price) desc
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!