您好我有一个访问查询,任何人都可以帮我请我将查询转换为MS SQL 2008查询。
挣扎于Transform
和PIVOT
。
访问查询是:
TRANSFORM Count(vwg_vkCustomers.CustomerNo) AS CountOfCustomerNo
SELECT vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode
FROM vwg_vkCustomers
WHERE (((vwg_vkCustomers.TutorCode)="123456") AND ((vwg_vkCustomers.CustomerAdded)>#6/21/2011#))
GROUP BY vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode
PIVOT vwg_vkCustomers.Type;
谢谢
答案 0 :(得分:2)
我不知道IIF的T-SQL版本,但这是不使用TRANSFORM / PIVOT的交叉表查询的Access版本。对每个所需的列重复IIF表达式:
SELECT vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode,
Sum(IIF(vwg_vkCustomers.Type="Whatever",1,0) AS TypeWhatever
FROM vwg_vkCustomers
WHERE (((vwg_vkCustomers.TutorCode)="123456") AND ((vwg_vkCustomers.CustomerAdded)>#6/21/2011#))
GROUP BY vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode
答案 1 :(得分:2)
将此与TimD的答案结合起来,因为我不经常与TRANSFORM
争吵....
对于Iif
,请使用CASE
:
...
CASE vwg_vkCustomers.Type
WHEN 'Whatever' 1
ELSE 0
END,
...
请注意,您使用单引号而不是双引号。