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);
Related
COL1
COL2
COL3
A
B
A
C
D
C
for example lets say I have a dataset like this. I want to count the values, each value in multiple columns and same rows. As a result it has to say the count of the values I put into.
2A 1B and
2C 1D
Anyone can help?
You don't count values in a row, you count values in a column. So, you use sql to reformat your data in to a single column, then count the values in the usual way.
SELECT
column_value,
COUNT(*)
FROM
(
SELECT col1 AS column_value FROM your_table
UNION ALL
SELECT col2 AS column_value FROM your_table
UNION ALL
SELECT col3 AS column_value FROM your_table
)
AS pivoted
GROUP BY
column_value
ORDER BY
column_value
As I commented, here is the code for the same. Can be optimized a bit by removing multiple select statements.
Declare #val Varchar(MAX);
Select #val=COALESCE(#val + ' ' + result, result)
FROM (
SELECT CONCAT(cnt,col) result FROM (
SELECT col, count(*)cnt FROM (
SELECT col
FROM
(
SELECT col1, col2, col3
from #temp
) AS cp
UNPIVOT
(
col FOR cols IN (col1, col2, col3 )
) AS up)t
group by col)t)t2
select #val
I'm trying (on Impala SQL) to get the rows that have the biggest/ smallest difference between two columns, and I'm trying something like this:
SELECT *
FROM table
WHERE col1 - col2 IN ( SELECT MAX(col1-col2)
FROM table, SELECT MIN(col1-col2) FROM table )
Using only one subquery works, but if I add both of them inside IN it gives an error.
Any suggestions on how I can do this?
Use a subquery join:
SELECT *
FROM table t
JOIN (
SELECT MIN(col1 - col2) AS min_diff, MAX(col1 - col2) AS max_diff
FROM table
) AS agg ON t.col1 - t.col2 IN (agg.min_diff, agg.max_diff)
in your case you cannot use "in" like that you need to join it together or union it as list. I'll show you some example
SELECT * FROM table WHERE col1 - col2 IN ( SELECT MAX(col1-col2) FROM table union SELECT MIN(col1-col2) FROM table)
hop it will help you.
Use union as follows:
SELECT * FROM table WHERE col1 - col2 IN ( SELECT MAX(col1-col2) FROM table
Union
SELECT MIN(col1-col2) FROM table )
-- update
Use rank as follows:
SELECT t.*,
Rank() over (order by col1 - col2) as rn,
Rank() over (order by col1 - col2 desc) as rnd
FROM table t) t
Where rn = 1 or rnd = 1
I will prefer using CTE as shown below:
with difference as
(
select min(col1-col2) minDifference,max(col1-col2) maxDifference
from table
)
select *
from table as t
join difference as d
where t.col1-t.col2 in (d.minDifference,d.maxDifference)
I know that in SQL Server, one can use SELECT clause without FROM clause, and create a table with one row and one column
SELECT 1 AS n;
But I was just wondering, is it possible to use SELECT clause without FROM clause, to create
a table with one column and multiple rows
a table with multiple columns and one row
a table with multiple columns and multiple rows
I have tried many combinations such as
SELECT VALUES(1, 2) AS tableName(n, m);
to no success.
You can do it with CTE and using union(Use union all if you want to display duplicates)
Rextester Sample for all 3 scenarios
One Column and multiple rows
with tbl1(id) as
(select 1 union all
select 2)
select * from tbl1;
One row and multiple columns
with tbl2(id,name) as
(select 1,'A')
select * from tbl2;
Multiple columns and multiple rows
with tbl3(id,name) as
(select 1,'A' union all
select 2,'B')
select * from tbl3;
-- One column, multiple rows.
select 1 as ColumnName union all select 2; -- Without FROM;
select * from ( values ( 1 ), ( 2 ) ) as Placeholder( ColumnName ); -- With FROM.
-- Multiple columns, one row.
select 1 as TheQuestion, 42 as TheAnswer; -- Without FROM.
select * from ( values ( 1, 42 ) ) as Placeholder( TheQuestion, TheAnswer ); -- With FROM.
-- Multiple columns and multiple rows.
select 1 as TheQuestion, 42 as TheAnswer union all select 1492, 12; -- Without FROM.
select * from ( values ( 1, 2 ), ( 2, 4 ) ) as Placeholder( Column1, Column2 ); -- With FROM.
You can do all that by using UNION keyword
create table tablename as select 1 as n,3 as m union select 2 as n,3 as m
In Oracle it will be dual:
create table tablename as select 1 as n,3 as m from dual union select 2 as n,3 as m from dual
You can use UNION operator:
CREATE TABLE AS SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values you can use UNION ALL.
The column names in the result-set are usually equal to the column names in the first SELECT statement in the UNION.
try this:
--1) a table with one column and multiple rows
select * into tmptable0 from(
select 'row1col1' as v1
union all
select 'row2col1' as v1
) tmp
--2) a table with multiple columns and one row
select 'row1col1' as v1, 'row1col2' as v2 into tmptable1
--3) a table with multiple columns and multiple rows
select * into tmptable2 from(
select 'row1col1' as v1, 'row1col2' as v2
union all
select 'row2col1' as v2, 'row2col2' as v2
) tmp
One can create a view and later can query it whenever required
-- table with one column and multiple rows
create view vw1 as
(
select 'anyvalue' as col1
union all
select 'anyvalue' as col1
)
select * from vw1
-- table with multiple columns and multiple rows
create view vw2 as
(
select 'anyvalue1' as col1, 'anyvalue1' as col2
union all
select 'anyvalue2' as col1, 'anyvalue2' as col2
)
select * from vw2
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.
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....
;