Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql Syntax

Posted on 2008-10-06
1
Medium Priority
?
297 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 60

Accepted Solution

by:
Kevin Cross earned 2000 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

Technology Partners: 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

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.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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