Set Value With NULL When Pivot SQL Selection Return 0 Rows

Arina

How to set NULL value when you select data using pivot return 0 rows?

;WITH cte AS (
SELECT
        DATENAME(month, RPT.DateID) as Month,
        ISNULL(SUM(RPT.TransactionIn), 0) as ATransactionIn,
        ISNULL(SUM(RPT.TransactionOut), 0) as BTransactionOut,
        ISNULL(SUM(RPT.OutstandingTransaction), 0) as COutstandingTransaction
        FROM RPT_SummaryPOApproval RPT
        WHERE RPT.Deleted = 0 --AND RPT.DivisionCode = 'asd'
        GROUP BY DATENAME(month, RPT.DateID)
), pivoted
as
(     
SELECT *
FROM (
    SELECT [Month], [Transactions], [Values]
    FROM (
        SELECT *
        FROM cte
    ) as p 
    UNPIVOT (
        [Values] FOR [Transactions] IN (ATransactionIn, BTransactionOut, COutstandingTransaction )
    ) as unpvt
) as k 
PIVOT (
    MAX([Values]) FOR [Month] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
) as pvt

)
SELECT * FROM pivoted
ORDER BY [Transactions] ASC 

Those code will result something like this:

Transaction                 January  February  March .... Dec
ATransactionIn              12       0         0          0
BTransactionOut             10       0         0          0
COutstandingTransaction     2        0         0          0

When I uncomment Filter by DivisionCode (on the first code)

WHERE RPT.Deleted = 0 AND RPT.DivisionCode = 'asd'

The result become like this

Transaction                January  February  March .... Dec

How can I show the result like this?

Transaction                January  February  March .... Dec
ATransactionIn              0        0         0          0
BTransactionOut             0        0         0          0
COutstandingTransaction     0        0         0          0
Arina

I solved my problem by using union to show 0 when there's no data to display.

;WITH cte AS (
SELECT
        DATENAME(month, RPT.DateID) as Month,
        ISNULL(SUM(RPT.TransactionIn), 0) as ATransactionIn,
        ISNULL(SUM(RPT.TransactionOut), 0) as BTransactionOut,
        ISNULL(SUM(RPT.OutstandingTransaction), 0) as COutstandingTransaction
        FROM RPT_SummaryPOApproval RPT
        WHERE RPT.Deleted = 0 --AND RPT.DivisionCode = 'asd'
        GROUP BY DATENAME(month, RPT.DateID)
        UNION ALL
        SELECT '0', '0', '0', '0'
), pivoted
as
(     
SELECT *
FROM (
    SELECT [Month], [Transactions], [Values]
    FROM (
        SELECT *
        FROM cte
    ) as p 
    UNPIVOT (
        [Values] FOR [Transactions] IN (ATransactionIn, BTransactionOut, COutstandingTransaction )
    ) as unpvt
) as k 
PIVOT (
    MAX([Values]) FOR [Month] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
) as pvt

)
SELECT * FROM pivoted
ORDER BY [Transactions] ASC 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Replacing a Null value with 0 within a Pivot

From Dev

SQL query should not return rows that contain null values in pivot table

From Dev

SQL query should not return rows that contain null values in pivot table

From Dev

If value return the value. If a record not exists or when column is null, return 0 in Sql Server - different ways

From Dev

Sql Server pivot columns into rows fill NULL with zero or existing value

From Dev

How To Set NULL is 0 in SQL Dynamic Pivot Query

From Dev

Sql result return value when one of the rows has a certain value

From Dev

How can I set 0 if a query return a null value in MySql?

From Dev

Why does my SQL query return no rows, when sum is 0

From Dev

Set zero default value for null rows in SQL Server

From Dev

Return NULL if no rows are found SQL

From Dev

SQL Query return 0 rows

From Dev

MySQL - Way to set a default return value when NULL is found?

From Dev

how to return all rows even when they have no value in Oracle SQL

From Dev

SQL separate columns by rows value (pivot)

From Dev

How to pivot all rows value using sql

From Dev

SQL: how to pivot rows based on its value

From Dev

Modelbinder defaults to 0 when returning null instead of the value set in the controller

From Dev

Value return when no rows in PDO

From Dev

SQL INSERT when two SELECTs return a not-null value

From Dev

Sql pivot selection

From Dev

Return max value from a SQL selection

From Dev

SQL Server how to set a default value when the column is null

From Dev

null in return value in sql procedure

From Dev

How to count rows of pivot table where value is greater than 0

From Dev

SQL to return rows where one column is 0

From Dev

SQL Pivot - How To Show all rows with null values?

From Dev

SQL Pivot - How To Show all rows with null values?

From Dev

How to convert multiple rows into one row with multiple columns using Pivot in SQL Server when data having NULL values

Related Related

  1. 1

    SQL Replacing a Null value with 0 within a Pivot

  2. 2

    SQL query should not return rows that contain null values in pivot table

  3. 3

    SQL query should not return rows that contain null values in pivot table

  4. 4

    If value return the value. If a record not exists or when column is null, return 0 in Sql Server - different ways

  5. 5

    Sql Server pivot columns into rows fill NULL with zero or existing value

  6. 6

    How To Set NULL is 0 in SQL Dynamic Pivot Query

  7. 7

    Sql result return value when one of the rows has a certain value

  8. 8

    How can I set 0 if a query return a null value in MySql?

  9. 9

    Why does my SQL query return no rows, when sum is 0

  10. 10

    Set zero default value for null rows in SQL Server

  11. 11

    Return NULL if no rows are found SQL

  12. 12

    SQL Query return 0 rows

  13. 13

    MySQL - Way to set a default return value when NULL is found?

  14. 14

    how to return all rows even when they have no value in Oracle SQL

  15. 15

    SQL separate columns by rows value (pivot)

  16. 16

    How to pivot all rows value using sql

  17. 17

    SQL: how to pivot rows based on its value

  18. 18

    Modelbinder defaults to 0 when returning null instead of the value set in the controller

  19. 19

    Value return when no rows in PDO

  20. 20

    SQL INSERT when two SELECTs return a not-null value

  21. 21

    Sql pivot selection

  22. 22

    Return max value from a SQL selection

  23. 23

    SQL Server how to set a default value when the column is null

  24. 24

    null in return value in sql procedure

  25. 25

    How to count rows of pivot table where value is greater than 0

  26. 26

    SQL to return rows where one column is 0

  27. 27

    SQL Pivot - How To Show all rows with null values?

  28. 28

    SQL Pivot - How To Show all rows with null values?

  29. 29

    How to convert multiple rows into one row with multiple columns using Pivot in SQL Server when data having NULL values

HotTag

Archive