Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58

    Unanswered: GROUP BY with if/else

    I have this group statement:
    GROUP BY calc_inh.kwaliteit_id, calc_id.kwaliteit_id_std, calc_inh.dikte

    What I need is the next statement but I don't know how to achive this in onlly one query:

    If calc_inh.kwaliteit_id > 0
    GROUP BY calc_inh.kwaliteit_id, calc_inh.dikte

    else
    GROUP BY calc_id.kwaliteit_id_std, calc_inh.dikte

    Any ideas?

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    Hi,

    your question is a little vague. I think what you are looking for here is a change in the group by depending on some value. Where is that value coming from? It might be better to provide the table definitions and the full query?
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58
    Quote Originally Posted by it-iss.com View Post
    Hi,I think what you are looking for here is a change in the group by depending on some value.
    Correct.

    Quote Originally Posted by it-iss.com View Post
    Where is that value coming from?
    From my database

    Quote Originally Posted by it-iss.com View Post
    It might be better to provide the table definitions and the full query?
    Code:
    $sql_i = "SELECT
    		calc_id.id AS id,
    		calc_id.naam AS naam,
    		calc_inh.pos AS pos,
    		calc_id.naam_in_pos AS naam_in_pos,
    		calc_id.kwaliteit_id_std AS kwaliteit_id_std,
    		calc_id.attest_std AS attest_std,
    		calc_inh.aantal AS aantal,
    		calc_inh.lengte AS lengte,
    		calc_inh.breedte AS breedte,
    		calc_inh.dikte AS dikte,
    		calc_inh.kwaliteit_id AS kwaliteit_id,
    		calc_inh.gewicht_stk AS gewicht_stk,
    		omtr.omschrijving AS soort
    		FROM offerte_inh AS inh
    		LEFT JOIN calculatie_id AS calc_id
    		ON calc_id.id = inh.calculatie_id
    		LEFT JOIN calculatie_inh AS calc_inh
    		ON calc_inh.id_calculatie = calc_id.id
    		LEFT JOIN calculatie_omtrek AS omtr
    		ON omtr.id = calc_inh.soort_id
    		WHERE inh.offerte_id = '".$row['id']."'
    		GROUP BY calc_inh.kwaliteit_id, calc_id.kwaliteit_id_std, calc_inh.dikte
    		ORDER BY calc_inh.id ASC ";
    I hope you can help me out?

  4. #4
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    Code:
    SELECT calc_id.id               AS id,
           calc_id.naam             AS naam,
           calc_inh.pos             AS pos,
           calc_id.naam_in_pos      AS naam_in_pos,
           calc_id.kwaliteit_id_std AS kwaliteit_id_std,
           calc_id.attest_std       AS attest_std,
           calc_inh.aantal          AS aantal,
           calc_inh.lengte          AS lengte,
           calc_inh.breedte         AS breedte,
           calc_inh.dikte           AS dikte,
           calc_inh.kwaliteit_id    AS kwaliteit_id,
           calc_inh.gewicht_stk     AS gewicht_stk,
           omtr.omschrijving        AS soort
    FROM   offerte_inh AS inh
           LEFT JOIN calculatie_id AS calc_id
             ON calc_id.id = inh.calculatie_id
           LEFT JOIN calculatie_inh AS calc_inh
             ON calc_inh.id_calculatie = calc_id.id
           LEFT JOIN calculatie_omtrek AS omtr
             ON omtr.id = calc_inh.soort_id
    WHERE  inh.offerte_id = 'info'
    GROUP  BY IF(calc_inh.kwaliteit_id >
              0, calc_inh.kwaliteit_id, calc_inh.kwaliteit_id_std),
              calc_inh.dikte
    ORDER  BY calc_inh.id ASC
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  5. #5
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58
    Thanks for helping me out Ronan!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •