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:
Related
I'm using union all in my code but one of the tables contains a column that the other one doesn't, how can I insert that column in the output, I don't wanna add it in the table itself.. I need that column to be there and the number of columns should be the same from both tables
Thanks
The normal way to deal with this is to pad the short query with 'dummy' columns.
Suppose TABLE1 has four columns called COL1, COL2, COL3 and COL4.
The TABLE2 3 called COLA, COLB and COLC.
SELECT
COL1
,COL2
,COL3
,COL4
FROM TABLE1
UNION
SELECT
COLA
,COLB
,COLC
,NULL -- or empty string or zero or any padding value compatible with the data-type of COL4.
FROM TABLE2 ;
The results should have 4 columns (COL1 thru COL4) because the column names are taken from the first query.
This practice of padding out data structures to the same signature is common in Databases when combining related but different types and I know it as The Battenberg Manoeuvre.
I would like to generate group row id for each unique rows. Any hint or suggestion would be helpful. I am using SQL Server 2017.
You can use the windowed function ROW_NUMBER() as shown below:
Select col1
, col2
, ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col1, col2) [Group ID]
from <YourTableName>
Db<>Fiddle Demo
For your Case ROW NUMBER Function and use PARTITION by Column Names
SELECT *,ROW_NUMBER () OVER (Partition By COL1 ORDER BY COL1) from #T
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.
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
I have a table in SQL Server that I want to plus amount of a specific column and have the result in next row.
How can I do that?
I want to plus amount of a specific column and have the result in next row.
In case you are looking to insert another from the previous row after adding certain amount, you could use the following:
INSERT INTO MyTable (Col1, Col2, Col3)
SELECT Col1, Col2 + <additional amount>, Col3
FROM MyTable
WHERE
<Criteria to select that row of interest>
In case you are looking to select all the rows in a table and aggregate the amount column and show the result in a separate row, then you could use the following:
SELECT Col1, Col2, Col3 FROM MyTable
UNION
SELECT '', SUM(Col2), '' FROM MyTable