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.
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:
I am trying to run an insert query on a SQL wide table (Sparse Table), but I am getting an error:
"Column name or number of supplied values does not match table definition"
Query:
Insert [dbo].[Table1] (Table1 is a sparse table)
select id, [A],[B], [C], [D], [E] from (
Select ID,
CategoryId,
1 as Flag
From dbo.table2
) a Pivot(Avg(Flag) For CategoryID In (
[A],
[B],
[C],
[D],
[E]
)) As PivotTable
I am able to run the query for a normal sql table but it fails for a sparse table. I would really appreciate any help on this
Thanks in Advance
You should do:
Create an INSERT statement that explicitly lists the columns it will insert into - assuming that ID might be an IDENTITY column
that you don't want / can't insert into
Define the exact number of values to fill into these columns
your INSERT statement should be something like:
insert into table_1 (cola, colb, colc)
select cola, colb, colc from table_2
insert into tb1 values('1', '2','3') - this works fine as long you only have 3 columns
if you have 4 columns but only want to insert into 3 of them.
You have to include column names in INSERT INTO
insert into tb1 (Col1,col2,col3) select col1, col2, col3 from tb_2
Note: Always explicitly define the list of columns that an INSERT statement should fill data into
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.
Hello I would like to insert in a table called Data multiple columns from another table called SourceTable and one colum that has a standar value for every row added in Data.
Assume that you have Column1 and Column2 in the table called SourceTable and source_id is precalculated and it will be the same for every row added into Data on this query.
INSERT INTO Data (Columns1, Column2, source_id)
SELECT Column1, Column2
FROM SourceTable
UNION SELECT 2;
I tried this one but is not working, most likely because the SELECT 2 returns only one row.
Your issue is that you're giving SQL 3 columns to insert 2 values into, if source_id is going to be 2 as your union selects then you'd want something like this;
INSERT INTO Data (Columns1, Column2, source_id)
SELECT Column1, Column2, 2
FROM SourceTable
The number of columns you're inserting needs to match the number of columns that you're inserting to. The way you were doing it would have produced this result;
Column1 Column2 source_id
Value1 Value2
2
but even the union would have failed as the queries that you're unioning need to have the same number of columns.
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