Solved

SQL Query if  Exists column concatenate results

Posted on 2014-01-08
5
371 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]
Comment Utility
please try again to attach the spreadsheet...
0
 

Author Comment

by:databarracks
Comment Utility
Ok second attempt
Query-Plan.xlsx
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
Hi Guy,

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

Author Closing Comment

by:databarracks
Comment Utility
Very good answer and quick response
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

763 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

10 Experts available now in Live!

Get 1:1 Help Now