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


#Servers
#Programminglanguages
 View Only
  • 1.  Grouping using window function or similar based on ending text.

    Posted 04/17/23 04:02 PM

    Say I have the following data set. I want to be able to group based upon the wrap up date. Wrap up signals the end of a group.   There will be multiple customers in the table with similar data.

    Customer TransDate TransCode
    1 1/13/2023 Data1    
    1 1/26/2023 data2
    1 1/27/2023 data3
    1 1/27/2023 WRAP-UP        
    1 2/10/2023 data4
    1 2/10/2023 data5
    1 2/10/2023 data6
    1 2/20/2023 WRAP-UP        
    1 3/22/2023 data7
    1 3/22/2023 data8
    1 3/22/2023 data9
    1 3/24/2023 data10
    1 3/30/2023 WRAP-UP        
    1 3/31/2023 data11
    1 4/1/2023 data12


    In other words I want to produce the following data but I can't get the SQL right. I am using db2 DSN12015.  Any help to produce the output below using SQL would be helpful.  TIA!

    Customer TransDate TransCode Group
    1 1/13/2023 Data1     1
    1 1/26/2023 data2 1
    1 1/27/2023 data3 1
    1 1/27/2023 WRAP-UP         1
    1 2/10/2023 data4 2
    1 2/10/2023 data5 2
    1 2/10/2023 data6 2
    1 2/20/2023 WRAP-UP         2
    1 3/22/2023 data7 3
    1 3/22/2023 data8 3
    1 3/22/2023 data9 3
    1 3/24/2023 data10 3
    1 3/30/2023 WRAP-UP         3
    1 3/31/2023 data11 4
    1 4/1/2023 data12 4






    ------------------------------
    Christopher Harmon
    ------------------------------

    #SQL


  • 2.  RE: Grouping using window function or similar based on ending text.

    Posted 04/17/23 04:35 PM

    For something like this, you should be able to do a subquery to count how many 'WRAP-UP' records were written before the current record.
    Based on the data provided, im not 100% sure what fields control the order of records, but the following query is based on the date controlling the order of records.
    If its customer and date, then add customer to the where clause.

    select customer, 
               transaction_date,
               code,
               (select count(*)
                from qtemp/a as a
                where code = 'WRAP-UP'
                  and b.transaction_date > a.transaction_date) + 1 as group
        from qtemp/a as b

    This query produced these results from the temporary table i built.


    If this table isn't keyed or has something to help control the order, then you may need to use the RRN function to provide a key value to each row.
    I can provide an example of this if needed.

    -Mike Z



    ------------------------------
    Mike Zaringhalam
    ------------------------------



  • 3.  RE: Grouping using window function or similar based on ending text.

    Posted 04/18/23 11:34 AM

    Thanks much for the information Mike.  

    Yes I think I will need the additional RRN coding as there are multiple customers and multiple transaction dates not necessarily in order in the source table.  The order by would be Customer and Transaction Date.  Thanks again.



    ------------------------------
    Christopher Harmon
    ------------------------------



  • 4.  RE: Grouping using window function or similar based on ending text.

    Posted 04/18/23 12:39 PM

    Below is the example query using RRN.
    We select every row from the table while applying a relative record number (RRN). This is essentially a temporary key so we know the exact order of records.
    Once we have that temporary table, the final select is similar to the previous answer except it uses the row_num field to look for previous 'WRAP-UP' records.


    with 
        row_numbers as (
            select RRN(a) row_num,
                   customer,
                   transaction_date,
                   code
            from qtemp/a
        )
        
        select customer, 
               transaction_date,
               code,
               (select count(1) + 1
                from row_numbers b
                where code = 'WRAP-UP'
                  and b.row_num < a.row_num) as group
        from row_numbers a;

    -Mike Z



    ------------------------------
    Mike Zaringhalam
    ------------------------------