Solved

Sql Syntax Joins

Posted on 2008-10-23
4
270 Views
Last Modified: 2010-03-20
Hello, I was wondering how I can modify the sql query I have below so that instead of setting @StatCode manully each time It would select all of the StatCodes in a table and complete the operation below for each one.

The table and field which contains the StatCode values is StatSource.StatCode.  I tried to do this myself but was unable to work through it. Hopefully after I see it done one more time i will be able to do it myself!


Thanks!
Declare @StatCode int
Set @StatCode = 8
select COUNT(VisitorID) As Clicks, COUNT(Distinct(VisitorID)) As UniqueVisitors
from outboundtraffic 
where VisitorID in(Select VisitorID from InboundTraffic where StatCode = @StatCode)

Open in new window

0
Comment
Question by:grogo21
[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
  • 2
4 Comments
 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 150 total points
ID: 22792285
You should be able to get what you want with simple table joins.  Since VisitorID exists in outboundtraffic and inboundtraffic you can join those two tables, and since StatCode exists in both inboundtraffic and StatSource you can join those tables.  Try the query below.

select
  COUNT(ot.VisitorID) As Clicks,
  COUNT(Distinct(ot.VisitorID)) As UniqueVisitors
from
  outboundtraffic ot
 inner join
  InboundTraffic it
 on
  ot.VisitorID = it.VisitorID
 inner join
  StatSource ss
 on
  it.StatCode = ss.StatCode
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22792336
select b.StatCode, COUNT(a.VisitorID) As Clicks, COUNT(Distinct(a.VisitorID)) As UniqueVisitors
from outboundtraffic a, InboundTraffic b, StatSource c
WHERE a.VisitorID = b.VisitorID
AND b.StatCode = c.StatCode
GROUP BY b.StatCode
0
 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 150 total points
ID: 22792344
assume table schema is

outboundtraffic
      VisitorID
      
InboundTraffic
      VisitorID
      StatCode
      
StatSource
      StatCode

select b.StatCode, COUNT(a.VisitorID) As Clicks, COUNT(Distinct(a.VisitorID)) As UniqueVisitors
from outboundtraffic a, InboundTraffic b, StatSource c
WHERE a.VisitorID = b.VisitorID
AND b.StatCode = c.StatCode
GROUP BY b.StatCode

Open in new window

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 200 total points
ID: 22792826
Need to be a bit careful with joins and so on so that the record count does not exceed the outbound record count as a result of joining to additional tables.... So test the joins with a straight select as you were doing before...


select  SS.statcode, COUNT(OB.VisitorID) As Clicks, COUNT(Distinct(OB.VisitorID)) As UniqueVisitors
from outboundtraffic OB
inner join (Select VisitorID, statcode from InboundTraffic group by visitorid, statcode) as IB on IB.visitorID = OB.visitorID
inner join StatSource SS on SS.statcode = IB.statcode
group by SS.statcode
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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