Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:55:32+00:00 2026-06-05T01:55:32+00:00

I have a query that uses SUM IF() to do a cross-tab result set.

  • 0

I have a query that uses SUM IF() to do a cross-tab result set. In this query I have the value sin the SUM IF() hard coded. The problem is that new values are added to the database. Is there a way to write the query without hard coding the values in the SUM IF()? Here is the query:

select storeid, sum(if(marketsegmentid = 6, 1, 0)) as 6,
sum(if(marketsegmentid = 7, 1, 0))
as 7, sum(if(marketsegmentid = 12, 1, 0)) as 12, sum(if(marketsegmentid = 17, 1, 0)) as 17,
sum(if(marketsegmentid = 22, 1, 0)) as 22, sum(if(marketsegmentid = 27, 1, 0)) as 27,
sum(if(marketsegmentid = 32, 1, 0)) as 32, sum(if(marketsegmentid = 37, 1, 0)) as 37,
sum(if(marketsegmentid = 42, 1, 0)) as 42, sum(if(marketsegmentid = 47, 1, 0)) as 47,
sum(if(marketsegmentid = 52, 1, 0)) as 52, sum(if(marketsegmentid = 97, 1, 0)) as 97,
sum(if(marketsegmentid = 102, 1, 0)) as 102, sum(if(marketsegmentid = 107, 1, 0)) as 107,
sum(if(marketsegmentid = 112, 1, 0)) as 112, sum(if(marketsegmentid = 117, 1, 0)) as 117,
sum(if(marketsegmentid = 122, 1, 0)) as 122, sum(if(marketsegmentid = 127, 1, 0)) as 127,
sum(if(marketsegmentid = 132, 1, 0)) as 132, sum(if(marketsegmentid = 137, 1, 0)) as 137,
sum(if(marketsegmentid = 142, 1, 0)) as 142
from storemarketsegments
group by storeid;

The query is used in a report and the results are exported to CSV. The 1’s are used as flags in the result set.

The table I am querying is set up like this:

CREATE TABLE storemarketsegments(id INT NOT NULL, marketsegmentid INT NOT NULL);

The marketsegments are kept in a separate table:

CREATE TABLE marketsegment(ID INT NOT NULL AUTO_INCREMENT, PRIMARY
KEY(id), name VARCHAR(45), description VARCHAR(45));

Any help would be greatly appreciated. I am not sure if there is a way to write the query without hard coding the values and I don’t mind updating the query in the report whenever new marketsegments are added but thought I would check. Thank you in advance for your assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:55:34+00:00Added an answer on June 5, 2026 at 1:55 am

    You can return each count as a separate row, and then filter as needed in the application layer:

    select storeid, marketsegmentid, count(*) as Count
    from storemarketsegments 
    group by storeid, marketsegmentid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So My problem is this, I have a query that uses Mysql User-defined variable
I have a query that uses a where clause. At times, this may be
I have this query that uses a UNION: $this->db->query(' SELECT DISTINCT users.user_pic, users.id, users.username,
I have a query that uses for xml explit to return XML result. select
I have a query that produces a result like this: The data is sorted
I have a query that uses ORDER BY ASC and as such 0's come
I have a mysql query that uses union to join multiple queries into one
We have an XML Publisher template that uses a query (built in query manager)
I have an Excel spreadsheet that uses a TFS query to pull information on
Having a strange issue - we have a page that uses a query string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.