Transpose multiple rows to multiple columns with no aggregate using T-SQL - sql

Users have multiple certificates which might be of 3 different types. They may hold multiple certificates of those types. I would like to put them into a single record anticipating that they will have a max of five certificates of each type.
I've written a query that will find the type and put its information into an appropriately named column but still get one row per certificate.
Data shape is:
Name, cert_Type, Cert Name, cert_State, Cert_Expiration
JOE, Equipment, NULL, Operator, 01/30/2022
JOE Equipment, Rigger, 12/31/2021
JOE License, Maryland, 08/12/2025
I'm doing a group by, but still need some aggregating function to get the desired result which might look like this:
| UserName| userID|Cred_Type_1|Cred_1|Type_1_Cred_1_ Expires|Type_1_Cred_2|Type_1_Cred_2_Expires|Cred_type_2|Type2_State |Type_2_expires|
| ----------- | ----------- |-------------|-------------|------------|------------|-----------|-----------|---|---|
|Joe|123|Equipment|Operator|01/30/2022|Rigger|12/31/2021|License | Maryland|08/12/2025|
Note that there is no aggregate here, not counting or averaging or summing. Is there another aggregate function that will do this?

If I understand correctly, you can use row_number() and conditional aggregation:
select userid, username,
max(case when seqnum = 1 then cert_type end),
max(case when seqnum = 1 then cert_name end),
max(case when seqnum = 1 then cert_state end),
max(case when seqnum = 1 then cert_expiration end),
max(case when seqnum = 2 then cert_type end),
max(case when seqnum = 2 then cert_name end),
max(case when seqnum = 2 then cert_state end),
max(case when seqnum = 2 then cert_expiration end),
max(case when seqnum = 3 then cert_type end),
max(case when seqnum = 3 then cert_name end),
max(case when seqnum = 3 then cert_state end),
max(case when seqnum = 3 then cert_expiration end),
from (select t.*,
row_number() over (partition by userid order by cert_expiration desc) as seqnum
from t
) t
group by userid, username;

Related

SQL view on table to combine rows into additional columns

This has stumped me a little, I have a table like this
Id
Address
Address 1
Postcode
1
1 straight street
4 corners
BL51 ANK
1
46 Double Close
Some Place
ZE12 7TB
2
7 The Fields
Farmland
FA7 5ME
I need to create a view that will produce this result:
Id
Address
Address 1
Postcode
Address
Address 1
Postcode
1
1 straight street
4 corners
BL51 ANK
46 Double Close
Some Place
ZE12 7TB
2
7 The Fields
Farmland
FA7 5ME
So basically based on the ID there can be between 4 and 50 rows, I need this returning as a single row with multiple columns containing the different data. I didn't want to do 50 joins as I'm sure there is a smarter way to do this.
Any help is much appreciated.
You can use conditional aggregation (or pivot). Simpler and faster than 50 joins, but still cumbersome:
select id,
max(case when seqnum = 1 then address end),
max(case when seqnum = 1 then address1 end),
max(case when seqnum = 1 then postcode end),
max(case when seqnum = 2 then address end),
max(case when seqnum = 2 then address1 end),
max(case when seqnum = 2 then postcode end),
. . .
max(case when seqnum = 50 then address end),
max(case when seqnum = 50 then address1 end),
max(case when seqnum = 50 then postcode end)
from (select t.*,
row_number() over (partition by id order by (select null)) as seqnum
from t
) t
group by id;
The code is pretty repetitive, so it is simple to generate it in a spreadsheet.

Combine multiple rows to single row group by Specific column

I have a table that looks similar to this here:
I would like to able to combine all that data when they have the same Id.
Which would look like this:
Use conditional aggregation:
select id,
max(case when seqnum = 1 then section end) as section1,
max(case when seqnum = 1 then value1 end) as section1_value1,
max(case when seqnum = 1 then value2 end) as section1_value2,
max(case when seqnum = 2 then section end) as section2,
max(case when seqnum = 2 then value1 end) as section2_value1,
max(case when seqnum = 2 then value2 end) as section2_value2
from (select t.*,
row_number() over (partition by id order by section) as seqnum
from t
) t
group by id;

Pivoting multiple transactions per customer

I have a query which gives customer transactional info. Each customer has multiple transactions and designations these transactions go to.The query outputs CustomerId,Amount,Date,Designation. This has multiple rows for each customer.I want to pivot this so that there is only one row per customer.
I know this is not an ideal way to represent this data, but for the purpose of this particular use case, it has to be in this format. The number of columns will be (max of the number of transactions per customer) X 3
To pivot over a fixed list of transactions per client, you could use row_number() and conditional aggregation:
select
CustomerId,
max(case when rn = 1 then Amount end) Amount1,
max(case when rn = 1 then DateReceived end) DateReceived1,
max(case when rn = 1 then Account end) Account1,
max(case when rn = 2 then Amount end) Amount2,
max(case when rn = 2 then DateReceived end) DateReceived2,
max(case when rn = 2 then Account end) Account2,
max(case when rn = 3 then Amount end) Amount3,
max(case when rn = 3 then DateReceived end) DateReceived3,
max(case when rn = 3 then Account end) Account3
from (
select t.*, row_number() over(partition by CustomerId order by DateReceived) rn
from mytable t
) t
group by CustomerId
This handles up to 3 transactions per client. For more, you can expand the select clause with more tuples of max(case when rn = ... then ... end) expressions.

Split ROWS into multiple COLUMN

I have some information on student name and their roll no -
And I want to split them into 3 sets of column like below. The total no of rows should always be ceiling value of (# of rows/3)
You can do this with conditional aggregation. However, SQL tables represent unordered sets, so which values end up where is arbitrary:
select max(case when seqnum % 3 = 0 then name end) as name_1,
max(case when seqnum % 3 = 0 then roll end) as roll_1,
max(case when seqnum % 3 = 1 then name end) as name_2,
max(case when seqnum % 3 = 1 then roll end) as roll_2,
max(case when seqnum % 3 = 2 then name end) as name_3,
max(case when seqnum % 3 = 2 then roll end) as roll_3
from (select t.*, row_number() over (order by (select null)) - 1 as seqnum
from t
) t
group by floor(seqnum / 3);
If you have an ordering column, then use it instead of (select null).

need to convert data in multiple rows with same ID into 1 row with multiple columns

I reviewed versions of my question already addressed, but some of the good tips I found (using rank() over (partition...) for example, do not seem to work in the Sybase version I am on.
I am hoping to run a procedure that pulls data organized as follows:
Email | Preference
email1 | PreferenceXYZ
email1 | PreferenceABC
And render it in a table like the following:
Email | Preference1 | Preference2
email1 | PreferenceXYZ | PreferenceABC
In essence, I have multiple records for the same person (best identified via email record as a unique identifier) and I want to capture these multiple preferences for a given user and create 1 individual record per user (per email).
If you only have two preferences, then you can use min() and max():
select email, min(preference) as preference1,
(case when min(preference) <> max(preference) then max(preference) end) as preference2
from t
group by email;
EDIT:
If you have up to seven values, then pivot using row_number():
select email,
max(case when seqnum = 1 then preference end) as preference1,
max(case when seqnum = 2 then preference end) as preference2,
max(case when seqnum = 3 then preference end) as preference3,
max(case when seqnum = 4 then preference end) as preference4,
max(case when seqnum = 5 then preference end) as preference5,
max(case when seqnum = 6 then preference end) as preference6,
max(case when seqnum = 7 then preference end) as preference7
from (select t.*, row_number() over (partition by email order by preference) as seqnum
from t
) t
group by email;
EDIT II:
You can actually do this with a correlated subquery instead of row_number():
select email,
max(case when seqnum = 1 then preference end) as preference1,
max(case when seqnum = 2 then preference end) as preference2,
max(case when seqnum = 3 then preference end) as preference3,
max(case when seqnum = 4 then preference end) as preference4,
max(case when seqnum = 5 then preference end) as preference5,
max(case when seqnum = 6 then preference end) as preference6,
max(case when seqnum = 7 then preference end) as preference7
from (select t.*,
(select count(*)
from t t2
where t2.email = t.email and
t2.preference <= t.preference
) as seqnum
from t
) t
group by email;