Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help with union all

Author  Topic 

squarefish
Starting Member

28 Posts

Posted - 2009-09-30 : 05:27:58
Hi there,

I am trying to get the below statement to work. Basically the two statements are returning the same data from the same table, firstly where a location is the same as a selected location, then where it is different from the location.

It should produce stock for the selected branch ordered by RetailPrice asc then stock for all other branches ordered by RetailPrice asc, but it is ignoring the order?

Any one know why?

SELECT     make, model, Derivative, BodyStyle, Doors, FuelType, Transmission, DriveTrain, Gears, ActualColour, ActualTrim, Mileage, RetailPrice, location, 
RegDate, RegPlate, StockNo, phone, RegLetter, lifestyle, code, CapID, VehicleType, nonfran, PCPCommission, PCPRate, PCPAPR, HPCommission,
HPRate, HPAPR, LPCommission, LPRate, LPAPR, deposit, landingpages, town, dealerdropdown
FROM (SELECT TOP (100) PERCENT dbo.stock.Manufacturer AS make, dbo.stock.Range AS model, dbo.stock.Derivative, dbo.stock.BodyStyle,
dbo.stock.Doors, dbo.stock.FuelType, dbo.stock.Transmission, dbo.stock.DriveTrain, dbo.stock.Gears, dbo.stock.ActualColour,
dbo.stock.ActualTrim, dbo.stock.Mileage, dbo.stock.RetailPrice, dbo.dealerinfo.name AS location, dbo.stock.RegDate, dbo.stock.RegPlate,
dbo.stock.StockNo, dbo.dealerinfo.phone, dbo.stock.RegLetter, dbo.models.fldLifestyle AS lifestyle, dbo.dealerinfo.code, dbo.stock.CapID,
dbo.stock.VehicleType, dbo.stock.nonfran, dbo.tblFinanceRates.PCPCommission, dbo.tblFinanceRates.PCPRate,
dbo.tblFinanceRates.PCPAPR, dbo.tblFinanceRates.HPCommission, dbo.tblFinanceRates.HPRate, dbo.tblFinanceRates.HPAPR,
dbo.tblFinanceRates.LPCommission, dbo.tblFinanceRates.LPRate, dbo.tblFinanceRates.LPAPR, dbo.tblFinanceRates.deposit,
dbo.dealerinfo.landingpages, dbo.dealerinfo.town, dbo.dealerinfo.dealerdropdown
FROM dbo.dealerinfo INNER JOIN
dbo.stock ON dbo.dealerinfo.linkref = dbo.stock.AutoLocateSubscriberID LEFT OUTER JOIN
dbo.tblFinanceRates ON dbo.stock.StockNo = dbo.tblFinanceRates.stockno LEFT OUTER JOIN
dbo.models ON dbo.stock.Manufacturer = dbo.models.fldMake AND dbo.stock.Range = dbo.models.fldmodel
ORDER BY dbo.stock.RetailPrice) AS temptbl
WHERE (RetailPrice BETWEEN 0 AND 100000) AND (landingpages LIKE '%vauxhall') AND (make = 'Vauxhall') AND (town = 'ely')
UNION ALL
SELECT make, model, Derivative, BodyStyle, Doors, FuelType, Transmission, DriveTrain, Gears, ActualColour, ActualTrim, Mileage, RetailPrice, location,
RegDate, RegPlate, StockNo, phone, RegLetter, lifestyle, code, CapID, VehicleType, nonfran, PCPCommission, PCPRate, PCPAPR, HPCommission,
HPRate, HPAPR, LPCommission, LPRate, LPAPR, deposit, landingpages, town, dealerdropdown
FROM (SELECT TOP (100) PERCENT stock_1.Manufacturer AS make, stock_1.Range AS model, stock_1.Derivative, stock_1.BodyStyle, stock_1.Doors,
stock_1.FuelType, stock_1.Transmission, stock_1.DriveTrain, stock_1.Gears, stock_1.ActualColour, stock_1.ActualTrim, stock_1.Mileage,
stock_1.RetailPrice, dealerinfo_1.name AS location, stock_1.RegDate, stock_1.RegPlate, stock_1.StockNo, dealerinfo_1.phone,
stock_1.RegLetter, models_1.fldLifestyle AS lifestyle, dealerinfo_1.code, stock_1.CapID, stock_1.VehicleType, stock_1.nonfran,
tblFinanceRates_1.PCPCommission, tblFinanceRates_1.PCPRate, tblFinanceRates_1.PCPAPR, tblFinanceRates_1.HPCommission,
tblFinanceRates_1.HPRate, tblFinanceRates_1.HPAPR, tblFinanceRates_1.LPCommission, tblFinanceRates_1.LPRate,
tblFinanceRates_1.LPAPR, tblFinanceRates_1.deposit, dealerinfo_1.landingpages, dealerinfo_1.town, dealerinfo_1.dealerdropdown
FROM dbo.dealerinfo AS dealerinfo_1 INNER JOIN
dbo.stock AS stock_1 ON dealerinfo_1.linkref = stock_1.AutoLocateSubscriberID LEFT OUTER JOIN
dbo.tblFinanceRates AS tblFinanceRates_1 ON stock_1.StockNo = tblFinanceRates_1.stockno LEFT OUTER JOIN
dbo.models AS models_1 ON stock_1.Manufacturer = models_1.fldMake AND stock_1.Range = models_1.fldmodel
ORDER BY stock_1.RetailPrice) AS temptbl_1
WHERE (RetailPrice BETWEEN 0 AND 100000) AND (landingpages LIKE '%vauxhall') AND (make = 'Vauxhall') AND (town <> 'ely')

asgast
Posting Yak Master

149 Posts

Posted - 2009-09-30 : 05:48:56
Add order by after the last union, not in the subselect
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 02:01:24
as suggested, you can guarantee order only if order by is specified in outermost select
Go to Top of Page

squarefish
Starting Member

28 Posts

Posted - 2009-10-01 : 03:07:37
I have a table that has data like this

item location
item1 loc1
item2 loc2
item3 loc1
item4 loc3

And I want to be able to sort it by location so any of these outcomes

item1 loc1
item3 loc1
item2 loc2
item4 loc3

or

item2 loc2
item1 loc1
item3 loc1
item4 loc3

or

item4 loc3
item1 loc1
item2 loc2
item3 loc1

Is This possible? I want the selected loc to show first, but within it I want the items for that location sorted by item.

Otherwise if I do the outer order I would just get my original
item1 loc1
item2 loc2
item3 loc1
item4 loc3

Help????
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 03:13:54
its possible. something like

select item,location
from(select item,location,case when location=@loc then 0 else 1 end as ordval
from yourtable
)t
order by ordval,item


loc is the passed on location value
Go to Top of Page

squarefish
Starting Member

28 Posts

Posted - 2009-10-01 : 03:18:19
Yep you lost me at it's possible...!

I don't quite understand the @loc bit?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 03:34:10
@loc is location value that you pass and you want as first item, pass values like loc1,loc2,.. and you can see how it orders
Go to Top of Page

squarefish
Starting Member

28 Posts

Posted - 2009-10-01 : 03:52:26
Visakh16...legend!

It works beautifully!

Thank You
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 04:46:04
welcome
Go to Top of Page
   

- Advertisement -