如何显示null当值不存在时

时间:2013-06-24 08:40:12

标签: sql-server tsql

我有一个查询,我需要显示明智的月份,如3,6,9,12个月,它实际上是月计划,如金,银,经典等 如果有会员金额需要显示值或其他需要显示空值.... 这是我的疑问:

IF OBJECT_ID(N'Tempdb..#months') IS NOT NULL
    DROP TABLE  #months;
        IF OBJECT_ID(N'Tempdb..#membership') IS NOT NULL
    DROP TABLE  #membership;
DECLARE @i_FromCustID INT = 1
DECLARE  @i_BranchId INT,@i_CasteId INT,@i_GenderID INT
    CREATE TABLE #membership
    (
     MembershipName VARCHAR(50),
     Duration INT,
     MemberShipAmount INT
    )

    CREATE TABLE #months(ID IDENTITY()Duration INT)

     SELECT
        @i_BranchId = BranchID,@i_GenderID = GenderID
    FROM 
        Cust_BasicInfo  
    WHERE Cust_ID = @i_FromCustID

    SELECT 
        @i_CasteId = CasteID
    FROM 
        Cust_Details
    WHERE Cust_ID = @i_FromCustID   
        SELECT  @i_BranchId,@i_GenderID,@i_CasteId

        INSERT INTO #months
        SELECT 3
        UNION 
        SELECT 6
        UNION
        SELECT 9
        UNION 
        SELECT 12

Select * From   #months

        --INSERT INTO #membership 
            SELECT CASE 
        WHEN CHARINDEX('_', MembershipName) > 0
            THEN SUBSTRING(MembershipName, 1, CHARINDEX('_', MembershipName) - 1)
        ELSE MembershipName
        END AS NAME,
    MemberShipDuration,
    MembershipAmount,
    ms.Duration
FROM #months ms
LEFT JOIN Emp_MembershipTypes em ON ms.Duration = em.MemberShipDuration
    AND MemberShipTypeID = 236
    AND BranchID = @i_BranchId
    AND CasteID = @i_CasteId
    AND GenderID = @i_GenderID
    AND MembershipName IS NOT NULL
ORDER BY MembershipName

我的outPut就像这样:

NAME    MemberShipDuration  MembershipAmount    Duration
Classic 12  1500    12
Classic 12  1500    12
Gold    3   1000    3
Gold    6   1000    6
Gold    9   1000    9
Gold    12  1000    12
Silver  3   1000    3
Silver  6   1   6

但是我怎么能得到像这样的输出

NAME    MemberShipDuration  MembershipAmount    Duration
Classic 3   1500    3
Classic 6   NULL    6
Classic 9   NULL    9
Classic 12  NULL    12
Gold    3   1000    3
Gold    6   1000    6
Gold    9   1000    9
Gold    12  1000    12
Silver  3   1000    3
Silver  6   1   6
Silver  9   NULL    9
Silver  12  NULL    12

1 个答案:

答案 0 :(得分:1)

您需要延长月份以包含类型,并将LEFT JOIN添加到两列

像这样。

CREATE TABLE #months(Name varchar(20), Duration INT);
..
INSERT INTO #months
SELECT
    X.Name, Y.Duration
FROM
    (VALUES ('Classic'),('Gold'),('Silver')) X(Name)
    CROSS JOIN
    (VALUES (3),(6),(9),(12)) Y(Duration)
...
SELECT
    ms.Name,
    ms.Duration,
    MembershipAmount,
    MemberShipDuration
FROM
    #months ms
    LEFT JOIN Emp_MembershipTypes em ON ms.Duration = em.MemberShipDuration AND
            CASE
                WHEN CHARINDEX('_', MembershipName) > 0 THEN SUBSTRING(MembershipName, 1, CHARINDEX('_', MembershipName) - 1)
                ELSE MembershipName
            END = ms.Name
                    AND MemberShipTypeID = 236
                    AND BranchID = @i_BranchId
                    AND CasteID = @i_CasteId
                    AND GenderID = @i_GenderID
                    AND MembershipName IS NOT NULL
ORDER BY
    ms.Name, ms.Duration

你可能也需要SUM(MembershipAmount)(使用GROUP BY),但很难说......