我需要在新列的名称中使用列的值....这是我需要帮助的行:
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
代码:
SELECT
[CustomerId],
@MetricMonth AS "MetricMonth",
@MetricYear AS "MetricYear",
[LeadType], [DeviceTypeId], [DepartmentName],
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
FROM
[myTable]
WHERE
LeadType = @LeadType
AND _CreateDate BETWEEN @StartDateTime AND @EndDateTime
GROUP BY
[CustomerId], [LeadType], [DeviceTypeId], [DepartmentName]
需要的原因是接收表有标记为这样的列,这似乎是最干净的方法。 DepartmentName
有16个可能的值,所以我不想要一堆案例陈述。
这是结果的样本。由于DepartmentName和DeviceTypeId,将有多个组。
CustomerId MetricMonth MetricYear LeadType DeviceTypeId DepartmentName NewName
28590 4 2014 Email 1 New 9
36980 4 2014 Email 1 Finance 3
876 4 2014 Email 1 New 9
谢谢!
答案 0 :(得分:1)
你实际上想要一个具有多个值的列名,即一个具有多个名称的列,这在任何SQL的风格中都是不可能的,afaik。
除此之外,您有两种选择:
如果您只想要一个名为“部门电子邮件”的栏目,其值为“部门1电子邮件:30”,那么您可以这样做:
SELECT [DepartmentName],[DepartmentName] +'电子邮件:'+ CAST(COUNT([DepartmentName])AS VARCHAR(20))
FROM [myTable]
GROUP BY [部门名称]