Solved

SQL Query if  Exists column concatenate results

Posted on 2014-01-08
5
386 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
[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
  • 3
  • 2
5 Comments
 
LVL 143

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 143

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

696 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