If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > GROUP BY with if/else

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-17-11, 08:04
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
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?
Reply With Quote
  #2 (permalink)  
Old 03-17-11, 09:02
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
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
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-17-11, 09:21
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
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?
Reply With Quote
  #4 (permalink)  
Old 03-17-11, 09:45
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
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
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 03-22-11, 05:49
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Thanks for helping me out Ronan!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On