Show particular record in first row in SQL Server - sql

I have a query like this:
Select ShipViaCode
from tbl_VW_Epicor_ShipVia_Master
and I am getting this output:
AIR
CHRT
COMP
CO20
Co40
CSPU
FEDX
NFRT
ser
TRCK
I want to get my first row as 'TRCK', so how can I re-write the query?

Select ShipViaCode from tbl_VW_Epicor_ShipVia_Master order by ShipViaCode desc;
Cheers :)

Select ShipViaCode from tbl_VW_Epicor_ShipVia_Master
order by (case when ShipViaCode = 'TRCK' then 0 else 1 end)

You can sort you result by that column but in descending order,
Select ShipViaCode from tbl_VW_Epicor_ShipVia_Master order by ShipViaCode desc

Related

SQL: Order first by table.column = 'matchingstring', then order everything else alphabetically?

I'm currently using MySQL and I want to order my book_versions records where the book_versions.name = 'paperback' show up first, and then the rest of the book_versions (book_versions.name != 'paperback') show. How would I accomplish this?
order by case when book_versions.name = 'paperback' then 0 else 1 end,
book_versions.name, -- remove this line if other names should not be ordered
book_versions.isbn
See sqlFiddle to see the difference
in mysql, you can also use field
order by field(book_versions.name, 'paperback') DESC,
book_versions.name ASC,
book_versions.isbn ASC
Try:
ORDER BY
CASE WHEN book_versions.name = 'paperback' THEN 0 ELSE 1 END, -- puts paperbacks first (because paperbacks cause this to =0, which is ordered before 1)
book_versions.name -- then order alphabetically

How to return a specific row to the Bottom from a SQL Query results?

I have a simple sql query which return the data as
select [BusinessModel], count(*) As Total from DimHotelExpand
group by [BusinessModel]
The result comes as:
DA 53894
DL 7098
ECM 1472
Flex 14789
GDS 33487
Lead 1499050
MT 71306
Unknown 572467
I want the row with value 'Lead' to appear at the bottom of the result. How can I do this?
You can try this, if you don't know if Lead has always the biggest value:
select
[BusinessModel], count(*) As Total
from DimHotelExpand
group by
[BusinessModel]
order by
case [BusinessModel] when 'Lead' then 1 else 0 end asc
Try this one -
SELECT
d.BusinessModel
, Total = COUNT(1)
FROM dbo.DimHotelExpand d
GROUP BY d.BusinessModel
ORDER BY
CASE WHEN d.BusinessModel = 'Lead' THEN 0 ELSE 1 END
, Total
You can order it by your total column (since Lead's count is higher) like this:
SELECT [BusinessModel], count(*) As Total from DimHotelExpand
GROUP BY [BusinessModel]
ORDER BY count(*)
You can try with "union all".
select [BusinessModel], count(*) As Total from DimHotelExpand
where [BusinessModel] <> 'Lead'
group by [BusinessModel]
union all
select [BusinessModel], count(*) As Total from DimHotelExpand
where [BusinessModel] = 'Lead'
group by [BusinessModel]
A solution with "Order by", I believe don't work fine. Only work fine while "Lead" is more greater bussines model.
I think frikozoid solution is also valid.

SQL - Order By Different case on one column

I have data like this:
I wanna order the data, and the result will be like this:
CHECKING_ACCT_MONTHS--------------------11-201110-201109-2011AVERAGE
in another words, the data will ordered descending, but the AVERAGE data will be at the bottom. How can I do that,.?
Query should be ...
SELECT *
FROM TableName
ORDER BY
CASE
WHEN CHECKING_ACCT_MONTHS = 'AVERAGE'
THEN 1 ELSE 0
END,
CHECKING_ACCT_MONTHS DESC

SQl Query with order by

Can you please help me to form a sql query (MySQL) that allows me to sort the results by order ascending / descending (A- Z / Z - A) and putting in the last lines that have no value.
Less error prone than altering the column of order is:
SELECT
columnOfInterest
FROM
theTable
ORDER BY
CASE WHEN columnOfInterest IS NULL THEN 1 ELSE 0 END CASE
, columnOfInterest
select stateName
from stateTable
order by coalesce(stateName, 'ZZZZZZZZZZZZZZZZZZZZZZZZZ')
You're looking for the 'order by' statement tacked on to your select query.
http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
SELECT HEADER FROM TABLE ORDER BY ELEMENTTOORDER

Order By a field being equal to a specific value?

Let's say I have this MySQL query:
SELECT * FROM A WHERE x='abc' OR y=0;
How can I prioritize the rows so that cases where x='abc' are ordered FIRST? If y=0 but x!='abc', I want those rows to come after cases where x='abc'.
Can this be accomplished with a simple ORDER BY clause?
Thanks!
SELECT *
FROM A
WHERE x='abc'
OR y=0
order by case when x='abc' then 0 else 1 end;
I would do it with a case statement:
SELECT *,
CASE WHEN column_one = 0 THEN 0
WHEN column_two = 'ADMIN' THEN 1
END AS multi_column
FROM sometable
ORDER BY multi_column;
ORDER BY FIELD(x,'abc',x)