Solved

Sql Syntax

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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 …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

17 Experts available now in Live!

Get 1:1 Help Now