Solved

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

Posted on 2014-02-11
2
397 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 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 53
Copy data to New Year 9 29
Searching a barcode number within a string. 7 24
Need quicker response from an Execption table 11 12
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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

713 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