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

  • Home
  • SEARCH
  • 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 6356079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:53:40+00:00 2026-05-24T22:53:40+00:00

i have a table with the following layout. Email Blast Table EmailBlastId | FrequencyId

  • 0

i have a table with the following layout.

Email Blast Table

EmailBlastId |  FrequencyId | UserId
---------------------------------
1            |   5          |   1
2            |   2          |   1
3            |   4          |   1


Frequency Table

Id | Frequency 
------------
 1 |  Daily
 2 |  Weekly
 3 |  Monthly
 4 |  Quarterly
 5 |  Bi-weekly

I need to come up with a grid display on my asp.net page as follows.

Email blasts per month.

UserId | Jan | Feb | Mar | Apr |..... Dec | Cumulative
-----------------------------------------------------
1        7      6     6     7          6     #xx

The only way I can think of doing this is as below, for each month have a case statement.

select SUM(
        CASE WHEN FrequencyId = 1 THEN 31 
        WHEN FrequencyId = 2 THEN 4
        WHEN FrequencyId = 3 THEN 1
        WHEN FrequencyId = 4 THEN 1
        WHEN FrequencyId = 5 THEN 2 END) AS Jan, 
      SUM(
        CASE WHEN FrequencyId = 1 THEN 28 (29 - leap year)
        WHEN FrequencyId = 2 THEN 4
        WHEN FrequencyId = 3 THEN 1
        WHEN FrequencyId = 4 THEN 0
        WHEN FrequencyId = 5 THEN 2 END) AS Feb, etc etc
FROM EmailBlast 
Group BY UserId

Any other better way of achieving the same?

  • 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-05-24T22:53:41+00:00Added an answer on May 24, 2026 at 10:53 pm

    Is this for any given year? I’m going to assume you want the schedule for the current year. If you want a future year you can always change the DECLARE @now to specify any future date.

    “Once in 2 weeks” (usually known as “bi-weekly”) doesn’t fit well into monthly buckets (except for February in a non-leap year). Should that possibly be changed to “Twice a month”?

    Also, why not store the coefficient in the Frequency table, adding a column called “PerMonth”? Then you only have to deal with the Daily and Quarterly cases (and is it an arbitrary choice that this will happen only in January, April, and so on?).

    Assuming that some of this is flexible, here is what I would suggest, assuming this very minor change to the table schema:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.Frequency 
    (
        Id INT PRIMARY KEY,
        Frequency VARCHAR(32),
        PerMonth TINYINT
    );
    
    CREATE TABLE dbo.EmailBlast 
    (
        Id INT,
        FrequencyId INT,
        UserId INT
    );
    

    And this sample data:

    INSERT dbo.Frequency(Id, Frequency, PerMonth)
      SELECT 1, 'Daily', NULL
      UNION ALL SELECT 2, 'Weekly', 4
      UNION ALL SELECT 3, 'Monthly', 1
      UNION ALL SELECT 4, 'Quarterly', NULL
      UNION ALL SELECT 5, 'Twice a month', 2;
    
    INSERT dbo.EmailBlast(Id, FrequencyId, UserId)
      SELECT 1, 5, 1
      UNION ALL SELECT 2, 2, 1
      UNION ALL SELECT 3, 4, 1;
    

    We can accomplish this using a very complex query (but we don’t have to hard-code those month numbers):

    DECLARE @now DATE = CURRENT_TIMESTAMP;
    DECLARE @Jan1 DATE = DATEADD(MONTH, 1-MONTH(@now), DATEADD(DAY, 1-DAY(@now), @now));
    
    WITH n(m) AS 
    (
        SELECT TOP 12 m = number
            FROM master.dbo.spt_values
            WHERE number > 0 GROUP BY number
    ),
    months(MNum, MName, StartDate, NumDays) AS
    (    SELECT m, mn = CONVERT(CHAR(3), DATENAME(MONTH, DATEADD(MONTH, m-1, @Jan1))),
            DATEADD(MONTH, m-1, @Jan1), 
            DATEDIFF(DAY, DATEADD(MONTH, m-1, @Jan1), DATEADD(MONTH, m, @Jan1))
        FROM n
    ),
    grp AS
    (
        SELECT UserId, MName, c = SUM (
            CASE x.Id WHEN 1 THEN NumDays
                WHEN 4 THEN CASE WHEN MNum % 3 = 1 THEN 1 ELSE 0 END
                ELSE x.PerMonth END )
        FROM months CROSS JOIN (SELECT e.UserId, f.* 
            FROM EmailBlast AS e 
            INNER JOIN Frequency AS f
            ON e.FrequencyId = f.Id) AS x
        GROUP BY UserId, MName
    ),
    cumulative(UserId, total) AS
    (
        SELECT UserId, SUM(c)
          FROM grp GROUP BY UserID
    ),
    pivoted AS
    (
        SELECT * FROM (SELECT UserId, c, MName FROM grp) AS grp 
        PIVOT(MAX(c) FOR MName IN (
            [Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
        ) AS pvt
    )
    SELECT p.*, c.total 
        FROM pivoted AS p
        LEFT OUTER JOIN cumulative AS c
        ON p.UserId = c.UserId;
    

    Results:

    UserId  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec total
    1       7   6   6   7   6   6   7   6   6   7   6   6   76
    

    Clean up:

    DROP TABLE dbo.EmailBlast, dbo.Frequency;
    GO
    

    In fact the schema change I suggested doesn’t really buy you much, it just saves you two additional CASE branches inside the grp CTE. Peanuts, overall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table layout with the following code <table cellspacing=0 class=photogalleryTable> <tbody> <tr>
I have an SQL server with the following layout Table ( id int title
Say I have a table with the following layout: Id Int PRIMARY KEY IDENTITY
need help in some wired problem. I have a sqlite table items as following
I have the following layout: <div style=float:right ...>Contents</div> <table>contents</table> I want that the div
I have the following table layout. Each line value will always be unique. There
Say you have a table layout like the following: couses : id (INT), courseName
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
Let's say I have a table containing following data: | id | t0 |
I have already googled for this I have a Table with following structure in

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.