Take values of different columns in the same 1 column table - sql

I am working on Ms Sql Server 2008 R2.
now i want one column which has results of 3 different columns of same table..
Let me explain with Figure.
Table: mainTable
Id Column1 Column2 Column3
-------------------------------------
1 urla urlb urlc
2 urld urle urlf
3 urlg urlh urli
Now i want one column
Table Name: ResultTable
Id ColumnResult
-------------
1 urla
2 urlb
3 urlc
4 urld
5 urle
6 urlf
7 urlg
8 urlh
9 urli
Thanks in Advance.
Sahil Patel

You can use UNION ALL:
SELECT
Id = ROW_NUMBER() OVER(ORDER BY ColumnResult),
Column1
FROM (
SELECT Id, Column1 AS ColumnResult FROM mainTable UNION ALL
SELECT Id, Column2 FROM mainTable UNION ALL
SELECT Id, Column3 FROM mainTable
)t

Related

Get unique rows from two tables, but keep duplicates from the same table

I want to split a table into two tables (or more, but let's say two).
table_original
id column1 column2
1 1 2
2 1 3
3 1 4
4 1 4
5 1 5
We can also assume that id is a unique identifier. Now I split this table into two, by using a CREATE TABLE table1 AS SELECT * FROM table_original WHERE column2 <= 4 and CREATE TABLE table2 AS SELECT * FROM table_original WHERE column2 >= 4. Now I have these two tables:
table1
id column1 column2
1 1 2
2 1 3
3 1 4
4 1 4
table2
id column1 column2
3 1 4
4 1 4
5 1 5
How to get the same results from those two tables that I can get from the original table? If I run a query SELECT * FROM table1 UNION SELECT * FROM table2 it will be the same as SELECT * FROM table_original because of the unique id value, however if I run a query SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2 it returns:
column1, column2
1 2
1 3
1 4
1 5
which is not the same as SELECT column1, column2 FROM table_original, which returns:
column1, column2
1 2
1 3
1 4
1 4
1 5
Duplicates from the same table are removed. However, if I wanted to let's say do a count on duplicates, the results will be different, which is bad. So is there a way to do a UNION type operation but keep duplicates that are found in the same table?
not sure what are you trying to achieve but you need to use union all:
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2
union all keeps the duplicates
The UNION on whole rows in your solution will be painfully expensive for big tables (and wide rows). And it fails outright with any column type that doesn't support the equality operator (like json). See:
UNION ALL on JSON data type
This query is substantially faster, making use of the unique index on table1(id). (Create that index if you don't have it!)
SELECT column1, column2
FROM table1 -- bigger table first to micro-optimize some more
UNION ALL
SELECT column1, column2
FROM table2 t2
WHERE NOT EXISTS (SELECT FROM table1 WHERE id = t2.id)
See:
Select rows which are not present in other table
About UNION ALL (as opposed to just UNION):
Is order preserved after UNION in PostgreSQL?
Combining 3 SELECT statements to output 1 table
The question remains: Why keeps completely duplicate rows in multiple tables?
I've figured out the answer.
To keep the duplicates found in the same table, but eliminate everything else, I used a query SELECT column1, column2 FROM (SELECT * FROM table1 UNION SELECT * FROM table2) AS t;
This way the UNION uses the unique id values to eliminate real duplicates, and after that I just filter the result to get the columns I need.

SQL sum values in columns for each row

I have the following table:
column1 | column2 | column3
1 3 4
5 7 6
how do I sum the values of say, column 2 and 3, to return the sum?
The expected result is:
res
7
13
You can do maths within a select statement, so the following will work:
SELECT column2 + column3 AS res FROM table
This works in postgresql.
select sum(col2+col3) from (
select col1, col2,col3,row_number() over() as rows from column_sum ) as foo
group by rows order by rows;

SQL: KEEP Unique value of C1 with Highest value in C2

In my two columns of data I would like to keep only the unique values of ColumnOne that have the highest value in ColumnTwo.
For example
ColumnOne ColumnTwo
2 6
3 2
7 8
2 7
3 4
7 3
I would like the results:
ColumnOne ColumnTwo
2 7
3 4
7 8
You can do this with a group by statement:
select Column1, max(Column2)
from your_table
group by Column1
delete t1
from myTable t1
left join (select t2.Column1, max(t2.Column2) maxColumn2
from myTable t2
group by t2.Column1) tMax
on t1.Column1 = tMax.Column1
and t1.Column2 = tMax.maxColumn2
where tMax.Column1 is null
The below query will help you to accomplish your output for tables with huge no. of records:
create table table1_new as (select * from table1) with no data;
insert into table1_new
select columnone, max(columntwo) over(partition by columnone) from table1 group by columnone;
Validate data and then interchange table names:
drop table table1;
rename table1_new to table1;

SQL Table and Stored Procedure Creation of Total Column

I have created a table in SQL and am trying to retrieve a ROW that returns the Total of all rows in a the value column and names the description value 'Total'. Is this done in the stored procedure?
EX: Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
I want the table to end looking like the following:
Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
4 6/30/14 Total 30.3
Can you please help with how to do this?
Thank you.
Here is an sql statement that does what you ask:
SELECT Column1, Column2, Desc, ValueColumn
FROM
(
SELECT 1 as rolluporder, Column1, Column2, Desc, ValueColumn
FROM TABLE
UNION ALL
SELECT 2 as rolluporder, null as Column1, null as Column2,
'Total' AS Desc, SUM(ValueColumn) as ValueColumn
FROM TABLE
) T
ORDER BY rolluporder, Column1
This looks a little different but I expect it is what you really want:
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
null null Total 30.3
DECLARE #IDC AS INT
SET #IDC = (
SELECT TOP 1 Column1
FROM [yourtable]
ORDER BY 1 DESC
)
SELECT *
FROM [yourtable]
UNION ALL
SELECT #IDC, Column2, 'Total', SUM(ValueColumn)
FROM [yourtable]
GROUP BY Column2

a sql query from oracle

I am using oracle 10g EE database.I have one table mytable and has two columns and data is as follows:
Note: I want to find out data based on same value in 2nd column only, it does not matter whether there exists same or different value in first column.
10 is repeated 3 times for A, B and C and these 3 are required output
similarly 20 is repeated 2 times for C and D and these are also required output
column1 column2
-------------- ---------------
A 10 //required
A 10 //required
B 10 //required
C 20//required
D 20//required
E 30--------not required as 30 is only here and not duplicated
F 40--------not required as 40 is only here and not duplicated
following output is required i.e. same value in 2nd column having same or different values in 1st column
column1 column2
-------------- ---------------
A 10
A 10
B 10
C 20
D 20
SELECT column1,
column2
FROM <table> t1
WHERE column2 IN (SELECT column2
FROM <table> t2
GROUP BY column2
HAVING count(*) > 1);
It sounds like you want
SELECT *
FROM table_name t1
WHERE column2 IN( SELECT column2
FROM table_name t2
GROUP BY column2
HAVING COUNT(*) > 1 )
This appears to work with your sample data
SQL> with table_name as (
2 select 'A' column1, 10 column2 from dual union all
3 select 'A', 10 from dual union all
4 select 'B', 10 from dual union all
5 select 'C', 20 from dual union all
6 select 'D', 30 from dual)
7 SELECT *
8 FROM table_name t1
9 WHERE column2 IN( SELECT column2
10 FROM table_name t2
11 GROUP BY column2
12 HAVING COUNT(*) > 1 );
C COLUMN2
- ----------
B 10
A 10
A 10
select * from table where column2 in ( select column2 from table group by coulmn2 having count(*)>1);
should work for you.
Thanks
Abhi