Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7824647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:40:14+00:00 2026-06-02T08:40:14+00:00

I already have a view which is a break down by day (ByDay in

  • 0

I already have a view which is a break down by day (ByDay in query below).

Now I want to filter it such that a row is included in the result if it’s campaign is in the top 10 revenue generating campaigns.

I got what I wanted but I have a feeling I might skipped over the obvious/right way to do it in the process.

WITH T AS (
    SELECT
     ByDay.[Day]
    ,ByDay.[Campaign Name]
    ,ByDay.[Revenue USD]
    FROM ByDay
    WHERE ByDay.[Period]='Mar 2012'
),
TT AS (
    SELECT
     ByDay.[Campaign Name]
    ,SUM(ByDay.[Revenue USD]) [Sum Revenue USD]
    FROM ByDay
    WHERE ByDay.[Period]='Mar 2012'
    GROUP BY ByDay.[Campaign Name]
)
SELECT * FROM (
SELECT
     T.[Day]
    ,T.[Campaign Name]
    ,T.[Revenue USD]
    ,TT.[Sum Revenue USD]
    ,DENSE_RANK() OVER(ORDER BY TT.[Sum Revenue USD] DESC) AS R
    FROM T
    INNER JOIN TT ON T.[Campaign Name]=TT.[Campaign Name]
) TTT
WHERE R < 11
ORDER BY R

Is there a simpler way to get something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:40:15+00:00Added an answer on June 2, 2026 at 8:40 am

    You could use SUM([Revenue USD])OVER(Partition By [Campaign Name]) to get the sum for each Campaign Name. Then you only need one CTE:

    http://msdn.microsoft.com/en-us/library/ms189461.aspx

    WITH T AS (
        SELECT
         ByDay.[Day]
        ,ByDay.[Campaign Name]
        ,ByDay.[Revenue USD]
        ,SUM(ByDay.[Revenue USD]) OVER(Partition By ByDay.[Campaign Name]) [Sum Revenue USD] 
        FROM ByDay
        WHERE ByDay.[Period]='Mar 2012'
    )
    SELECT * FROM (
    SELECT
         T.[Day]
        ,T.[Campaign Name]
        ,T.[Revenue USD]
        ,T.[Sum Revenue USD]
        ,DENSE_RANK() OVER(ORDER BY T.[Sum Revenue USD] DESC) AS R
        FROM T
    ) TTT
    WHERE R < 11
    ORDER BY R
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an Day View calendar app. I have already created the empty
I already have code which lazy loads scripts on request. My issue now is
I already have an application built in asp.net 3.5 and now I want to
I already have a custom UIAlertView and in which i want to add UIImageView
I want to create a property out of a view which already has my
I have a view which already has SchemaBinding applied AND it has a UNIQUE
I would like to have the keyboard already displaying when I show my view.
i already have a project with TreeNode class which creates a hierachy of nodes
I already have an enterprise Java EE application. I want expose some of the
I already have a view controller with a default view and few things added

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.