5

I have database that has 12 columns of numeric data - one column for each month.

Column names are

Jan, Feb, Mar. Apr, May, Jun, Jul, Aug, Sep, Oct, Nov and Dec.

I know how to write code to sum first 3 months (Jan, Feb, and Mar) and return value in Total.

SELECT Jan+Feb+Mar AS Total FROM MyData

Question: How do I write generic query to give me sum of first N months?

Do I need to write code to generate the SQL statement or is there another way?

3
  • 4
    I think you need to write code to generate the SQL, because SQL logic can't decide which columns are included in the query. A better design might be to spread your months across rows. Then, you could restrict to the time frame you want using a simple WHERE clause, without having to resort to dynamic SQL. Commented Mar 1, 2018 at 6:25
  • Which DBMS product are you using? Postgres? Oracle? "SQL" is just a query language, not the name of a specific database product. Commented Mar 1, 2018 at 6:59
  • 1
    Any chance you can change your design? If you had this in two columns (month, value) and 12 rows, it would be trivial. Having these types of issues is a symptom of a design that's not a SQL Pattern. Commented Mar 1, 2018 at 8:19

3 Answers 3

3

I think you would need dynamic SQL to auto generate a query with the first N months. But you could also define a view which contains all the sums, e.g.

CREATE VIEW sums AS (
    SELECT Jan AS total1,
    SELECT Jan + Feb AS total2,
    SELECT Jan + Feb + Mar AS total3,
    ...
    SELECT Jan + Feb + Mar + ... + Nov + Dec AS total12
    FROM yourTable
)

Then, you could query the view and access any sum you want.

Sign up to request clarification or add additional context in comments.

Comments

0

You can generate a string which you can execute using sp_executesql command

DECLARE @query NVARCHAR(MAX);
SET @query = 'SELECT #selectColumns# FROM tableName'

--- write your logic to get how many columns you need based on value of `N`
--- make their summation  as stringToReplace (i.e. = 'Jan + Feb + Mar ...')
--- and replace them with #selectColumns# in @query

EXECUTE(@query)

PS: if you are using database other than MSSQL Server, you should be able to find some way to execute string as SQL command.

For example, for MySQL, there is prepare

PREPARE stmt1 FROM @query; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1; 

For Oracle, you'll be having EXEC SQL EXECUTE IMMEDIATE :query;

4 Comments

How do you know which database the OP is using?
As it's not mentioned by OP, I assume that's not what he is asking. I think the main question he is having is how to ask sql server which columns to select
Again, how do you know the OP is using SQL Server?
oh.. ok... you're right.. your approach to create a view makes more sense... +1 for that!
0

If, you are working with SQL Server, then you could use APPLY operator to generate the month sequence.

SELECT
       SUM(a.Total) Totals 
FROM table t
CROSS APPLY (
        VALUES (1, 'Jan', Jan), (2, 'Feb', Feb), (3, 'Mar', Mar),
               ....
               (12, 'Dec', Dec)
)a(Seq, Months, Total)
WHERE a.Seq <= 3 -- pass Nos of Months to get the sum of Totals

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.