• Resolved garykz

    (@garykz)


    someone wrote this query for me and we need to filter on the “Group_concat(mem_type. name)AS Member_Type” field so we only get data where this field contains (New Jersey). There are other values in this field so I have to keep the CONCAT but then I want to only show records that have “New Jersey”.

    ________________
    SELECT DISTINCT Group_concat(membership.id),

    contact.first_name,

    contact.middle_name,

    contact.last_name,

    contact.job_title,

    contact.organization_name,

    address.street_address,

    address.supplemental_address_1,

    (SELECT name

    FROM civicrm_state_province

    WHERE id = address.state_province_id) AS state,

    address.city,

    address.postal_code,

    phone.phone,

    Group_concat(mem_type. name) AS Member_Type,

    Group_concat(membership.join_date) AS since_date,

    Group_concat(membership.start_date),

    Group_concat(membership.end_date),

    (SELECT name

    FROM civicrm_membership_status

    WHERE id = Group_concat(membership.status_id)) AS

    membership_status,

    contribution.total_amount,

    email.email

    FROM civicrm_membership membership

    LEFT JOIN civicrm_membership_type mem_type

    ON mem_type.id = membership.membership_type_id

    LEFT JOIN civicrm_contact contact

    ON contact.id = membership.contact_id

    LEFT JOIN civicrm_address address

    ON address.contact_id = contact.id

    AND address.is_primary = 1

    LEFT JOIN civicrm_phone phone

    ON phone.contact_id = contact.id

    AND phone.is_primary = 1

    LEFT JOIN civicrm_membership_payment payment

    ON payment.membership_id = membership.id

    LEFT JOIN civicrm_contribution contribution

    ON contribution.id = payment.contribution_id

    LEFT JOIN civicrm_email email

    ON email.contact_id = contact.id

    AND email.is_primary = 1

    WHERE membership.status_id IN ( 1, 2, 3 )

    AND membership.contact_id IN (SELECT contact_id

    FROM civicrm_membership

    WHERE membership.status_id IN ( 1, 2, 3 )

    GROUP BY contact_id)

    GROUP BY membership.contact_id

    ________________

    https://www.ads-software.com/plugins/elisqlreports/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Eli

    (@scheeeli)

    If you only want to query the records that have New Jersey as the mem_type.name then you could add the following line between the two GROUP BY lines:
    WHERE mem_type.name = ‘New Jersey’

    But if you still want to query all the records but only show the ones have New Jersey in the results (still also showing the other values for those row in that CONCAT column) then you could add this to the very end of your query:
    HAVING Member_Type LIKE ‘%New Jersey%’

    Please let me know if either of these options gets you what you want or if you have any trouble implementing them.

    Aloha, Eli

    Thread Starter garykz

    (@garykz)

    The HAVING statement did it. THANKS. That is exactly what I needed for this query. I had tried the WHERE clause before but then that would eliminate data that I needed in the Group_Concat fields.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to limit in this SQL Query’ is closed to new replies.