Solved

Sql Syntax

Posted on 2008-10-23
9
166 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.


Thanks!
Declare @StatCode int

Set @StatCode = 8

select (select COUNT(Distinct(VisitorID))+.0 

		from OutboundTraffic 

		where VisitorID in(select VisitorID 

						   from InboundTraffic 

						   where StatCode = @StatCode))/(select COUNT(*) from Stats where StatCode = @StatCode)

Open in new window

0
Comment
Question by:grogo21
9 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
Comment Utility
something like this:
SELECT s.StatCode
     , ( select COUNT(Distinct(ot.VisitorID))+.0 
            from OutboundTraffic ot 
            where ot.VisitorID in ( select it.VisitorID 
                                       from InboundTraffic it
                                       where it.StatCode = s.StatCode
                                  ) 
       ) /  (select COUNT(*) from Stats where stats.StatCode = s.StatCode) as perc
  FROM StatSource s

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
An approach like this may work.  May have to tweak the join type to your liking, but hopefully you get the point of the derived tables.
SELECT t1.StatCode,

	CASE ISNULL(t2.sCount, 0) WHEN 0 THEN 0 

	ELSE ISNULL(t1.oCount, 0) / t2.sCount

	END AS StatCount

FROM 

(

	SELECT i.StatCode, COUNT(Distinct(o.VisitorID))+.0 As oCount

	FROM OutboundTraffic o INNER JOIN InboundTraffic i

	ON o.VisitorID = i.VisitorID

) AS t1

INNER JOIN

(

	SELECT StatCode, COUNT(*) AS sCount 

	FROM Stats

) AS t2

ON t1.StatCode = t2.StatCode

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Sorry Angel, didn't see your post until just now.
0
 

Author Comment

by:grogo21
Comment Utility
mwvisa1: I got error:
Column 'InboundTraffic.StatCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

angelllll
what if (select COUNT(*) from Stats where stats.StatCode = s.StatCode) as perc = 0?

Thanks!
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 50 total points
Comment Utility
You'll have to account for it:

SELECT s.StatCode
     , case when (select COUNT(*) from Stats where stats.StatCode = s.StatCode)  = 0 then 0 else ( select COUNT(Distinct(ot.VisitorID))+.0
            from OutboundTraffic ot
            where ot.VisitorID in ( select it.VisitorID
                                       from InboundTraffic it
                                       where it.StatCode = s.StatCode
                                  )
       ) end /  (select COUNT(*) from Stats where stats.StatCode = s.StatCode) as perc
  FROM StatSource s
0
 

Author Comment

by:grogo21
Comment Utility
I still get a divide by zero error.

Thanks!
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
Comment Utility
Sorry I forgot the group by statement.
SELECT t1.StatCode,

	CASE ISNULL(t2.sCount, 0) WHEN 0 THEN 0 

	ELSE ISNULL(t1.oCount, 0) / t2.sCount

	END AS StatCount

FROM 

(

	SELECT i.StatCode, COUNT(Distinct(o.VisitorID))+.0 As oCount

	FROM OutboundTraffic o INNER JOIN InboundTraffic i

	ON o.VisitorID = i.VisitorID

         GROUP BY i.StatCode

) AS t1

INNER JOIN

(

	SELECT StatCode, COUNT(*) AS sCount 

	FROM Stats

         GROUP BY StatCode

) AS t2

ON t1.StatCode = t2.StatCode

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
And you can change my alias StatCount to be StatPercent or something more appropriate.
0
 

Author Comment

by:grogo21
Comment Utility
Thanks Everyone!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

7 Experts available now in Live!

Get 1:1 Help Now