Why We Can't Select Data From Temporary Table into another Temporary Table - sql

My Code Like
SELECT col1, Col2, Col3, Col4
INTO #Temp1
FROM Emplyees;
and second Query :
SELECT Col1,Sum(Col2)
INTO #Temp2
FROM #temp1
The Second Query Don't Work and gave me Error as Empty or Wrong Aliases column
untill you Select * From #Temp1 .
This make me ASK why sql accepted * and not for some column Selected .
Thanks For all.

SELECT Col1, Sum(Col2) as SumofCol2
INTO #Temp2
FROM #temp1
GROUP BY Col1

Well the error is a bit misleading. I would expect it to moan about the lack of a group by clause and the lack of a real name for the second column. Try this:
SELECT Col1, Sum(Col2) AS SumCol
INTO #Temp2
FROM #temp1
GROUP BY Col1

Related

How to use values from another table in a SELECT statement?

select max(select col0 from table0) as col0, col1, col2
from table1
I hope it is clear from my "pseudo code" what I am trying to do. I basically want to add one single value from table0 to table1 so all rows of the first column is the same value.
But Toad cannot execute this form of code. Can you suggest a way for me to solve my problem?
The aggregate functions goes within the subquery, not around it:
select
(select max(col0) from table0) col0,
col1,
col2
from table1
select (select max(col0) from table0) as col0, tb1.col1, tb1.col2 from table1 tb1

merge two queries with different where and different grouping into 1

Sorry, I asked this question just before and got some good answers but then I realised I made a mistake with the query in question, if I change the question in the original post that could make the answers invalid so I'm posting again with the right query this time, please forgive me, I hope this is acceptable.
DECLARE #Temp TABLE
(MeasureDate, col1, col2, type)
INSERT INTO #Temp
SELECT MeasureDate, col1, col2, 1
FROM Table1
WHERE Col3 = 1
INSERT INTO #Temp
SELECT MeasureDate, col1, col2, 3
FROM Table1
WHERE Col3 = 1
AND Col4 = 7000
SELECT SUM(col1) / SUM(col2) AS Percentage, MeasureDate, Type
FROM #Temp
GROUP BY MeasureDate, Type
I do two inserts into the temp table, 2nd insert with an extra WHERE but same columns same table, but different type, then I do SUM(col1) / SUM(col2) on the temp table to return the result I need per MeasureDate and type. Is there a way to merge all these inserts and selects into one statement so I don't use a temp table and do a single select from Table1? Or even if I still need the temp table, merge the selects into one select instead of two separate selects? Stored procedure works fine as it is, just looking for a way to shorten it.
Thanks.
Sure can. I might start with combining the two queries from your inserts using UNION ALL (this variation of UNION will not remove duplicates), wrapped up in a CTE from which you can perform your final query:
WITH MeasureData(MeasureDate, col1, col2, type) AS (
SELECT MeasureDate, col1, col2, 1
FROM Table1
WHERE Col3 = 1
UNION ALL
SELECT MeasureDate, col1, col2, 3
FROM Table1
WHERE Col3 = 1
AND Col4 = 7000
)
SELECT SUM(col1) / SUM(col2) AS Percentage, MeasureDate, Type
FROM MeasureData
GROUP BY MeasureDate, Type
That's it, no more table variable or insert statements.
No real need for a UNION, you can handle this with a CASE statement:
SELECT SUM(col1) / SUM(col2) AS Percentage, MeasureDate, Type
FROM (
SELECT MeasureDate, col1, col2, case when Col4 = 7000 then 3 else 1 end type
FROM Table1
WHERE Col3 = 1
) t
GROUP BY MeasureDate, Type
Edit, as Gordon correctly points out, for Type = 1, this query wouldn't produce the same results. Here's a variation on Gordon's good answer that might be easier to visually understand using a CROSS JOIN and IF logic:
SELECT T1.MeasureDate,
T.Type,
SUM(IF(T.Type=1,Col1,IF(T.Type=3 AND T1.Col4=7000,T1.Col1,0))) /
SUM(IF(T.Type=1,Col2,IF(T.Type=3 AND T1.Col4=7000,T1.Col2,0))) AS Percentage
FROM Table1 T1
CROSS JOIN (SELECT 1 Type UNION SELECT 3) T
WHERE T1.Col3 = 1
GROUP BY T1.MeasureDate, T.Type
Condensed SQL Fiddle
Your method is double counting cases where col3 = 1 and col4 = 7000. Here is a method that takes this into account, without union on the overall table:
select t.type, SUM(t1.col1) / SUM(t1.col2) AS Percentage, t1.MeasureDate, t.Type
from table1 t1 join
(select 1 as type union all
select 3 as type
) t
on t.type = 1 or t1.col4 = 7000
where t1.col3 = 1
group by measuredate, type;

db2 select distinct rows, but select all columns

Experts, I have a single table with multiple columns. col1, col2, col3, col4, col5, col6
I need to select distinct (col4), but I need all other columns also on my output.
If I run, this ( select distinct(col4 ) from table1 ), then I get only col4 on my output.
May I know, how to do it on db2?.
Thank you
You simply do this...
Select * From Table1 Where col4 In (Select Distinct(col4) From Table1)
I'm not sure if you will be able to do this.
You might try to run group by on this column. You will be able to run some aggregate functions on other columns.
select count(col1), col4 from table1 group by (col4);
none of the answers worked for me so here is one that i got working. use group by on col4 while taking max values of other columns
select max(col1) as col1,max(col2) as col2,max(col3) as col3
, col4
from
table1
group by col4
At least in DB2, you can execute
SELECT
DISTINCT *
FROM
<YOUR TABLE>
Which will give you every distinct combination of your (in this case) 6 columns.
Otherwise, you'll have to specify what columns you want to include. If you do that, you can either use select distinct or group by.

SELECT INTO with HSQLDB

I am trying to create a new table from the result of a select. This works fine with SQL Server:
SELECT * INTO newTable FROM (SELECT col1, col2, col3 FROM oldTable) x;
Now, I want to achieve the exact same thing with HSQLDB (Version 2.2). I have tried several forms like
SELECT * INTO newTable FROM (SELECT col1, col2, col3 FROM oldTable);
SELECT INTO newTable FROM SELECT col1, col2, col3 FROM oldTable;
CREATE TABLE newTable AS SELECT col1, col2, col3 FROM oldTable;
All these variants result in some form of syntax error. How can I create a table from a select with HSQLDB?
The manual has an example for this:
CREATE TABLE t (a, b, c) AS (SELECT * FROM atable) WITH DATA
HSQLDB requires parentheses around the select (unlike all other DBMS) and it also requires the WITH DATA clause
Ok I found very easier way to do this.
select * into t_bckp FROM t;
Its interesting.

join two tables into one big table

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:
SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2
However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).
May it work to just do:
SELECT col1, col2, col3
INTO Table1
FROM Table2
Start with union all:
select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2
Your query is trying to deduplicate things, which would slow it down considerably.
I think the best option is to create a view in sql server, this will optimize the performance of the query:
SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2
(As other users said: "union" is used to select distinct values from two tables
where as "union all" is used to select all values including
duplicates from the tables.)
At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.
You could use this to fill the second table:
Insert into table2 select * from table1;
Or if you want to be more specific:
Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;
(Note: some DBMSs might require putting parenthesis around the SELECT clause.)
select * into new table(your new table name)
from table1.col1,table1.col2,table2.col1;
here columns can be your required columns .
select * into newtable from table1
union all
select * from table2
Worked well. Guidelines, both tables have exact same column names :)