I have a sql select statement which contains some columns that are computed from some other columns or tables. I gave a name for this column using As keyword.
Now, I want to sort this table by the computed column.
I cant use that name for sorting.
Someone please help to sort the sql table using computed column.
In older versions of SQL Server, you can define the alias in a subquery:
select *
from (
select col1 + col2 as col3
from YourTable
) SubQueryAlias
order by
col3
In SQL Server 2008+ you should be able to order by an alias without a subquery:
select col1 + col2 as col3
from YourTable
order by
col3
one more option you can use COLUMN INDEX NUMBER in order by as show in follwoing example
select ACol,AVal,CAST(ACol as varchar(3)) + aval as 'New' from ABC order by 3
this will use 'New' columnd to sort
Related
Is there any way I can select from a table without specifying the order by column in the order by clause?
select col1 from table order by col2
This works in TSQL, but doesn't appear to be allowed in Snowflake.
Yes, this is possible:
CREATE OR REPLACE TABLE tab AS
SELECT 1 AS col1, 'B' AS col2 UNION ALL
SELECT 2, 'A';
SELECT col1
FROM tab
ORDER BY col2;
Output:
Azure SQL Server 2019.
We have a table Table1 with over 100 columns of differing types of nvarchar data, all of which are allowed NULL values, and where there could be anywhere from 1 to 100 columns populated in a given record. I need to formulate a query that returns the rows ranked by how many columns have values in them, in descending order.
I started going down a road of using DATALENGTH and having to type out the name of every single column, but I can only imagine there has to be a more efficient way. Assuming the column names are column1, column2, column3 etc, how would I accomplish this?
How about a lateral join that unpivots the columns to rows? This requires enumerating the columns just once, like so:
select t.*, c.cnt
from mytable t
cross apply (
select count(*) cnt
from (values (t.column1), (t.column2), (t.column3)) x(col)
where col is not null
) c
order by c.cnt desc
I have a query as:
SELECT T1.col1, T1.col2, T2.col3, T2.col4 from Table1 T1 join Table2 T2
This returns the columns(T1,T2,T3,T4) as expected.
col1 col2 col3 col4
A 11 0.5 B
Now i want to add a column to the result set with out modifying the above Select statement.
I want output result set should have another column(col8) from Table1 but i cannot add this column(col8) to the exisiting Select statement.
col1 col2 col3 col4 col8
A 11 0.5 B 9
I know there is Union in SQL server. But Union requires
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order.
I want to achieve similar to UNION but with out above conditions. I want only column to be added to the result set.
Is this achievable in SQL Server?
Any help?
Thanks in advance.
Is there a work around with this type of sort in Microsoft SQL sever 2008?
Just want it to sort with my temp column.
SELECT x AS TempTest FROM table1 WHERE TempTest IS NOT NULL
I can never remember which versions support referencing aliased columns, but a surefire method is to use a derived table.
Select * From
(
SELECT x AS TempTest FROM table1
)MyTempTable
WHERE TempTest IS NOT NULL
Order By TempTest
All you're doing is aliasing an existing column, so just sort by the original column name. Alternatively, you could sort by the column ordinal position.
SELECT x AS TempTest FROM table1 WHERE x IS NOT NULL ORDER BY 1
I used following query in SQL Server 2000
SELECT
U.FirstName,
SUM(VE.Score)AS Score, SUM(VE.QuizTime) AS Time,
SUM(VE.IsQuizType) AS QuizesAttempted,
SUM(VE.IsProgrammingType) AS ProgrammingProblemsAttempted
from
Users U INNER JOIN VirtualExercise VE on U.UserID=VE.UserID
where U.UserID IN( 10,11 ) AND ProgramID = 2
group by U.FirstName
order by VE.Score desc
It working fine in SQL Server 2000 but not working in SQL Server 2005.
Gives following error:
Column "VirtualExercise.Score" is
invalid in the ORDER BY clause because
it is not contained in either an
aggregate function or the GROUP BY
clause. --- Inner Exception
Please help...
SQL Server 2005 is correct: you are trying to order by a value that doesn't exist in the output because you must either GROUP on it or aggregate it. VE.Score is neither. SQL Server 2000 allowed some ambiguities like this (edit: see "ORDER BY clause" at http://msdn.microsoft.com/en-us/library/ms143359(SQL.90).aspx for info on this)
I assume you mean
ORDER BY SUM(VE.Score) DESC
SQL Server 2000 is kind of dumb where it comes to table and column aliases in the result set. Provided an explicit alias column is introduced (e.g. Score in your case, Col3 in the example below), it doesn't matter what table alias you use in the ORDER BY clause:
create table #T1 (
ID int not null primary key,
Col1 varchar(10) not null
)
go
create table #T2 (
ID int not null primary key,
Col2 varchar(10) not null
)
go
insert into #T1 (ID,Col1)
select 1,'abc' union all
select 2,'def'
go
insert into #T2 (ID,Col2)
select 1,'zyx' union all
select 2,'wvu'
go
select *,1 as Col3 from #T1 t1 inner join #T2 t2 on t1.ID = t2.ID order by t2.Col3
Since you presumably want to sort by the computed Score column, just remove the VE. prefix.
Since you have defined SUM(VE.Score)AS Score in select , do you mean ORDER BY Score DESC