我有一个包含以下列的表:
Cost Rate
Repair 12
Repair 223
Wear 1000
Wear 666
Fuel 500
Repair 600
Fuel 450
Wear 400
我希望这些数据作为列(修复,磨损,燃料):
Repair Wear Fuel
825 2066 950
如何使用MS Access查询执行此操作?
答案 0 :(得分:5)
虽然有一个传统的SQL解决方案非常复杂,但是阅读this page提醒我MS Access有一个TRANSFORM ... PIVOT语句,您可能应该查看并使用它来执行此操作。< / p>
我无法确定但它看起来应该是这样的:
TRANSFORM Sum([Items].[Rate]) AS SumOfRate
SELECT [Items].[Costs]
FROM Items
GROUP BY [Items].[Costs]
PIVOT Format([Items].[Costs]);
它可以变得更加漂亮。 E.G。
PIVOT Format([Items].[month],"mmm") In ("Jan","Feb",...,"Nov","Dec");
答案 1 :(得分:3)
未经测试但
SELECT
(SELECT sum(rate) from mytable where cost = 'Repair') AS Repair ,
(SELECT sum(rate) from mytable where cost = 'Wear') AS Wear,
(SELECT sum(rate) from mytable wherecost = 'Fuel') AS Fuel
答案 2 :(得分:2)
您可以使用此查询
select
sum(select rate from yourtable where cost = 'Repair') "Repair",
sum(select rate from yourtable where cost = 'Wear') "Wear",
sum(select rate from yourtable where cost = 'Fuel') "Fuel"
from dual
如果您使用的是Oracle
否则,如果有任何其他数据库引擎,您可以在此之前快速声明
create table dual (x char)
insert into dual values "x"
答案 3 :(得分:2)
您可以使用交叉表查询执行此操作。 “访问查询”窗口中有一个向导,可指导您创建一个向导。只需单击“新建查询”按钮,然后选择“交叉表查询向导”。
答案 4 :(得分:1)
这将获得您需要的数据:
select cost, sum(rate)
from mytable
group by cost
然后你可以按照自己喜欢的方式呈现它。