我可以在Access中的交叉表查询中添加虚拟行

时间:2014-12-15 22:14:19

标签: sql ms-access pivot crosstab

当源表/查询中不存在数据时,是否可以向交叉表查询添加虚拟行标题?我想表明我知道某个类别存在,即使目前没有任何类别属于该类别。

为了说明,我想接受这个输出......

| Category    | Total  | Class 1 | Class 2   | Class 3  | Class 4  |
| PARTK2130   |  0.10  | 0.00    |  0.00     |  0.10    | 0.00     |
| PARTK3140   |  0.29  | 0.01    |  0.24     |  0.04    | 0.00     |
| PARTK4150   | 30.98  | 0.33    | 12.22     | 18.44    | 0.00     |

让它看起来像这样(注意* New *行)......

| Category    | Total | Class 1  | Class 2   | Class 3  | Class 4  |
| PARTK1120   |  0.00 |  0.00    |  0.00     |  0.00    | 0.00     | *New*
| PARTK2130   |  0.10 |  0.00    |  0.00     |  0.10    | 0.00     |
| PARTK3140   |  0.29 |  0.01    |  0.24     |  0.04    | 0.00     |
| PARTK4150   | 30.98 |  0.33    | 12.22     | 18.44    | 0.00     |
| PARTK5160   |  0.00 |  0.00    |  0.00     |  0.00    | 0.00     | *New*
| PARTK6170   |  0.00 |  0.00    |  0.00     |  0.00    | 0.00     | *New*
| PARTK7180   |  0.00 |  0.00    |  0.00     |  0.00    | 0.00     | *New*

我能想到这样做的唯一方法是创建一个新的虚拟源表/查询,用一些虚拟记录填充它,然后修改我的交叉表查询作为UNION查询,查看这个虚拟源以及原版的。我得到了它的工作,但这似乎像很多猴子业务。

实际上,我学会了在没有数据时添加额外列的方法(参见Class 4列和this post),所以我认为添加额外的行也是一种技巧。希望无论如何。

这是SQL:

TRANSFORM Format(Nz(Sum(qryPartK.MILES_OF_PHYS_LENGTH),0),"Fixed") 
AS SumOfMILES_OF_PHYS_LENGTH
SELECT IIf([PCT_SMYS]<=30,"PARTK2130",
IIf([PCT_SMYS]<=40,"PARTK3140", IIf([PCT_SMYS]<=50,"PARTK4150",
IIf([PCT_SMYS]<=60,"PARTK5160",IIf([PCT_SMYS]<=72,"PARTK6170",
IIf([PCT_SMYS]>72 And [PCT_SMYS]>=80,"PARTK7180",
IIf([PCT_SMYS]>80,"PARTK80MORE"))))))) AS Category, 
Sum(qryPartK.MILES_OF_PHYS_LENGTH) AS Total
FROM qryPartK
GROUP BY IIf([PCT_SMYS]<=30,"PARTK2130",
IIf([PCT_SMYS]<=40,"PARTK3140",IIf([PCT_SMYS]<=50,"PARTK4150",
IIf([PCT_SMYS]<=60,"PARTK5160",IIf([PCT_SMYS]<=72,"PARTK6170",
IIf([PCT_SMYS]>72 And [PCT_SMYS]>=80,"PARTK7180",
IIf([PCT_SMYS]>80,"PARTK80MORE")))))))
PIVOT qryPartK.CLASS_LOC_text In ("Class 1","Class 2","Class 3","Class 4");

2 个答案:

答案 0 :(得分:1)

使用带有子查询的outer join

select c.category, r.total, r.class1, r.class2, r.class3, r.class4
from (select 'PARTK1120' category 
      union all select 'PARTK2130'
      union all select 'PARTK3140'
      union all select 'PARTK4150'
      union all select 'PARTK5160'
      union all select 'PARTK6170'
      union all select 'PARTK7180') c 
left join yourresults r on c.category = r.category

您可以使用coalescenull值转换为0coalesce(r.total, 0) total

答案 1 :(得分:0)

UNION使您的数据与众不同。如果类别不同,它将在结果中显示。如果您只返回有价值的行,则必须添加Where Total <> 0

修改

TRANSFORM Format(Nz(Sum(qryPartK.MILES_OF_PHYS_LENGTH),0),"Fixed") 
AS SumOfMILES_OF_PHYS_LENGTH
SELECT IIf([PCT_SMYS]<=30,"PARTK2130",
IIf([PCT_SMYS]<=40,"PARTK3140", IIf([PCT_SMYS]<=50,"PARTK4150",
IIf([PCT_SMYS]<=60,"PARTK5160",IIf([PCT_SMYS]<=72,"PARTK6170",
IIf([PCT_SMYS]>72 And [PCT_SMYS]>=80,"PARTK7180",
IIf([PCT_SMYS]>80,"PARTK80MORE"))))))) AS Category, 
Sum(qryPartK.MILES_OF_PHYS_LENGTH) AS Total
FROM qryPartK

WHERE Sum(qryPartK.MILES_OF_PHYS_LENGTH)<>0

GROUP BY IIf([PCT_SMYS]<=30,"PARTK2130",
IIf([PCT_SMYS]<=40,"PARTK3140",IIf([PCT_SMYS]<=50,"PARTK4150",
IIf([PCT_SMYS]<=60,"PARTK5160",IIf([PCT_SMYS]<=72,"PARTK6170",
IIf([PCT_SMYS]>72 And [PCT_SMYS]>=80,"PARTK7180",
IIf([PCT_SMYS]>80,"PARTK80MORE")))))))
PIVOT qryPartK.CLASS_LOC_text In ("Class 1","Class 2","Class 3","Class 4");

尝试添加WHERE。