Solved

Sql Syntax

Posted on 2008-10-06
1
289 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

Suggested Solutions

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. …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 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