create table in oracle from operations on columns in other table - sql

I have a table (say table_1) having some columns of type number. Now i want to create other table (say table_2) having columns (name, sum, avg, max, min) which will store computed value of columns from the table_1.
Right now i'm creating table_2 and then inserting row in table_2 for each column in table_1 one at a time.
I want to do this in a single statement update. Query Like: "Create table_2(name, sum, avg, ...) select ....".
Please help me creating the execute statement.

This is an UNPIVOT operation.
SELECT colname, SUM(value), AVG(value), MIN(value), MAX(value)
FROM table1
UNPIVOT ( value FOR colname IN (x,y,..) )
GROUP BY colname
Where "x,y,..." should be the actual column names in your source table.
Edited to add
In pre-11g versions of Oracle, you can roll your own unpivot. Example for two columns:
WITH driver AS (
SELECT level colnum FROM dual CONNECT BY level <= 2
)
SELECT
CASE WHEN colnum=1 THEN 'x' WHEN colnum=2 THEN 'y' END colname,
CASE WHEN colnum=1 THEN sum_x WHEN colnum=2 THEN sum_y END colsum,
CASE WHEN colnum=1 THEN avg_x WHEN colnum=2 THEN avg_y END colavg
FROM driver
CROSS JOIN (
SELECT SUM(x) sum_x, AVG(x) avg_x, SUM(y) sum_y, AVG(y) avg_y
FROM mytable
)
ORDER BY colnum

That's legitimate: you can do
CREATE TABLE myTable as
SELECT....
;
or even using a common table expression:
CREATE TABLE myTable as
WITH myCte as
(
SELECT....
)
SELECT....
;

Related

How to find maximum value in entire table using SQL

I have a table with multiple columns and wanted to find the maximum value in the entire table (across all columns), let me know if it is possible? if yes how
All columns are in integer data type
You can use MAX and GREATEST to achieve this.
Data Set:
CREATE TABLE test
(
col1 INTEGER,
col2 INTEGER,
col3 INTEGER
);
INSERT INTO test VALUES
(1,100,2 ),(2,300,3 ),(3,350, 400 );
You can achieve it using below.
SELECT Greatest(Max(col1), Max(col2), Max(col3)) as Max_Value
FROM test;
DB Fiddle: Try it here
In Postgres and Oracle (and I believe in MySQL as well) you can use:
select max(greatest(col_1, col_2, col_3, col_4))
from the_table;
Supose that you have these columns:
ID (PK)
Column 1 (int)
Column 2 (int)
Column 3 (int)
You can use a SELECT with a UNION clause inside it, something like this:
SELECT ID, MAX(FindNumber) AS FoundedNumber
FROM
(
SELECT ID, Column1 AS FindNumber
FROM YourTable
UNION
SELECT ID, Column2 AS FindNumber
FROM YourTable
UNION
SELECT ID, Column3 AS FindNumber
FROM YourTable
) subselect
GROUP BY ID
This solution is for Microsoft SQL Server.

Defining a subtable and then query from that table using SQL

I have a table with many columns, and I want to count the unique values of each column. I know that I can do
SELECT sho_01, COUNT(*) from sho GROUP BY sho_01
UNION ALL
SELECT sho_02, COUNT(*) from sho GROUP BY sho_02
UNION ALL
....
Here sho is the table and sho_01,.... are the individual columns. This is BigQuery by the way, so they use UNION ALL.
Next, I want to do the same thing, but for a subset of sho, say SELECT * FROM sho WHERE id in (1,2,3). Is there a way where I can create a subtable first, and then query the subtable? Something like this
SELECT * FROM (SELECT * FROM sho WHERE id IN (1,2,3)) AS t1;
SELECT sho_01, COUNT(*) from t1 GROUP BY sho_01
UNION ALL
SELECT sho_02, COUNT(*) from t1 GROUP BY sho_02
UNION ALL
....
Thanks
Presumably, the columns are all of the same type. If so, you can simplify this using arrays:
select el.which, el.val, count(*)
from (select t1.*,
array[struct('sho_01' as which, sho_01 as val),
struct('sho_2', show_02),
. . .
] as ar
from t
) t cross join
unnest(ar) el
group by el.which, el.val;
You can then easily filter however you want by adding a where clause before the group by.
Below is for BigQuery Standard SQL and allows you to avoid manual typing of column names or even knowing them in advance
#standardSQL
SELECT
TRIM(SPLIT(kv, ':')[OFFSET(0)], '"') column,
SPLIT(kv, ':')[OFFSET(1)] value,
COUNT(1) cnt
FROM `project.dataset.table` t,
UNNEST(SPLIT(TRIM(TO_JSON_STRING(t), '{}'))) kv
GROUP BY column, value
-- ORDER BY column, value

I want to retrieve max value from two column - SQL table

Please find attached image for table structure
You could try using UNION ALL for build a unique column result and select the max from this
select max(col)
from (
select col1 col
from trans_punch
union all
select col2
from trans_punch) t
You can use a common table expression to union the columns, then select the max.
;with cteUnionPunch(Emp_id, both_punch) AS
(
SELECT Emp_id, In_Punch FROM trans_punch
UNION ALL
SELECT Emp_id, Out_Punch FROM trans_punch
)
SELECT Emp_id, max(both_punch) FROM cteUnionPunch GROUP BY Emp_id
You can use apply :
select tp.Emp_id, max(tpp.Punchs)
from trans_punch as tp cross apply
( values (In_Punch), (Out_Punch) ) tpp(Punchs)
group by tp.Emp_id;

remove duplicate values of one column by sql query

I have a table with two column, values of one column is null and another column have many duplicated values and blanks. how can I remove duplicated values and blanks from that column by a query?
You can use temporary table for this task as below:
SELECT DISTINCT * INTO #tmpTable FROM MyTable
TRUNCATE TABLE MyTable
INSERT INTO MyTable SELECT * FROM #tmpTable
You can create an empty copy of the table. Then you run an INSERT INTO new_table SELECT DISTINCT * FROM old_table. Finally, drop the old table and rename the new one.
One way of doing this - using CTE
create table #dups (col1 int, col2 int)
insert into #dups
select null,null union all
select null,1 union all
select null,1 union all
select null,1 union all
select null,2 union all
select null,2 union all
select null,3 union all
select null,null
select * from #dups
;WITH cte
AS (SELECT col1,col2,ROW_NUMBER() OVER (PARTITION BY Col1,Col2
ORDER BY ( SELECT 0)) RN
FROM #dups
)
DELETE FROM cte
WHERE RN > 1 OR col2 is null
1) If you want to keep the row with the lowest id value:
DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name
2) If you want to keep the row with the highest id value:
DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name
Please get the copy of your table before processing this.
if don't get than send me table name i will put replace your table name.
It is tested with MySQL 5.1
Not sure about other versions
see the link
Delete all Duplicate Rows except for One in MySQL?
If I understood right, you want to avoid for the column2 NULL and blanks?
SELECT COLUMN1, COLUMN2 FROM TABLE
GROUP BY
COLUMN1, COLUMN2
WHERE
COLUMN2 NOT NULL AND COULMN2 <> ''
This query is going to show results only when the COLUMN2 has some data.
try this
DELETE FROM [Table]
WHERE (ColmnB IN
(SELECT ColumnB
FROM [Table] AS Table_1
GROUP BY ColumnB
HAVING (COUNT(*) > 1))) OR
(RTRIM(LTRIM(ISNULL(ColumnB,''))) = '')
table has tow column . first column is null value . second column has duplicate value and blank value.

How to create select query to view multiple columns for values?

COUNT(DATECREATED)
88708
26625
17092
how to create a select query for viewing this three values in single column as different column names like
COUNT(DATECREATED) COUNT(DATECREATED) COUNT(DATECREATED)
88708 26625 17092
88708
88708
If you just want 1 row with 3 rows as columns, then use this. Here col is the alias for your count statement.
with tbl as
(select rownum as rno,col from
(your existing query) t
)
select (select col from tbl where rno=1) col1 ,
(select col from tbl where rno=2) col2,
(select col from tbl where rno=3 ) col3
from dual
If there are more rows and you want to use them as column, then read about Dynamic pivot in Oracle and you shall get your answer.
Finally i did like this.. Thanks all for quick reply
select sum(colname) as aliasname, sum(colname) as aliasname, sum(colname) as aliasname from
(select count(colname) as colname,0 as colname,0 as colname from cof
union all
select 0,count(colname),0 from cof where colname is not null
union all
select 0,0,count(colname) from cof where colname is not null);