【发布时间】:2016-06-17 15:48:51
【问题描述】:
我试图仅在两个字段满足特定条件时才计算它们的记录。
这个 [Is it possible to specify condition in Count()?] 帖子很有帮助,但它没有考虑将 varchar 转换为 int。
这是我的代码:
SELECT Mailing_Id ,Mailing_Nm,Subject_Line,Campaign_Nm,Start_Ts,End_Ts, Mailed_Cnt, Invalid_Cnt ,Actual_Sent_Cnt ,Bounce_Cnt ,Open_Cnt ,Click_Cnt
,count(case ag.logtype when '7' then 1 end) as Unsubs
,count(case ag.category when '1' then 1 end) as Block
,count(case ag.category when '2' then 1 end) as Hard
,count(case ag.category when '3' then 1 end) as Soft
,count(case ag.category when '4' then 1 end) as Tech
,count(case ag.category when '9' then 1 end) as Unknown
FROM [StrongMailTracking].[dbo].[SM_MAILING_SUMMARY] ms left join sm_aggregate_log ag on ms.mailing_id = ag.mailingid
WHERE datepart(year,start_ts) = 2015 and (mailing_nm not like '%delivery report%' and mailing_nm not like '%daily helpdesk%' and mailing_nm not like '%test%')
GROUP BY Mailing_Id ,Mailing_Nm ,Subject_Line ,Campaign_Nm ,Start_Ts ,End_Ts ,Mailed_Cnt ,Invalid_Cnt ,Actual_Sent_Cnt ,Bounce_Cnt ,Open_Cnt ,Click_Cnt
ORDER BY mailing_id asc
请注意这 6 个案例陈述。日志类型是 int,类别是 varchar。
我试过了:
- 删除单引号
- 当 ...
- 在投射时删除单引号
- 先转换为数字然后再转换为整数
但我不断收到此错误:“Msg 245, Level 16, State 1, Line 1 将 varchar 值 'dynamic-preview-7179' 转换为数据类型 int 时转换失败。”
有人知道该怎么做吗?
【问题讨论】:
-
我认为这没有问题。除非数据类型是 int,否则您将字符串与 int 进行比较
-
问题是不能在SQL case 语句中使用两种数据类型。它只能返回一种数据类型,并且它的选择方式有一个偏好顺序。使用 Gordon Linoffs 的答案。 msdn.microsoft.com/en-us/library/ms181765.aspx如果您想了解更多信息,请在该帖子中搜索数据类型。
-
@Sam Gordon 的回答无效,请查看我的评论
-
您是否必须删除所有计数语句才能使该查询正常工作,或者您是否尝试删除每个计数语句以查看导致问题的原因?
-
还要检查 sm_aggregate_log 表中的字段 logtye 和 category 以查看其中包含 'dynamic-preview-7179'
标签: sql sql-server