Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to do case in select statement show all records in one row

Posted on 2014-02-11
2
Medium Priority
?
415 Views
Last Modified: 2014-02-11
Hi all.

I have the following select statement (MySQL table) where it shows a Y if the builder/community has a record matching the case statement.

But it is showing multiple rows for the same builder/community depending on how many Y there are for each work type schedule id.

I want to be able to show everything (all the Y or blanks) in one row for each builder community instead of multiple rows.

Thank you in advance!

¿SELECT DISTINCT Builder, Community,Charts, 
   CASE WHEN WorkByCommunity.workscheduletypeid=3 Or WorkByCommunity.workscheduletypeid=4 Or WorkByCommunity.workscheduletypeid=5
    THEN 'Y' ELSE '' END as Flooring, 
   CASE WHEN WorkByCommunity.workscheduletypeid=10 THEN 'Y' ELSE '' END as ArcStone,
   CASE WHEN WorkByCommunity.workscheduletypeid=11 THEN 'Y' ELSE '' END as BathAccessories,
   CASE WHEN WorkByCommunity.workscheduletypeid=15 THEN 'Y' ELSE '' END as WindowTreatments,
    CASE WHEN WorkByCommunity.workscheduletypeid=12 Or WorkByCommunity.workscheduletypeid=13 Or WorkByCommunity.workscheduletypeid=14 THEN 'Y' ELSE '' END as Specialties
FROM BuilderCommunity LEFT OUTER JOIN WorkByCommunity  ON BuilderCommunity.BuilderCommunityID = WorkByCommunity.BuilderCommunityID  WHERE  BuilderCommunity.Status = 'Active' 
ORDER BY Builder, Community

Open in new window

0
Comment
Question by:Sim1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39850386
You can use the group concat function to combine the rows.
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 2000 total points
ID: 39850404
Group-by with aggregates:
SELECT DISTINCT
	Builder
,	Community
,	Charts
,	MAX(CASE WHEN WorkByCommunity.workscheduletypeid=3 Or WorkByCommunity.workscheduletypeid=4 Or WorkByCommunity.workscheduletypeid = 5 THEN 'Y' ELSE '' END) as Flooring
,	MAX(CASE WHEN WorkByCommunity.workscheduletypeid=10 THEN 'Y' ELSE '' END) as ArcStone
,	MAX(CASE WHEN WorkByCommunity.workscheduletypeid=11 THEN 'Y' ELSE '' END) as BathAccessories
,	MAX(CASE WHEN WorkByCommunity.workscheduletypeid=15 THEN 'Y' ELSE '' END as WindowTreatments
,	MAX(CASE WHEN WorkByCommunity.workscheduletypeid=12 Or WorkByCommunity.workscheduletypeid=13 Or WorkByCommunity.workscheduletypeid=14 THEN 'Y' ELSE '' END) as Specialties
FROM	BuilderCommunity
LEFT
JOIN	WorkByCommunity  ON BuilderCommunity.BuilderCommunityID = WorkByCommunity.BuilderCommunityID
WHERE	BuilderCommunity.Status = 'Active' 
GROUP
BY	Builder
,	Community
,	Charts

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question