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.
Related
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;
How can I get this data set in Image 1 to look like the data in Image 2. Basically rather than having each purchase on its own line I want to group by Name and have all that persons purchases on one line. They can buy a max of 5 items and my database is about 30 million lines worth of purchases.
P.S The date order is not important
You can use row_number() and conditional aggregation:
select name,
max(case when seqnum = 1 then item end) as item_1,
max(case when seqnum = 1 then date end) as date_1,
max(case when seqnum = 2 then item end) as item_2,
max(case when seqnum = 2 then date end) as date_2,
max(case when seqnum = 3 then item end) as item_3,
max(case when seqnum = 3 then date end) as date_3
from (select t.*,
row_number() over (partition by name order by date asc) as seqnum
from t
) t
group by name;
You can use PIVOT with row_number as follows:
Select * from
(select t.*,
row_number() over (partition by name order by date_purchased) rn
from your_table t
) PIVOT
(Max(item_purchased), max(date_purchased) For rn in (1,2,3));
I would like to pivot results from a table into a new structure. So that it can map all the children to the parent product.
Current Result
Parent_Prod_Num|Child_Prod_Num|Child_Prod_Code|Child_Prod_Name
1|11|a123|a
1|12|b123|ab
1|13|c123|abc
Expected Result
Parent_Prod_Num|Child_Prod_Num_1| Child_Prod_Code_1|Child_Prod_Name_1| Child_Prod_Num_2| Child_Prod_Code_2|Child_Prod_Name_2| Child_Prod_Num_3| Child_Prod_Code_3|Child_Prod_Name_3
1|11|a123|a|12|b123|ab|13|c123|abc
For a fixed maximum number of children per parent, you can enumerate the rows with row_number(), then pivot with conditional aggregation:
select parent_prod_num,
max(case when rn = 1 then child_prod_num end) as child_prod_num_1,
max(case when rn = 1 then child_prod_code end) as child_prod_code_1,
max(case when rn = 1 then child_prod_name end) as child_prod_name_1,
max(case when rn = 2 then child_prod_num end) as child_prod_num_2,
max(case when rn = 2 then child_prod_code end) as child_prod_code_2,
max(case when rn = 2 then child_prod_name end) as child_prod_name_2,
max(case when rn = 3 then child_prod_num end) as child_prod_num_3,
max(case when rn = 3 then child_prod_code end) as child_prod_code_3,
max(case when rn = 3 then child_prod_name end) as child_prod_name_3
from (
select t.*, row_number() over(partition by parent_prod_num order by child_prod_num) rn
from mytable t
) t
group by parent_prod_num
I'm trying to convert Rows to Columns from a MS SQL table...
my MS SQL table is like...
I want to SELECT the output as below...
I tried with pivot tables and cross join.. unfortunately could not make it.
any help is highly appreciated
You can use ROW_NUMBER() in a subquery to rank records, and the do conditional aggregation in the outer query:
SELECT
id,
SubId,
MAX(CASE WHEN rn = 1 THEN code END) Code1,
MAX(CASE WHEN rn = 1 THEN TotalAmount END) Code1TotalAmount,
MAX(CASE WHEN rn = 1 THEN TotalDays END) Code1TotalDays,
MAX(CASE WHEN rn = 2 THEN code END) Code2,
MAX(CASE WHEN rn = 2 THEN TotalAmount END) Code2TotalAmount,
MAX(CASE WHEN rn = 2 THEN TotalDays END) Code2TotalDays,
MAX(CASE WHEN rn = 3 THEN code END) Code3,
MAX(CASE WHEN rn = 3 THEN TotalAmount END) Code3TotalAmount,
MAX(CASE WHEN rn = 3 THEN TotalDays END) Code3TotalDays
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY ID, SubId ORDER BY code) rn
FROM mytable t
) x
GROUP BY ID, SubId
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;