Solved

Sql Syntax

Posted on 2008-10-23
9
169 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
[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
9 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 22790803
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
ID: 22790869
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
ID: 22790885
Sorry Angel, didn't see your post until just now.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:grogo21
ID: 22791102
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
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 50 total points
ID: 22791192
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
ID: 22791230
I still get a divide by zero error.

Thanks!
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
ID: 22791325
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
ID: 22791335
And you can change my alias StatCount to be StatPercent or something more appropriate.
0
 

Author Comment

by:grogo21
ID: 22791382
Thanks Everyone!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax Grouping Sum question 7 36
Search query matching words 20 38
selective rebuild of SQL Tables in scheduled job 10 39
SQL syntax question 6 42
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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