ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

RPGLE SQL join with count

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RPGLE SQL join with count

    I want to join these two queries to be just one query and can never seem to get the correct results. Query1 is for a file that has multiple records for ELER and ELDIVISON and I want the count. Query2 is a file that contains the description for the ELER, ELDIVISON so one record per ELER, ELDIVISON. I still want just the final list to be from Query1 with the list (I dont want every record from FILEB if there are no records in FILEA.

    Any Help Appreciated
    PHP Code:
    Query1:
    Select ELER,         
           
    ELDIVISON,    
           
    count(*)      
    From  FILEA
    Where ELTYPE 
    'I'   
    Group by ELER,       
             
    ELDIVISON   
    Order by ELER
    ,       
             
    ELDIVISON
    Query2:
    PHP Code:
    Select CDDESC                
      from FILEB    
     where CDER   
    = :ELER and   
           
    CDTYPE 'DIV' and    
           
    CDFIVE = :ELDIVISON

  • #2
    Re: RPGLE SQL join with count

    If I understand fully what you are trying to do (and I maybe dont )

    Code:
    select Eler, ElDivision, count(*) from FILEA
     join FILEB  
    on ( FILEA.Eler, FILEA.ElDivision ) =                
       ( FILEB.Cder, FILEB.CDFIVE ) 
    where FILEB.Cder = :Eler AND 
          FILEB.CdType = 'DIV' AND 
          FILEB.Cdfive = :ElDivision
    group by Eler, ElDivision 
    order by Eler, ElDivision
    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

    Comment


    • #3
      Re: RPGLE SQL join with count

      You may also specify your SQL statements either as CTE (Common Table Expressions) or Sub-Selects and Join the Results:

      Code:
      With x as (Select ELER,  ELDIVISON, count(*) Counter     
                     From  FILEA
                     Where ELTYPE = 'I'   
                     Group by ELER, ELDIVISON)
      Select ELER, ELDIVISION, CDDESC, Counter
          From x Join Fileb  on     ELER       = CDER 
                                and ELDIVISION = CDFIVE 
                                and CDTYPE     = 'DIV'
          Order By ELER, ELDIVISION;

      or
      Code:
      Select ELER, ELDIVISION
         From (Select ELER, ELDIVISION, CDDESC, Count(*) Counter
                  From FileA
                  Where ELTYPE = 'I'
                  Group By ELER, ELDIVISION) x
                join FileB   on    ELER       = CDER 
                               and ELDIVISION = CDFIVE 
                               and CDTYPE     = 'DIV'
      Order By ELER, ELDIVISION
      Birgitta
      Last edited by B.Hauser; July 14, 2011, 07:08 AM.

      Comment


      • #4
        Re: RPGLE SQL join with count

        Thanks for the quick reply!!! But it still not quite what I am trying to accomplish

        What I need returned is FILEA.Eler, FILEA.ElDivision, FILEA.count(*) and FILEB.CDDESC

        The examples give me the same result that I get from Query1. That was why I have the query2 to retrieve the description of the division - I was hoping that I could just get the description with the count in one query.

        Comment


        • #5
          Re: RPGLE SQL join with count

          I changed my previous examples, so CDDESC is returned, too.

          Birgitta

          Comment


          • #6
            Re: RPGLE SQL join with count

            I tried something similar to that but even with your changes I get "Column ____ not in specified tables." returned - SQL0206

            First query example I get
            Column ELDIVISION not in specified tables.

            2nd query example I get
            Column CDDESC not in specified tables

            Would it help if I sent some example data for the files?

            Comment


            • #7
              Re: RPGLE SQL join with count

              Here is data and what I would expect returned:

              ELER ELDIVISION ELTYPE
              AAA 111 I
              AAA 111 I
              AAA 222 E
              AAA 222 I
              BBB 111 I
              BBB 444 I
              BBB 444 I
              CCC 333 E
              DDD 111 I


              CDER CDFIVE CDTYPE CDDESC
              AAA 111 DIV Company A Division 1
              AAA 222 DIV Company A Division 2
              BBB 111 DIV Company B Division 1
              BBB 444 DIV Company B Division 4
              CCC 333 DIV Company C Division 3
              DDD 111 DIV Company D Division 1


              Results

              ELER ELDIVISION Count CDDES
              AAA 111 2 Company A Division 1
              AAA 222 1 Company A Division 2
              BBB 111 1 Company B Division 1
              BBB 444 2 Company B Division 4
              DDD 111 1 Company D Division 1

              Comment


              • #8
                Re: RPGLE SQL join with count

                Not sure about the first query error but the 2nd one may be something like this:
                Code:
                Select ELER, ELDIVISION, CDDESC
                   From (Select ELER, ELDIVISION, Count(*) Counter
                            From FileA
                            Where ELTYPE = 'I'
                            Group By ELER, ELDIVISION) x
                          
                    join FileB   on    ELER       = CDER 
                                         and ELDIVISION = CDFIVE 
                                         and CDTYPE     = 'DIV'
                Order By ELER, ELDIVISION
                The reason is that CDDESC is from File B.
                Last edited by kausix777; July 14, 2011, 07:59 AM. Reason: formatting and spelling error
                â??No bird soars too high if he soars with his own wingsâ?? â?? William Blake

                Comment


                • #9
                  Re: RPGLE SQL join with count

                  THANKS!!! I added counter to the first select and it works perfectly.
                  PHP Code:
                  Select ELERELDIVISIONCDDESCCounter
                  From 
                  (Select ELERELDIVISIONCount(*) Counter
                  From FileA
                  Where ELTYPE 
                  'I'
                  Group By ELERELDIVISIONx
                  join FileB on ELER 
                  CDER 
                  and ELDIVISION CDFIVE 
                  and CDTYPE 'DIV'
                  Order By ELERELDIVISION 

                  Comment

                  Working...
                  X