Programming Languages on Power

Power Programming Languages

IBM Power, including the AIX, IBM i, and Linux operating systems, support a wide range of programming languages, catering to both traditional enterprise applications and modern development needs.


#Power

 View Only
Expand all | Collapse all

using header row with detail rows in a select

  • 1.  using header row with detail rows in a select

    Posted Tue May 23, 2023 01:55 PM

    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


  • 2.  RE: using header row with detail rows in a select

    Posted Tue May 23, 2023 03:22 PM

    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# ENDAS ded1,
           MAX(CASE WHEN seqnum = 2 THEN ded# ENDAS ded2,
           MAX(CASE WHEN seqnum = 3 THEN ded# ENDAS ded3,
          MAX(CASE WHEN seqnum = 4 THEN ded# ENDAS 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
    ------------------------------



  • 3.  RE: using header row with detail rows in a select

    Posted Tue May 23, 2023 04:33 PM

    Thanks!  I will try to get this to work on the real data...

    where do i find/register for this lab?



    ------------------------------
    David Strawn
    ------------------------------



  • 4.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 02:41 AM

    Dear David

    >>>> where do i find/register for this lab? <<<<

    The training class that Kent and his colleagues deliver is of the chargeable face-to-face type, not of the on-line self study type.  You can make a request for such a training from his team by using Contact Us hot link from this web page IBM Expert Labs client training at https://www.ibm.com/products/expertlabs/training
     



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 5.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 09:29 AM

    The enablement workshops are NOT limited to face-to-face. The online workshops are not self-study, but they can be done remotely.
    Descriptions can be found at:  ibm.biz/Db2iExpertLabs



    ------------------------------
    Kent Milligan
    ------------------------------



  • 6.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 02:35 AM

    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.
    ------------------------------



  • 7.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 09:50 AM

    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
    ------------------------------



  • 8.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 11:07 PM

    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.
    ------------------------------



  • 9.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 06:06 PM

    This should do it:
    db2 => select count(deduct_amt) from deductions where emp_no = 1001;

    1
    -----------
              4

      1 record(s) selected.

    If you want to know how many deductions each employee made:
    db2 => select emp_no, count(deduct_amt) from deductions group by emp_no;

    EMP_NO      2
    ----------- -----------
           1001           4
           1002           2
           1003           1
           1004           3

      4 record(s) selected.


    I added emp_no 1003 and 1004 to David's initial set of data.

    scot




  • 10.  RE: using header row with detail rows in a select

    Posted Wed May 24, 2023 08:47 AM

    If you don't need separate columns for each deduction you can try:

    select e.emp, e.name, listAgg( d.deduction, ' ' ) as Deductions
    from testEmp e
    join testDed d on d.emp = e.emp
    group by e.emp, e.name;

    Result:
    EMP    NAME    DEDUCTIONS
    1001    David S 10 15 16 25
    1002    John D   11 18




    ------------------------------
    Patrick Conner
    ------------------------------