1

我有以下数据要创建交叉表查询:

 ID   CallNo     CopyrightYear
 1    AH         2000
 2    AB         2000
 3    BC         2000
 4    AH         2002
 5    ZA         2005
 6    BC         2001
 7    AP         2003

这是我目前拥有的交叉表查询:

 TRANSFORM Count(Table1.[ID]) AS CountOfID
 SELECT Table1.[CallNo], Count(Table1.[ID]) AS [Total Of ID]
 FROM Table1
 GROUP BY Table1.[CallNo]
 PIVOT Table1.[CopyrightYear];

所以我的问题是我如何将版权年份从 2000 年到 2002 年和 2003 年到 2005 年等等...

预期成绩:

CallNo      2000 to 2002    2003 to 2005
AB              1   
AH              2   
AP                              1
BC              2   
ZA                              1
4

2 回答 2

1

这是你的答案:

TRANSFORM Count(tab1.[ID]) AS CountOfID
SELECT tab1.[CallNo], Count(tab1.[ID]) AS [Total Of ID]
FROM tab1
GROUP BY tab1.[CallNo]
PIVOT CStr(Int(([CopyrightYear]+1)/3)*3-1)+' to '+CStr(Int(([CopyrightYear]+1)/3)*3+1);

事实上,你可以只关注Int(([CopyrightYear]+1)/3)*3-1. CStr 转换主要用于装饰目的

对于 2000 年到 2004 年、2005 年到 2009 年等年,表达式将是(注意它是54 年范围的值):

TRANSFORM Count(tab1.[ID]) AS CountOfID
SELECT tab1.[CallNo], Count(tab1.[ID]) AS [Total Of ID]
FROM tab1
GROUP BY tab1.[CallNo]
PIVOT CStr(Int(([CopyrightYear])/5)*5)+' to '+CStr(Int(([CopyrightYear])/5)*5+4)
于 2013-03-07T05:04:20.513 回答
0
SELECT CallNo,
CASE WHEN COUNT(Copyrightyear =>2000 AND <=2002) AS '2000 to 2002', COUNT(Copyrightyear => 2003 AND <=2005) AS '2003 to 2005'
FROM Table1
GROUP BY CallNo

我不确定这是否可行。

于 2013-03-07T05:16:04.503 回答