Dear Mike
Thanks for mentioning pivoting as it is what I was trying to recall. But from my Google search, it turns out that in all cases of creating a pivoting table, the number of dimension is already known from a practical perspective of the matter being dealt with and therefore my question about querying the detail table was pointless.
------------------------------
Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
------------------------------
Satid S.
------------------------------
Original Message:
Sent: Wed May 24, 2023 09:50 AM
From: Mike Zaringhalam
Subject: using header row with detail rows in a select
You would have to create a generalized pivot procedure that takes all the distinct values of a column and turns them into separated columns.
When the number of distinct values to pivot is small, then Kent's solution works perfectly fine as long as its kept up to date as new values arise.
-Mike Z
------------------------------
Mike Zaringhalam
Original Message:
Sent: Wed May 24, 2023 02:34 AM
From: Satid Singkorapoom
Subject: using header row with detail rows in a select
Dear Kent
Just curious. David's sample is explicit that there are a max of 4 deduction entries for Emp# 1001. So, you use 4 deduction columns in your query above. But in practice this information is not explicitly known. How do we query the Deduction Table itself to find the maximum count of Ded# entries of a unique Emp# ?
------------------------------
Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
------------------------------
Satid S.
Original Message:
Sent: Tue May 23, 2023 03:22 PM
From: Kent Milligan
Subject: using header row with detail rows in a select
Here's a solution using the ROW_NUMBER specification that should do the trick. The ROW_NUMBER specification is just one of the Advanced SQL features covered inour Expert Labs Data Centric Development enablement offering.
WITH pivot_deductions AS
(SELECT emp#,
MAX(CASE WHEN seqnum = 1 THEN ded# END) AS ded1,
MAX(CASE WHEN seqnum = 2 THEN ded# END) AS ded2,
MAX(CASE WHEN seqnum = 3 THEN ded# END) AS ded3,
MAX(CASE WHEN seqnum = 4 THEN ded# END) AS ded4
FROM (SELECT emp#, ded#, ROW_NUMBER() OVER (PARTITION BY emp# ) AS seqnum FROM deduction) GROUP BY emp#)
SELECT e.emp#, name, ded1, ded2, ded3, ded4
FROM employee e
INNER JOIN pivot_deductions p ON e.emp# = p.emp#;
------------------------------
Kent Milligan
Original Message:
Sent: Tue May 23, 2023 01:54 PM
From: David Strawn
Subject: using header row with detail rows in a select
I have an employee table and a employee deduction table:
Employee table Deduction Table
Emp # Name Emp # Ded #
1001 David S 1001 10
1001 15
1001 16
1001 25
1002 John D 1002 11
1002 18
I am looking for a result set as follows:
result set
Emp # Name ded 1 ded 2 ded 3 ded 4
1001 David S 10 15 16 25
1002 John D 11 18
any way to do this?
------------------------------
David Strawn
------------------------------
#SQL