Genarate Months Column in SQL

Devos2022 21 Reputation points
2022-02-21T22:36:25.463+00:00

I am using sql compact database SDF and I have table Sales below is structure

[SALES_ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY, 
[Sales_Date] datetime, 
[Sales_Time] nvarchar(50), 
[User_ID] bigint NOT NULL, 
[customer_id] bigint NOT NULL, 
[Payment] float

please help me to write an sql query that displays the total sales by month for each customer ID

Customer_ID | January | February| March | April | May |June | July |....... |December |

1 25000 24000 28000 30000 10000 40000

2 35000 42000 10000 55000 3000 27500

.....

thanks in advance

Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-02-21T22:57:52.877+00:00
    SELECT Customer_id, 
         SUM(CASE WHEN Month(Sales_date) = 1 THEN Payment END AS January,
         SUM(CASE WHEN Month(Sales_date) = 2 THEN Payment END AS February,
        ...
    FROM tbl
    GROUP BY Customer_id
    

    This query runs on "big" SQL Server. I have not worked with SQL Compact Edition, so I don't know if the Month function is there.


  2. Ronen Ariely 15,206 Reputation points
    2022-02-21T23:14:34.157+00:00

    Here is a list of option to transform rows to columns (using Erland's term, this is for "big" SQL Server)

    (1) Using PIVOT

    (2) Using multiple self-joins (one for each month) -don't try this as this is awful solution

    (3) Using CROSS APPLY - my prefer option many times

    SELECT Stage.id,v.idd, v.colc
    FROM (VALUES ('1', 1, 2, 3),('2', 11, 22, 33)) AS Stage(id,col1,col2,col3) -- this just demonstrate the table which you want to use
    CROSS APPLY (VALUES ('col1', col1),('col2', col2),('col3', col3)) AS v(idd,colc)
    GO

    (4) Using an aggregate function - this is what Erland provided

    0 comments No comments

  3. LiHong-MSFT 10,056 Reputation points
    2022-02-22T06:25:37.16+00:00

    Hi @Devos2022
    You can use Pivot like this:

    SELECT * FROM   
         (SELECT customer_id, DATENAME(MONTH, Sales_Date) AS MName, Payment  
          FROM Sales   
    	  WHERE Sales_Date BETWEEN 'Start_date' AND 'End_date') S  
    PIVOT(SUM(Payment) FOR MName IN ([January],[February],[],....)) P  
    

    If SQL Compact doesn't support PIVOT, you can also try this:

    SELECT customer_id,  
    SUM(CASE DATENAME(MONTH, Sales_Date) WHEN 'January' THEN Payment ELSE 0 END) AS January,  
    SUM(CASE DATENAME(MONTH, Sales_Date) WHEN 'February' THEN Payment ELSE 0 END) AS February,  
    ...  
    ...  
    FROM Sales   
    WHERE Sales_Date BETWEEN 'Start_date' AND 'End_date'  
    GROUP BY customer_id  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.