Using SQL Lateral

Using SQL Lateral

I think we have probably all seen a table that we wish was designed a little differently. Like lets take a Yearly/Quarterly Sales file as an example where the programmer decided instead of making each year/quarter a unique row, they made the table with a year and 4 buckets for each quarter. Or 12 columns for each month. We now need to query by year and quarter. While this is probably a trivial task it would've been easier if I could have just said I want this year and this quarter but now I have to know the column name for each bucket. Here is a way to transform the data vertically instead of horizontal.

So let's first create a simple table.

Create Table qtemp.qtrly_sales (
      year Integer
      ,qtr1 Numeric(11, 2)
      ,qtr2 Numeric(11, 2)
      ,qtr3 Numeric(11, 2)
      ,qtr4 Numeric(11, 2)
    );        

This table contains the Year and 4 columns for each quarter in the year.

Now let's populate the table with some data.

Insert Into qtemp.qtrly_sales
  Values
    (
      2020
      ,40000
      ,35000
      ,15000
      ,50000
     )
    ,(
      2021
      ,75000
      ,80000
      ,95000
      ,85000
     )
    ,(
      2022
      ,125000
      ,130000
      ,145000
      ,150000
     )
    ,(
      2023
      ,115000
      ,120000
      ,175000
      ,203000
     );        

We can now query the table to see the data

select * from qtemp.qtrly_sales        

Now if we want to see the data vertically instead of horizontally we can use Lateral in DB2 to change the data.

Select qs.year
       ,qs.qtr
       ,qs.sales
  From qtemp.qtrly_sales d
       ,Lateral (
         Values
           (
             d.year
             ,1
             ,d.qtr1
            )
           ,(
             d.year
             ,2
             ,d.qtr2
            )
           ,(
             d.year
             ,3
             ,d.qtr3
            )
           ,(
             d.year
             ,4
             ,d.qtr4
            )
       ) As qs (year, qtr, sales);        

We can assign a constant to the QTR column (1,2,3,4) and have the sales amount listed horizontally. You could also change this SQL into a view so that it is reusable. Now you could select for a specific year and quarter easily.


Sergio Pena Rueda

Analista Programador

1 年

Magnífico

回复
Don Anderson

Application Developer at Fred Usinger, Inc.

1 年

Nice! Easy to understand example! Cheers :)

回复

nice one Ricky, cool sample with sql lateral

要查看或添加评论,请登录

Ricky Thompson的更多文章

  • Splitting a PDF with RPG and Java PDFBox based on Text

    Splitting a PDF with RPG and Java PDFBox based on Text

    So this is a follow up article to my previous example of just splitting each page of a PDF document. See the previous…

  • Splitting a PDF with RPG and Java PDFBox

    Splitting a PDF with RPG and Java PDFBox

    I saw a post on Facebook asking if there was a way to split a PDF. While the poster didn't specify how he was splitting…

    9 条评论
  • RPG End Code Blocks - VS Code Extension

    RPG End Code Blocks - VS Code Extension

    I have been trying to use VS Code - Code For IBMi more to get used to it. One of the things I missed in VS Code vs RDi…

    8 条评论
  • DB2 HTTP_POST

    DB2 HTTP_POST

    In the past when I was trying to learn how to use some webservices and there was a site that basically allowed you to…

    3 条评论
  • Part 3: Parsing Complex JSON Objects with DB2 SQL

    Part 3: Parsing Complex JSON Objects with DB2 SQL

    So this credit goes to Birgitta Hauser, I had e-mailed her prior to posting the last two articles asking for some help…

    10 条评论
  • Part 2: Parsing Complex JSON Objects with DB2 SQL

    Part 2: Parsing Complex JSON Objects with DB2 SQL

    In the previous article, it showed how to parse a complex JSON article but I wanted to add how you might process that…

    1 条评论
  • Parsing a Complex JSON with DB2

    Parsing a Complex JSON with DB2

    I recently needed to parse a pretty complex JSON object with DB2 SQL. This isn't the easiest thing in the world to do.

    5 条评论
  • ACS - Run SQL Scripts - Insert From Examples

    ACS - Run SQL Scripts - Insert From Examples

    I'm forever saving SQL Scripts from ACS to a PC file. Not that this is a bad habit or anything but finding them for…

    2 条评论
  • Generating the SQL DDL from an existing table

    Generating the SQL DDL from an existing table

    Every so often I see someone ask how they can generate the DDL from a Table, PF, View, or Procedure . Maybe the source…

    2 条评论
  • ILEastic / JQuery

    ILEastic / JQuery

    Do you remember the days of getting those AS/400 magazines that would have code in them that you could actually use…

    5 条评论

社区洞察