?
Solved

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

Posted on 2014-02-11
2
Medium Priority
?
406 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 40

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

765 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