Solved

Sql Syntax Joins

Posted on 2008-10-23
4
266 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grouping logic 6 47
SQL Query resolving a string conversion issue 26 38
Columnstore Indexes - real-time operational analytics 1 8
Sql Query 4 16
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

25 Experts available now in Live!

Get 1:1 Help Now