SQL: Lookup table rows into columns for reporting purposes - sql

I have the following two table data structure for dealing with custom user fields:
[UserFieldID] [UserFieldName]
-------------------------------
1 Location
2 Color
[UserID] [UserFieldID] [UserFieldValue]
----------------------------------------
1 1 Home
1 2 Orange
2 1 Office
2 2 Red
This allows any number of fields to be defined (globally) and users to have values for those custom fields. I need to figure out how to display this information for reporting purposes as part of a pre-existing report, in the following format:
UserID ... Location Color
----------------------------------------------------
1 Home Orange
2 Office Red
I know this probably involves using either PIVOT or UNPIVOT, but try as I might, they just confuse me.
Thanks in advance

There are several different ways that you can get the result, you can use an aggregate function with a CASE expression or you can use the PIVOT function to get this. Based on your comment that any number of fields can be defined, it sounds like you will need to use dynamic SQL to get the final result. Before writing a dynamic SQL version, I would always start with a static or hard-coded version of the query, then convert it to dynamic SQL.
Besides using these methods, I would also recommend using the windowing function row_number() to generate a unique value for each combination of userid and fieldname. Since you are pivoting string values, then you have to use either the max/min aggregate function which will return only one value for each fieldname, by adding the row_number you will be able to return multiple combinations of Location, etc for each user.
If you were using an aggregate function with a CASE expression the query would be:
select
userid,
max(case when userfieldname = 'Location' then userfieldvalue end) location,
max(case when userfieldname = 'Color' then userfieldvalue end) Color
from
(
select v.userid,
f.userfieldname,
v.userfieldvalue,
row_number() over(partition by v.userid, v.userfieldid
order by v.userfieldid) seq
from userFields f
left join userValues v
on f.userfieldId = v.userFieldId
) d
group by userid, seq
order by userid;
See SQL Fiddle with Demo
If you were using PIVOT, the hard-coded version of the query would be:
select userid, Location, Color
from
(
select v.userid,
f.userfieldname,
v.userfieldvalue,
row_number() over(partition by v.userid, v.userfieldid
order by v.userfieldid) seq
from userFields f
left join userValues v
on f.userfieldId = v.userFieldId
) d
pivot
(
max(userfieldvalue)
for userfieldname in (Location, Color)
) p
order by userid;
See SQL Fiddle with Demo.
Once you have the correct logic you can convert the PIVOT to dynamic SQL to be executed:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(UserFieldName)
from UserFields
group by UserFieldName, userfieldId
order by userfieldid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT userid, ' + #cols + '
from
(
select v.userid,
f.userfieldname,
v.userfieldvalue,
row_number() over(partition by v.userid, v.userfieldid
order by v.userfieldid) seq
from userFields f
left join userValues v
on f.userfieldId = v.userFieldId
) x
pivot
(
max(userfieldvalue)
for userfieldname in (' + #cols + ')
) p
order by userid'
execute sp_executesql #query;
See SQL Fiddle with Demo. All versions will give a result:
| USERID | LOCATION | COLOR |
|--------|----------|--------|
| 1 | Home | Orange |
| 1 | Office | (null) |
| 2 | Office | Red |

Related

Concatenate multiple rows from inside a correlated 'group by' subquery into a single text string

Similar questions have been asked before but I am specifically looking for an answer to do much the same with a correlated subquery.
I am doing this on SQL Server, and I cannot utilize stored procedure or temp table creation approach.
For those familiar with Client Matter billing; I have formulated a 'group by' query using row_number technique to return me back the top 3 performers for each unique clientmatter, summing their amounts over a period of time.
This gives me something like this:
clientmatterno attorneyname amount seq_num
111111.00001 John Doe $30,000 1
111111.00001 Mark Tim $23,000 2
111111.00001 Jane Sue $15,000 3
111111.00001 Mary Ann $5,000 4
222221.00501 John Doe $35,000 1
222221.00501 David Hu $30,000 2
444444.00003 Shelly Y $50,000 1
I think, I would have to first do a group by clause to sum up the amounts for each attorney in order to find the totals and hence get the correct seq_num to appear across.
I am now trying to use this subquery results to do the string concatenation such that I get the following results:
111111.00001 John Doe|Mark Tim|Jane Sue
222221.00501 John Doe|David Hu
444444.00003 Shelly Y
The Query that I think will work, seeing past questions on this topic:
select subq.clientmatterno as [Id],
,
STUFF(
(SELECT DISTINCT ',' + subq.attorneyname
FROM ????
WHERE ????
FOR XML PATH (''))
, 1, 1, '') AS TopPerformers
from (
SELECT clientmatterno, attorneyname, sum(amount),
row_number() over (partition by clientmatterno order by sum(amount) desc) as seq_num
FROM ...
WHERE ...
GROUP BY clientmatterno, attorneyname
) as subq
where seq_num <= 3
group by clientmatterno
My problem is on how to connect and build up the STUFF function. The error is very simple: I cannot seem to use the subquery set 'subq' in the FROM clause inside the STUFF function.
I have not tried out XML FOR Auto approach.
Try using a common table expression instead of a derived table:
with cte as (
SELECT
clientmatterno,
attorneyname,
sum(amount) amount,
seq = row_number() over (partition by clientmatterno order by sum(amount) desc)
FROM ...
WHERE ...
GROUP BY clientmatterno, attorneyname
)
SELECT
clientmatterno,
STUFF(
(
SELECT '|' + attorneyname
FROM cte
WHERE clientmatterno = a.clientmatterno
AND seq <= 3
FOR XML PATH ('')
), 1, 1, ''
) AS Attorneynames
FROM cte AS a
GROUP BY clientmatterno

SQL Server: Select multiple records in one select statement

In a query like this one:
SELECT *
FROM `Order`
WHERE `CustID` = '1'
My results are displayed like so:
| CustID| Order |
-----------------
| 1 | Order1|
| 1 | Order2|
| 1 | Order3|
-----------------
How do I write SQL statement, to get a result like this one?:
| CustID| Order |
---------------------------------
| 1 | Order1, Order2, Order3|
---------------------------------
In mySQL it's possible with Group_Concat, but in SQL Server it gives error like syntax error or some.
Use xml path (see fiddle)
SELECT distinct custid, STUFF((SELECT ',' +[order]
FROM table1 where custid = t.custid
FOR XML PATH('')), 1, 1, '')
FROM table1 t
where t.custid = 1
STUFF replaces the first , with an empty string, i.e. removes it. You need a distinct otherwise it'll have a match for all orders since the where is on custid.
FOR XML
PATH Mode
STUFF
You can use Stuff function and For xml clause like this:
SELECT DISTINCT CustId, STUFF((
SELECT ','+ [Order]
FROM [Order] T2
WHERE T2.CustId = T1.CustId
FOR XML PATH('')
), 1, 1, '')
FROM [Order] T1
fiddle here
Note: Using order as a table name or a column name is a very, very bad idea. There is a reason why they called reserved words reserved.
See this link for my favorite way to avoid such things.
try this.
Change table name and column names for what you need;
SELECT custID,
LISTAGG(Order, ', ') WITHIN GROUP (ORDER BY Order) text
FROM table_name
GROUP BY custID
edit for MSSQL . You should use group_concat function.
SELECT custID, GROUP_CONCAT(Order)
FROM table_name
WHERE CustID = 1
GROUP BY custID;

Single Column as N Rows [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 7 years ago.
I have a large table with 3 columns as follows:
Invoice Product Color
1 Pant Red
1 Pant Black
1 Shirt Green
2 Pant White
2 Pant Black
2 Pant Blue
I'd like to group on Invoice & Product and then have all unique Color values appear on the related grouped record as follows:
Invoice Product Colour1 Colour2 Colour3
1 Pant Red Black
1 Shirt Green
2 Pant White Black Blue
Is this possible in SQL Server?
It is possible in SQL Server -- if you know that there are three color columns. If there are a variable number, then it is still possible, but it requires dynamic SQL.
I would approach this using conditional aggregation:
select invoice, product,
max(case when seqnum = 1 then colour end) as colour1,
max(case when seqnum = 2 then colour end) as colour2,
max(case when seqnum = 3 then colour end) as colour3
from (select t.*,
row_number() over (partition by invoice, product order by (select nULL)) as seqnum
from table t
) t
group by invoice, product;
To convert rows into columns, you need to use Pivot in Sql Server. If you know the number of columns in advance, you can use pivoting statically as the answer suggested by Gordin Linoff.
Sometimes, the number of colors may vary(in your example there are only 3 colors). In such case, you cannot hardcode the column names. For that first of all you need to get columns names dynamically into a variable.
DECLARE #cols NVARCHAR (MAX)
SELECT #cols = COALESCE (#cols + ',[' + COLUMNNAME + ']', '[' + COLUMNNAME + ']')
FROM
(
SELECT DISTINCT
'COLOR'+CAST(ROW_NUMBER() OVER(PARTITION BY INVOICE,PRODUCT ORDER BY (SELECT 0)) AS VARCHAR(10)) COLUMNNAME
FROM #TEMP
) PV
ORDER BY COLUMNNAME
Now the above variable have values of columns as Comma Separated Values which can be used with IN operator dynamically for the below query. Since your table doesn't have values like COLOR1, COLOR2 etc, I have provided logic to get column names for each INVOICE and its PRODUCT using PARTITION BY clause.
DECLARE #query NVARCHAR(MAX)
SET #query = '-- This outer query forms your pivoted result
SELECT * FROM
(
-- Source data for pivoting
SELECT DISTINCT INVOICE,PRODUCT,COLOR,
''COLOR''+CAST(ROW_NUMBER() OVER(PARTITION BY INVOICE,PRODUCT ORDER BY (SELECT 0)) AS VARCHAR(10)) COLUMNNAME
FROM #TEMP
) x
PIVOT
(
--Defines the values in each dynamic columns
MIN(COLOR)
-- Get the names from the #cols variable to show as column
FOR COLUMNNAME IN (' + #cols + ')
) p
ORDER BY INVOICE;'
EXEC SP_EXECUTESQL #query
Click here to view result

Splitting values in one column to 2 columns

I need to do the following in sql.
table 1:
Year Client Investment
1999 X 100
1999 Y 200
2000 X 1000
2000 Y 2000
I want to display it in below format for my report:
Client 1999Year 2000Year
X 100 1000
Any idea how to do the above?
I am using sql server 2008
Please help.
This type of data transformation is known an a PIVOT. Some database products have a function that will turn the data from rows to columns.
You can use an aggregate function with a CASE expression in any database:
select client,
sum(case when year = 1999 then investment end) Year_1999,
sum(case when year = 2000 then investment end) Year_2000
from yourtable
group by client
See SQL Fiddle with Demo
Since you are using SQL Server 2008, you can use the PIVOT function to transform the data into columns:
select *
from
(
select client,
'Year_'+cast(year as varchar(4)) year,
investment
from yourtable
) src
pivot
(
sum(investment)
for year in (Year_1999, Year_2000)
) piv
See SQL Fiddle with Demo.
The other queries will work great if you have a known number of year values, but if you have an unknown number, then you will want to use dynamic SQL:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME('Year_'+cast(year as varchar(4)))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT client,' + #cols + ' from
(
select client,
''Year_''+cast(year as varchar(4)) year,
investment
from yourtable
) x
pivot
(
sum(investment)
for year in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo.
This can also be done by joining on the table multiple times:
select t1.client,
t1.investment Year_1999,
t2.investment Year_2000
from yourtable t1
left join yourtable t2
on t1.client = t2.client
and t2.year = 2000
where t1.year = 1999
See SQL Fiddle with Demo
All queries give the result:
| CLIENT | YEAR_1999 | YEAR_2000 |
----------------------------------
| X | 100 | 1000 |
| Y | 200 | 2000 |
There are many possible solutions for this. One is by using MAX() and CASE,
SELECT Client,
MAX(CASE WHEN YEAR = 1999 THEN Investment END) [1999Year],
MAX(CASE WHEN YEAR = 2000 THEN Investment END) [2000Year]
FROM TableName
WHERE Client = 'X'
GROUP BY Client
SQLFiddle Demo
or using PIVOT function
SELECT Client,
[1999] AS [1999YEAR],
[2000] AS [2000YEAR]
FROM
(
SELECT YEAR, CLient, Investment
FROM TableName
WHERE Client = 'X'
) pvt
PIVOT
(
MAX(InvestMent)
FOR YEAR IN ([1999],[2000])
) s
SQLFiddle Demo

SQL Query - Display Count & All ID's With Same Name

I'm trying to display the amount of table entries with the same name and the unique ID's associated with each of those entries.
So I have a table like so...
Table Names
------------------------------
ID Name
0 John
1 Mike
2 John
3 Mike
4 Adam
5 Mike
I would like the output to be something like:
Name | Count | IDs
---------------------
Mike 3 1,3,5
John 2 0,2
Adam 1 4
I have the following query which does this except display all the unique ID's:
select name, count(*) as ct from names group by name order by ct desc;
select name,
count(id) as ct,
group_concat(id) as IDs
from names
group by name
order by ct desc;
You can use GROUP_CONCAT for that
Depending on version of MSSQL you are using (2005+), you can use the FOR XML PATH option.
SELECT
Name,
COUNT(*) AS ct,
STUFF((SELECT ',' + CAST(ID AS varchar(MAX))
FROM names i
WHERE i.Name = n.Name FOR XML PATH(''))
, 1, 1, '') as IDs
FROM names n
GROUP BY Name
ORDER BY ct DESC
Closest thing to group_concat you'll get on MSSQL unless you use the SQLCLR option (which I have no experience doing). The STUFF function takes care of the leading comma. Also, you don't want to alias the inner SELECT as it will wrap the element you're selecting in an XML element (alias of TD causes each element to return as <TD>value</TD>).
Given the input above, here's the result I get:
Name ct IDs
Mike 3 1,3,5
John 2 0,2
Adam 1 4
EDIT: DISCLAIMER
This technique will not work as intended for string fields that could possibly contain special characters (like ampersands &, less than <, greater than >, and any number of other formatting characters). As such, this technique is most beneficial for simple integer values, although can still be used for text if you are ABSOLUTELY SURE there are no special characters that would need to be escaped. As such, read the solution posted HERE to ensure these characters get properly escaped.
Here is another SQL Server method, using recursive CTE:
Link to SQLFiddle
; with MyCTE(name,ids, name_id, seq)
as(
select name, CAST( '' AS VARCHAR(8000) ), -1, 0
from Data
group by name
union all
select d.name,
CAST( ids + CASE WHEN seq = 0 THEN '' ELSE ', ' END + cast(id as varchar) AS VARCHAR(8000) ),
CAST( id AS int),
seq + 1
from MyCTE cte
join Data d
on cte.name = d.name
where d.id > cte.name_id
)
SELECT name, ids
FROM ( SELECT name, ids,
RANK() OVER ( PARTITION BY name ORDER BY seq DESC )
FROM MyCTE ) D ( name, ids, rank )
WHERE rank = 1