我有一个查询,我需要显示明智的月份,如 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
我的输出是这样的:
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