Solved

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

Posted on 2014-02-11
2
403 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 500 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

707 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