Solved

SQL Query if  Exists column concatenate results

Posted on 2014-01-08
5
377 Views
Last Modified: 2014-01-08
Hi Guys,

I have attached a spreadsheet illustrating what I am trying to achieve with the desired query result. I know how to check if a record has rows in multiple tables and return the different row counts from the tables a rows using.

SELECT        COUNT(*) AS Count1
FROM            Table1
UNION ALL
SELECT        COUNT(*) AS Count2
FROM            Table2
UNION ALL
SELECT        COUNT(*) AS Count3
FROM            Table3
UNION ALL
SELECT        COUNT(*) AS Count4
FROM            Table4
UNION ALL
SELECT        COUNT(*) AS Count5
FROM            Table5

Open in new window


Could someone please help me right a query to produce my desired result as per my spreadsheet.

Many thanks in advance
0
Comment
Question by:databarracks
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39764830
please try again to attach the spreadsheet...
0
 

Author Comment

by:databarracks
ID: 39764838
Ok second attempt
Query-Plan.xlsx
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39764992
this would look like something like this (starter)
select c.customer_name
  , ( select count(*) from tblService1 s where s.customer_ID = c.customer_ID and s.active = 'Yes' ) service_1
  , ( select count(*) from tblService2 s where s.customer_ID = c.customer_ID and s.active = 'Yes' ) service_2
  , ( select count(*) from tblService3 s where s.customer_ID = c.customer_ID and s.active = 'Yes' ) service_3
  , ( select count(*) from tblService4 s where s.customer_ID = c.customer_ID and s.active = 'Yes' ) service_4
  , ( select count(*) from tblService5 s where s.customer_ID = c.customer_ID and s.active = 'Yes' ) service_5
  from tblCustomers c

Open in new window

0
 

Author Comment

by:databarracks
ID: 39765129
Hi Guy,

That did the trick,many thanks again for your help
0
 

Author Closing Comment

by:databarracks
ID: 39765130
Very good answer and quick response
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

816 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

7 Experts available now in Live!

Get 1:1 Help Now