Solved

Sql Syntax

Posted on 2008-10-06
1
291 Views
Last Modified: 2012-05-05
Hello, I need to alter this portion:

                Select count(LinkID) as Clicks, LinkID
                from OutboundTraffic
                group by LinkID

of the sql query i have below so that I can do a count of all records with the correct LinkID (as it does now) and also get a count of all records with the correct LinkID with a OutboundTraffic.ClickDate value = to todays date.  Im not sure of the syntax required to make this work.

Thanks
SELECT
        l.LinkID,
        l.NavigateURL,
        l.ImageFile,
        l.Status,
        isnull(l.StartDate, GetDate()) As StartDate,
        isnull(u.rating,0) AS Rating,
        isnull(o.Clicks,0) AS Clicks
FROM LinkIndex l
left join ( 
                Select sum(rating) as Rating, LinkID
                from UserRatings 
                group by LinkID
        ) u
on l.LinkID = u.LinkID
left join ( 
                Select count(LinkID) as Clicks, LinkID
                from OutboundTraffic
                group by LinkID
        ) o
on l.LinkId = o.LinkId
where status in (0,1,2) and userid = 1001
order by startdate

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
1 Comment
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22656301
You can modify like this:
Select count(LinkID) as Clicks, LinkID
                , SUM(Case DateDiff(dd, ClickDate, getdate()) When 0 Then 1 Else 0 End) AS TodaysClicks
                from OutboundTraffic
                group by LinkID

Using a case when to generate a 1 or 0 for records matching criteria and then use SUM to simulate a count of the 1's should do the trick.

SELECT
        l.LinkID,
        l.NavigateURL,
        l.ImageFile,
        l.Status,
        isnull(l.StartDate, GetDate()) As StartDate,
        isnull(u.rating,0) AS Rating,
        isnull(o.Clicks,0) AS Clicks,
        isnull(o.TodaysClicks,0) AS TodaysClicks
FROM LinkIndex l
left join ( 
                Select sum(rating) as Rating, LinkID
                from UserRatings 
                group by LinkID
        ) u
on l.LinkID = u.LinkID
left join ( 
                Select count(LinkID) as Clicks, LinkID
                , SUM(Case DateDiff(dd, ClickDate, getdate()) When 0 Then 1 Else 0 End) AS TodaysClicks
                from OutboundTraffic
                group by LinkID
        ) o
on l.LinkId = o.LinkId
where status in (0,1,2) and userid = 1001
order by startdate

Open in new window

0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Copy Database Wizard 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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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