SQL Server unpivot columns - sql

I have a table that I would like to unpivot in a SQL statement. It consists of a person and phone 1 through 5. Right now I'm doing a union for each phone but I fear it is causing performance issues.
Columns:
PERSON_GUID,
PHONE_1, PHONE_1_VOICE_FLG,
PHONE_2, PHONE_2_VOICE_FLG,
PHONE_3, PHONE_3_VOICE_FLG,
PHONE_4, PHONE_4_VOICE_FLG,
PHONE_5, PHONE_5_VOICE_FLG
How would I best unpivot the row with performance in mind so that the results are:
PERSON_GUID, PHONE_NO, VOICE_FLG

I prefer UNPIVOT but as for your solution -
Make sure you are using UNION ALL and not UNION.
UNION ALL just spills one query result after the other.
UNION eliminates rows duplications and this is where you pay in performance.
select PERSON_GUID,PHONE_NO,
case right(col,1)
when 1 then PHONE_1_VOICE_FLG
when 2 then PHONE_2_VOICE_FLG
when 3 then PHONE_3_VOICE_FLG
when 4 then PHONE_4_VOICE_FLG
when 5 then PHONE_5_VOICE_FLG
end VOICE_FLG
from t unpivot (PHONE_NO for col in
(PHONE_1,PHONE_2,PHONE_3,PHONE_4,PHONE_5)) u

Related

How can I convert 2 row into column in tsql?

I have 2 row data which I want to make it to be 2 column,
I tried union syntax but it didn't work.
Here is the data I have:
breed 1 breed2
I tried to convert it with this sql
select a.breed union a.breed
but it didn't work.
Here is what you want from the SQL:
breed1,breed2
SELECT
[breed1],
[breed2]]
FROM
(
SELECT 'breed1' myColumn
union
select 'breed2'
) AS SourceTable
PIVOT
(
AVG(mySecondColumn) FOR
myColumn IN ([breed1], [breed2]])
) AS PivotTable;
You can use a self join. This needs a way to pair rows together (so if you have four rows you get 1 and 2 in one result and 3 and 4 in the other rather than another combination).
I'm going to assume you have sequentially numbered rows in an Id column and an odd numbered row is paired with the one greater even Id:
select odd.Data as 'First', even.Data as 'Second'
from TheData odd
inner join TheData even on odd.Id+1 = even.Id
where odd.Id % 2 = 1;
More generally for more columns use of pivot is more flexible.
How about an aggregation query?
select min(breed) as breed1, max(breed) as breed2
from t;

SQL convert column names to row values

I have a table organized as follows:
Account Location Measure1 Measure2 Measure3
-------------------------------------------
123a A 100 20% 5
234b A 75 80% 8
I want to create records as follows:
Account Location Measure Value
-----------------------------------
123a A Measure1 100
123a A Measure2 20%
123a A Measure3 5
234b A Measure1 75
234b A Measure2 80%
234b A Measure3 8
Because my measure names are the column headings and not column values under a heading called "Measure" I cannot pivot the data on the measure name.
I know how to query what the column names are by querying INFORMATION_SCHEMA.COLUMNS. But I'm not sure how to proceed from there. I don't want to do a Union because there are about 100+ measure columns and the table is large.
The only assistance I have been able to find on the web refers to splitting values in a single column (e.g. semi-colon delimited strings) into multiple records. UNPIVOT doesn't work because again the measure name is not a value in a column, it is a column heading.
I would appreciate any assistance you can give me
A simple method that works in most databases uses union all:
select account, location, 'Measure1', Measure1
from t
union all
select account, location, 'Measure2', Measure2
from t
union all
select account, location, 'Measure3', Measure3
from t;
In databases that support lateral joins, there is more convenient syntax, such as:
select t.account, t.location, v.measure, v.value
from t cross join lateral -- or maybe cross apply
(values ('Measure1', t.Measure1), ('Measure2', t.Measure2), ('Measure3', t.Measure3)
) v(measure, value);

How can I combine multiple columns of the same data into a single column?

I have an issue here that is arising from poor data formatting (not on my behalf). I had a large CSV file downloaded from an external entity with nation wide data - it has about 5,000,000+ rows so that its too large of a file to open, let alone manually manipulate the data. I did get it uploaded to our SQL database, but getting the data into a usable format is difficult; each row has 10 different category codes, and can have multiple codes in each category. Unfortunately, they added new columns to handle this instead of adding a new row. Its tough to describe without an example:
ID A_Code1 A_Code2 A_Code3 B_Code1 B_Code2 B_Code3
1 123 765 654 qwe asd zxc
2 987 345 567 poi lkj mnb
and this is what I need:
ID A_Code B_Code
1 123 qwe
1 765 asd
1 654 zxc
2 987 poi
2 345 lkj
2 567 mnb
The way it is set up now makes querying nearly impossible as there are about 10 different types of on each row, and there are 10 columns for each code type. This means I have to query 100 different columns when I should only have to query 10.
If somebody knows a way to do this, it would be greatly appreciated. I have not been able to find anything like this so far, so I am getting desperate!
Thank you!
You need to unpivot the multiple columns of data into multiple rows, depending on your version of SQL Server there are several ways to get the result.
You can use CROSS APPLY and UNION ALL if using SQL Server 2005+:
select id, A_Code, B_Code
from yourtable
cross apply
(
select A_Code1, B_Code1 union all
select A_Code2, B_Code2 union all
select A_Code3, B_Code3
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
You can also use CROSS APPLY with VALUES if using SQL Server 2008+:
select id, A_Code, B_Code
from yourtable
cross apply
(
values
(A_Code1, B_Code1),
(A_Code2, B_Code2),
(A_Code3, B_Code3)
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
This allows you to convert the columns into rows in pairs - meaning A_Code1 and B_Code1 will be matched in the final result.
You could also use a UNION ALL:
select id, A_Code = A_Code1, B_Code = B_Code1
from yourtable
union all
select id, A_Code = A_Code2, B_Code = B_Code2
from yourtable
union all
select id, A_Code = A_Code3, B_Code = B_Code3
from yourtable ;
See SQL Fiddle with Demo

How to group by a column

Hi I know how to use the group by clause for sql. I am not sure how to explain this so Ill draw some charts. Here is my original data:
Name Location
----------------------
user1 1
user1 9
user1 3
user2 1
user2 10
user3 97
Here is the output I need
Name Location
----------------------
user1 1
9
3
user2 1
10
user3 97
Is this even possible?
The normal method for this is to handle it in the presentation layer, not the database layer.
Reasons:
The Name field is a property of that data row
If you leave the Name out, how do you know what Location goes with which name?
You are implicitly relying on the order of the data, which in SQL is a very bad practice (since there is no inherent ordering to the returned data)
Any solution will need to involve a cursor or a loop, which is not what SQL is optimized for - it likes working in SETS not on individual rows
Hope this helps
SELECT A.FINAL_NAME, A.LOCATION
FROM (SELECT DISTINCT DECODE((LAG(YT.NAME, 1) OVER(ORDER BY YT.NAME)),
YT.NAME,
NULL,
YT.NAME) AS FINAL_NAME,
YT.NAME,
YT.LOCATION
FROM YOUR_TABLE_7 YT) A
As Jirka correctly pointed out, I was using the Outer select, distinct and raw Name unnecessarily. My mistake was that as I used DISTINCT , I got the resulted sorted like
1 1
2 user2 1
3 user3 97
4 user1 1
5 3
6 9
7 10
I wanted to avoid output like this.
Hence I added the raw id and outer select
However , removing the DISTINCT solves the problem.
Hence only this much is enough
SELECT DECODE((LAG(YT.NAME, 1) OVER(ORDER BY YT.NAME)),
YT.NAME,
NULL,
YT.NAME) AS FINAL_NAME,
YT.LOCATION
FROM SO_BUFFER_TABLE_7 YT
Thanks Jirka
If you're using straight SQL*Plus to make your report (don't laugh, you can do some pretty cool stuff with it), you can do this with the BREAK command:
SQL> break on name
SQL> WITH q AS (
SELECT 'user1' NAME, 1 LOCATION FROM dual
UNION ALL
SELECT 'user1', 9 FROM dual
UNION ALL
SELECT 'user1', 3 FROM dual
UNION ALL
SELECT 'user2', 1 FROM dual
UNION ALL
SELECT 'user2', 10 FROM dual
UNION ALL
SELECT 'user3', 97 FROM dual
)
SELECT NAME,LOCATION
FROM q
ORDER BY name;
NAME LOCATION
----- ----------
user1 1
9
3
user2 1
10
user3 97
6 rows selected.
SQL>
I cannot but agree with the other commenters that this kind of problem does not look like it should ever be solved using SQL, but let us face it anyway.
SELECT
CASE main.name WHERE preceding_id IS NULL THEN main.name ELSE null END,
main.location
FROM mytable main LEFT JOIN mytable preceding
ON main.name = preceding.name AND MIN(preceding.id) < main.id
GROUP BY main.id, main.name, main.location, preceding.name
ORDER BY main.id
The GROUP BY clause is not responsible for the grouping job, at least not directly. In the first approximation, an outer join to the same table (LEFT JOIN below) can be used to determine on which row a particular value occurs for the first time. This is what we are after. This assumes that there are some unique id values that make it possible to arbitrarily order all the records. (The ORDER BY clause does NOT do this; it orders the output, not the input of the whole computation, but it is still necessary to make sure that the output is presented correctly, because the remaining SQL does not imply any particular order of processing.)
As you can see, there is still a GROUP BY clause in the SQL, but with a perhaps unexpected purpose. Its job is to "undo" a side effect of the LEFT JOIN, which is duplication of all main records that have many "preceding" ( = successfully joined) records.
This is quite normal with GROUP BY. The typical effect of a GROUP BY clause is a reduction of the number of records; and impossibility to query or test columns NOT listed in the GROUP BY clause, except through aggregate functions like COUNT, MIN, MAX, or SUM. This is because these columns really represent "groups of values" due to the GROUP BY, not just specific values.
If you are using SQL*Plus, use the BREAK function. In this case, break on NAME.
If you are using another reporting tool, you may be able to compare the "name" field to the previous record and suppress printing when they are equal.
If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an ORDER BY for the same columns. To avoid the overhead of sorting that GROUP BY produces, add ORDER BY NULL:
SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;
Relying on implicit GROUP BY sorting in MySQL 5.6 is deprecated. To achieve a specific sort order of grouped results, it is preferable to use an explicit ORDER BY clause. GROUP BY sorting is a MySQL extension that may change in a future release; for example, to make it possible for the optimizer to order groupings in whatever manner it deems most efficient and to avoid the sorting overhead.
For full information - http://academy.comingweek.com/sql-groupby-clause/
SQL GROUP BY STATEMENT
SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups.
Syntax:
1. SELECT column_nm, aggregate_function(column_nm) FROM table_nm WHERE column_nm operator value GROUP BY column_nm;
Example :
To understand the GROUP BY clauserefer the sample database.Below table showing fields from “order” table:
1. |EMPORD_ID|employee1ID|customerID|shippers_ID|
Below table showing fields from “shipper” table:
1. | shippers_ID| shippers_Name |
Below table showing fields from “table_emp1” table:
1. | employee1ID| first1_nm | last1_nm |
Example :
To find the number of orders sent by each shipper.
1. SELECT shipper.shippers_Name, COUNT (orders.EMPORD_ID) AS No_of_orders FROM orders LEFT JOIN shipper ON orders.shippers_ID = shipper.shippers_ID GROUP BY shippers_Name;
1. | shippers_Name | No_of_orders |
Example :
To use GROUP BY statement on more than one column.
1. SELECT shipper.shippers_Name, table_emp1.last1_nm, COUNT (orders.EMPORD_ID) AS No_of_orders FROM ((orders INNER JOIN shipper ON orders.shippers_ID=shipper.shippers_ID) INNER JOIN table_emp1 ON orders.employee1ID = table_emp1.employee1ID)
2. GROUP BY shippers_Name,last1_nm;
| shippers_Name | last1_nm |No_of_orders |
for more clarification refer my link
http://academy.comingweek.com/sql-groupby-clause/

Calculating a field from SQL Query Selecting from multiple tables with Union

I have the following query, which works great. The problem I have is that in both tables (and the aggregate unioned table), there is a field called MTGUID. I need to multiply MTGUID by a number (let's say 1.35, for ease of use) and have it return that number in the MTGUID field. I have tried a dozen ways to do this and can't get anything to play ball. I can create a new column for each calculated price, like (BKRETAIL.MTGUID * 1.35) AS MTG1, but we've got tens of thousands of lines of code that specifically use MTGUID. Any ideas?
I'm using Firebird SQL.
SELECT * FROM (
SELECT BKRETAIL.* FROM BKRETAIL WHERE BKRETAIL.MKEY='SOMEKEY'
UNION SELECT BKWHOLESALE.* FROM BKWHOLESALE WHERE MKEY='SOMEKEY')
ORDER BY
case STATUS
WHEN 'RT' then 1
WHEN 'WH' then 2
WHEN 'OL' then 3
WHEN 'OD' then 4
WHEN NULL then 5
else 6
end;
How about this:
SELECT MTGUID * 1.35 as calculatedMTGUID, SUBSEL.* FROM (
SELECT BKRETAIL.* FROM BKRETAIL WHERE BKRETAIL.MKEY='SOMEKEY'
UNION SELECT BKWHOLESALE.* FROM BKWHOLESALE WHERE MKEY='SOMEKEY') SUBSEL
ORDER BY
case STATUS
WHEN 'RT' then 1
WHEN 'WH' then 2
WHEN 'OL' then 3
WHEN 'OD' then 4
WHEN NULL then 5
else 6
end;
try this
SELECT MTGUID * 1.35 AS MTGUID,<list rest OF COLUMNS here>
FROM (
SELECT BKRETAIL.* FROM BKRETAIL WHERE BKRETAIL.MKEY='SOMEKEY'
UNION SELECT BKWHOLESALE.* FROM BKWHOLESALE WHERE MKEY='SOMEKEY')
ORDER BY
case STATUS
WHEN 'RT' then 1
WHEN 'WH' then 2
WHEN 'OL' then 3
WHEN 'OD' then 4
WHEN NULL then 5
else 6
end;
One option would be to replace the original MTGUID column with computed one, ie
rename the original MTGUID column in table(s);
add new MTGUID column with desired expression using COMPUTED BY (expr);
Advantage of this is that you don't have to alter your SQL statements, disadvantage is that you have to maintain the expression in many places (all the tables which have the column). Of course, the queries which need the original MTGUID value must be updated to use the renamed column, but if the number of such statements is significantly lower it could be worth the trouble.
I think a better solution would be to "hide" all this stuff behind a view but this requires alerting your SQL queries...