我正在撰写一份报告,为我们的新客户带回Landstatus。
我们有3个选项(Gold,Silver和Bronze,如果用户没有添加状态,则为null)。
数据存储在2个不同的表dbo.tbl_Profile_ProjectLand
和dbo.Tasks
有时我发现dbo.tbl_Profile_ProjectLand
的地位不同于dbo.Tasks
对于我的报告,我想使用dbo.tbl_Profile_ProjectLand
作为我的主要表格,如果这对于landstatus没有结果,那么请使用dbo.Tasks
问题是我似乎无法正确查询。 这是我的代码。
SELECT
Count
((CASE WHEN pp.landstatus > 0 THEN pp.landstatus
ELSE t.landstatus END)) As Taken,
ls.Description
FROM dbo.Tasks AS t
LEFT JOIN
tbl_User AS u
ON t.TakenBy = u.UserId
LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp
ON t.Fk_ProfileID = pp.Fk_ProfileID
LEFT JOIN
tbl_DDLandStatus AS ls
ON t.LandStatus = ls.Id
LEFT JOIN
dbo.tbl_EnquiryType AS e
ON t.EnquiryType = e.EnquirytypeId
where
(Month(t.DueDate) = Month(getdate())
AND YEAR(t.DueDate) = YEAR(getdate ()))and t.EnquiryType = 1
GROUP BY
ls.Description
ORDER BY ( CASE ls.Description
WHEN 'Gold - owned' THEN 1
WHEN 'Silver - identified / offered' THEN 2
WHEN 'Bronze - no land' THEN 3
ELSE 0
END)
这会带来以下结果
Taken Description
40 NULL
34 Gold - owned
28 Silver - identified / offered
8 Bronze - no land
现在我检查了40个null结果,这是不正确的,因为在中有Null值
dbo.tbl_Profile_ProjectLand
表和dbo.Tasks
中代表青铜状态的6。
我希望我已经解释过这个好像因为我几乎把自己弄糊涂了。 任何帮助将不胜感激。感谢
我找到了一种方法来做到这一点,但我似乎无法将它们分组。这是我的新代码
SELECT
COUNT ((CASE WHEN pp.landstatus > 0 THEN pp.landstatus
ELSE t.landstatus END)) As Taken,
((CASE WHEN ls.Description > '' THEN ls.Description
ELSE lst.description END)) As 'Description'
FROM dbo.Tasks AS t
LEFT JOIN tbl_DDLandStatus AS lst
ON t.landstatus = lst.Id
LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp
ON t.Fk_ProfileID = pp.Fk_ProfileID
LEFT JOIN tbl_DDLandStatus AS ls
ON pp.LandStatus = ls.Id
WHERE (Month(t.DueDate) = Month(getdate())
AND YEAR(t.DueDate) = YEAR(getdate ()))and t.EnquiryType = 1
GROUP BY
ls.Description,lst.description
ORDER BY ( CASE ls.Description
WHEN 'Gold - owned' THEN 1
WHEN 'Silver - identified / offered' THEN 2
WHEN 'Bronze - no land' THEN 3
ELSE 0
END)
这会带回这个结果,这个结果是现场但没有组合。
Taken Description
22 NULL
7 Bronze - no land
22 Gold - owned
14 Silver - identified / offered
5 Gold - owned
13 Gold - owned
7 Gold - owned
16 Silver - identified / offered
1 Silver - identified / offered
1 Silver - identified / offered
11 Silver - identified / offered
知道如何根据描述对它们进行分组吗?
答案 0 :(得分:1)
你可以像这样修改案例并检查
Select count(case when pp.landstatus is not null then pp.landstatus
ELSE t.landstatus END) as taken , your query
答案 1 :(得分:1)
更改连接的性质以适应状态值的差异是正确的。
我想你也会发现你不需要COUNT()
中的case表达式您需要做的就是调整组中的最新查询,以使用您用于描述列的相同案例表达式。
SELECT
COUNT(*) as Taken
, ((CASE
WHEN ls.Description > ''
THEN ls.Description
ELSE lst.description
END)) AS 'Description'
FROM dbo.Tasks AS t
LEFT JOIN tbl_DDLandStatus AS lst ON t.landstatus = lst.Id
LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp ON t.Fk_ProfileID = pp.Fk_ProfileID
LEFT JOIN tbl_DDLandStatus AS ls ON pp.LandStatus = ls.Id
WHERE (MONTH(t.DueDate) = MONTH(GETDATE())
AND YEAR(t.DueDate) = YEAR(GETDATE()))
AND t.EnquiryType = 1
GROUP BY
(CASE
WHEN ls.Description > ''
THEN ls.Description
ELSE lst.description
END)
ORDER BY
(CASE ls.Description
WHEN 'Gold - owned'
THEN 1
WHEN 'Silver - identified / offered'
THEN 2
WHEN 'Bronze - no land'
THEN 3
ELSE 0
END)
下图:上一次尝试(失败): 不太确定这些:
试试这个:
SELECT
COUNT
(CASE
WHEN pp.landstatus > 0
THEN pp.landstatus
END) AS Taken
, COALESCE(ls.Description, 'NULL') AS Description
FROM dbo.Tasks AS t
LEFT JOIN tbl_User AS u ON t.TakenBy = u.UserId
LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp ON t.Fk_ProfileID = pp.Fk_ProfileID
LEFT JOIN tbl_DDLandStatus AS ls ON t.LandStatus = ls.Id
LEFT JOIN dbo.tbl_EnquiryType AS e ON t.EnquiryType = e.EnquirytypeId
WHERE (MONTH(t.DueDate) = MONTH(GETDATE())
AND YEAR(t.DueDate) = YEAR(GETDATE()))
AND t.EnquiryType = 1
GROUP BY
COALESCE(ls.Description, 'NULL')
ORDER BY
(CASE ls.Description
WHEN 'Gold - owned'
THEN 1
WHEN 'Silver - identified / offered'
THEN 2
WHEN 'Bronze - no land'
THEN 3
ELSE 0
END)