Solved

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

Posted on 2014-02-11
2
379 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
2 Comments
 
LVL 39

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now