How to combine multiple columns into one column? - sql

I'm writing a query and want the results in one column
My current results return like this
Column1 Column2 column3
1 A CAT
I want the results to return like this
Column1
1
A
CAT

SELECT Column1 FROM TableName
UNION ALL
SELECT Column2 FROM TableName
UNION ALL
SELECT Column3 FROM TableName
If you don't want duplicate values, use UNION instead of UNION ALL.
You can also do this using UNPIVOT operator
SELECT Column123
FROM
(
SELECT Column1, Column2, Column3
FROM TableName
) AS tmp
UNPIVOT
(
Column123 FOR ColumnAll IN (Column1, Column2, Column3)
) AS unpvt;
https://www.w3schools.com/sql/sql_union.asp
https://www.mssqltips.com/sqlservertip/3000/use-sql-servers-unpivot-operator-to-help-normalize-output/

The answer is.. it depends..
If the number of columns are unknown.. then use unpivot as UZI has suggested
if you know all columns and is a small finite set..
you can simply go
Select
column1
from table
union all
select column2
from table
union all
select column3
from table

The Cartesian product of the T table with derived table of 3 rows.(each row of #t is presented 3 times, for а=1 and а=2 and а=3). For the first case we take value from Column1,
and for the second - from Column2 and for the Third - from Column3.
Here, certainly, there is both union and join but, in my opinion, the title's question means single scanning the table.
CREATE TABLE #t (Column1 NVARCHAR(25),Column2 NVARCHAR(25), column3 NVARCHAR(25))
INSERT INTO #t
SELECT '1','A','CAT'
SELECT
CASE a WHEN 1 THEN Column1 WHEN 2 THEN Column2 ELSE column3 END col
FROM #t, (SELECT 1 a UNION ALL SELECT 2 UNION ALL SELECT 3) B
DROP TABLE #t

Related

Subtract 2 rows using case statement in SQL Server 2008

My data is like below, it's in a single table
Column1 Column2
abc 100
abc 200
Now I need like below
abc 100 //here 200-100
I am banging my head on how to achieve this.
I have tried to use the row_number and then subtract using case statement like
Select
column1,
sum(
case when rownum=1
then column2
end
-
case when rownum=2
then column2
end
)
from table
group by column1
But this is giving me null.
Assuming there is no attribute which can define row ordering -
;with cte as(
select
row_number() over (order by (select null)) as IndexId,
Column1,
Column2
from #xyz
)
select sum(case when IndexID=1 then (-1 * Column2) else Column2 end), Column1
from cte
group by Column1
Input data-
declare #xyz table(Column1 varchar(10),Column2 int)
insert into #xyz
select 'abc' ,100 union all
select 'abc' ,200
Assuming you have an attribute rownum in table which is always 1 or 2 (it can be generated by some row_number() as you suggest in question, according to any order that is suitable for you)
Column1 Column2 Rownum
------------------------
abc 100 1
abc 200 2
then you can simply use
Select
column1,
sum(
case when rownum=1
then column2
else -column2
end
)
from table
group by column1
It performs a sum of the Column2 per Column1, however, in the row having rownum = 2 the Column2 value is negated. Therefore in our example you end up with 100 + (-200) = -100
You could do:
select column1, max(column2) - min(column2)
from t
group by column1;
Here is a short form of the answer above if you care:
SELECT
column1,
SUM(IIF(rownum=1,column2,-column2))
FROM table
GROUP BY column1

Create new record if column contains value

Wanted to know if I could "artificially" insert new records when a record contains a value for a specific column. For example say I have this table in my database with the following two records:
Column1 Column2 Column3
-------------------------
DataA1 DataA2 null
DataB1 DataB2 DataB3
Now Column3 is the column I want to trigger an extra row if there is a value. Column3 is essentially Column2 but with another value (this is non-normalized and I can't change it so I need to resort to a query instead). So I want to create a query that returns 3 rows using the example above and it should come out like this:
DataA1 DataA2
DataB1 DataB2
DataB1 DataB3
How do I write my sql to return the results above?
Use union all:
SELECT Column1, Column2
FROM TableName
WHERE Column3 IS NULL
UNION ALL
SELECT Column1, Column3
FROM TableName
WHERE Column3 IS NOT NULL
Not totally sure what you want here but I think you are looking for something like this.
select Column1
, Column2
from SomeTable
where Column2 is not null
UNION ALL
select Column1
, Column3
from SomeTable
where Column3 is not null
You could use a UNION statement to merge a result set that uses the third column as the second column when the third column is not null:
SELECT column1, column2
FROM Sample
UNION
SELECT column1, column3
FROM Sample
WHERE column3 IS NOT NULL
http://sqlfiddle.com/#!9/42ca15/6

SQL comparing all values in a many-one table

I have a table as follows
column1 column2
a 1
a 2
b 2
I need to write an sql query that will go through the table and return me all column 2 values that have both a and b in column 1.
Try this:
SELECT column2
FROM mytable
WHERE column1 IN ('a', 'b')
GROUP BY column2
HAVING COUNT(DISTINCT column1) = 2
Try this:
SELECT column1, column2, ROW_NUMBER()OVER(PARTITION BY column1 Order by column1 )as rowCounts into #tmp2
FROM #tmp
SELECT column2 FROM #tmp2 WHERE rowCounts = 1

Finding rows that have many similar values and one different one

I'm trying to isolate a problem with a violation of a unique key index. I'm pretty certain that the cause is resulting from columns that have the same value in 3 columns not having the same value in the 4th (when they should). As an example...
Key Column1 Column2 Column3 Column4
1 A B C D
2 A B C D
3 A B C D
4 A B C Z
I basically want to select column 4, or some way to let me identify column 4. I know it's a matter of using aggregrate functions but I'm not very familiar with them. Can anyone assist on a way to select Key, Column4 for rows that have a different column 4 value and the same column 1-3 values?
This is what you want:
select column1, column2, column3
from t
group by column1, column2, column3
having min(column4) <> max(column4)
Once you get the right values for the first three columns, you can join back in to get the specific rows.
Or, you can use window functions like this:
select t.*
from (select t.*, min(column4) over (partition by column1, column2 column3) as min4,
max(column4) over (partition by column1, column2 column3) as max4
from t
) t
where min4 <> max4;
If NULL is a valid "other" value that you want to count, you will need additional logic for that.
If you want to get all columns, then (it could be simpler if windowed count supported distinct but it's not):
with cte1 as (
select distinct * from Table1
), cte2 as (
select
*,
count(column4) over(partition by column1, column2, column3) as cnt
from cte1
)
select * from cte2 where cnt > 1;
if you want just to select key:
select
column1, column2, column3
from Table1
group by column1, column2, column3
having count(distinct column4) > 1
sql fiddle demo

SQL Count/Sum displaying column as rows

I have a table with 4 bit columns
I need to create a report that will show the total of all "true" values for each column but I need the column names to return as a row.
For examples, the table will contain:
Column1 Column2 Column3
1 1 0
0 1 0
1 1 0
The result should be:
Category Value
Column1 2
Column2 3
Column3 0
The table has other columns, I just need specific ones
Thanks
I don't know if there are other approaches, but the following should work:
select 'Column1' as "Category", sum(column1) as "Value" from my_table union
select 'Column2', sum(column2) from my_table union
select 'Column3', sum(column3) from my_table
Here's a SQLFiddle for it.
You can try UNPIVOT on the table (this is for SQL Server)
create table Test (Column1 bit, Column2 bit, Column3 bit)
insert into Test values (1,1,0)
insert into Test values (0,1,0)
insert into Test values (1,1,0)
SELECT Value, sum(Vals)
FROM
(CONVERT(INT, Column1) Column1, CONVERT(INT, Column2) Column2, CONVERT(INT, Column3) Column3
FROM Test) p
UNPIVOT
(Vals FOR Value IN
(Column1, Column2, Column3)
)AS unpvt
GROUP BY Value
PIVOT/UNPIVOT documentation
http://sqlfiddle.com/#!6/957c6/1/0
Try this:
select
category = "column1", value = sum (convert(int,col1)) from MyTable1
union
select
category = "column2", value = sum (convert(int,col2)) from MyTable1
union
select
category = "column3", value = sum (convert(int,col3)) from MyTable1