Solved

Sql Syntax

Posted on 2008-10-06
1
288 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
1 Comment
 
LVL 59

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now