Solved

Return count from Table3, data from Table1 and Table2

Posted on 2014-01-01
2
510 Views
Last Modified: 2014-01-01
Hi All,
I'm filling a table (Table4) with data from 3 other tables (Table1, Table2, & Table3)
From Table3 all I need is the count of number of records which match the criteria, from Tables1 & 2 I need actual data from the table.

I have an SQL statement:
Insert INTO Table4 (PID,SID,PrID,CDate,CStart,CEnd,CCode,Fax,Email,Phone,PACnt)
Select Table1.PID,Table2.SID,Table1.PrID,CDate,CStart,CEnd,CCode,Fax,Table2.Email,Phone, count (*) Table3 From
Table1  Full Outer Join Table2 on Table1.SID = Table2.SID Full Outer Join Table3 on Table3.SID = Table1.SID
where Table1.CDate >= '12/1/2013' and Table1.CDate <= '12/31/2013' and Table1.PID = 979 and Table1.UID = 225

If I use a field from Table3 - e.g. Field1.Table3 instead of the Count (*) it works except I don't get the count - just the value in that field.  For any given match of SID Table3 might have 0 or more records.  I just need the count of the number of records.  As I said, everything else works fine - Table4 gets filled with the proper data (except the count of records in Table3).

Any help would be appreciated
Thanks!
0
Comment
Question by:TopCatOnyx
2 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39750214
I think you'll find using a sub-query solves that.

insert into Table4 (PID, SID, PrID, CDate, CStart, CEnd, CCode, Fax, Email, Phone, PACnt)
  select Table1.PID, Table2.SID, Table1.PrID, CDate, CStart, CEnd, CCode, Fax, Table2.Email, Phone
    , (select count (*) from Table3 where Table3.SID = Table1.SID) Table3
  from Table1
  full outer join Table2 on Table1.SID = Table2.SID
  where Table1.CDate >= '12/1/2013' and Table1.CDate <= '12/31/2013'
  and Table1.PID = 979 and Table1.UID = 225

Open in new window

0
 

Author Closing Comment

by:TopCatOnyx
ID: 39750244
Hi Dale,
Great solution - works like a charm!  I have learned something new today - it is a great day.  
Thank You!
TCO
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
Experts-Exchange users below are the steps you can follow to upgrade your Lync server to latest CU's or cumulative updates. Note: Perform it during non-production hours.   Step 1: Backup your lync and SQL server database. Follow below article: h…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

13 Experts available now in Live!

Get 1:1 Help Now