如何将一行中的字段转换为列?

发布于 2024-07-25 10:03:50 字数 412 浏览 5 评论 0原文

我有一个表,其中包含以下列:

          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 查询来执行此操作?

I have a table with columns such as:

          Cost     Rate

          Repair   12
          Repair   223
          Wear     1000    
          Wear     666
          Fuel     500
          Repair   600
          Fuel     450
          Wear     400

and I want this data as columns(Repair,Wear,Fuel) as:

         Repair    Wear   Fuel
           825     2066    950

How could I do this using an MS Access Query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暗地喜欢 2024-08-01 10:03:50

虽然有一个传统的 SQL 解决方案非常笨拙,但阅读此页面会引起警惕我注意到 MS Access 有一个 TRANSFORM ... PIVOT 语句您可能应该研究并使用来执行此操作。

我不能确定,但​​它应该看起来像:

TRANSFORM Sum([Items].[Rate]) AS SumOfRate 
SELECT [Items].[Costs] 
FROM Items 
GROUP BY [Items].[Costs] 
PIVOT Format([Items].[Costs]);

而且它可以变得比这更奇特。 例如

PIVOT Format([Items].[month],"mmm") In ("Jan","Feb",...,"Nov","Dec");

While there's a traditional SQL solution for this that is pretty kludgy, reading this page alerted me to the fact that MS Access has a TRANSFORM ... PIVOT statement you probably should look into and use to do this.

I can't be certain but it should look like:

TRANSFORM Sum([Items].[Rate]) AS SumOfRate 
SELECT [Items].[Costs] 
FROM Items 
GROUP BY [Items].[Costs] 
PIVOT Format([Items].[Costs]);

And it can get fancier than this. E.G.

PIVOT Format([Items].[month],"mmm") In ("Jan","Feb",...,"Nov","Dec");
烟雨扶苏 2024-08-01 10:03:50

未经测试但是

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

untested but

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
空气里的味道 2024-08-01 10:03:50

可以使用此查询,

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"

you can use this query

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

if you are using Oracle

otherwise, if any other database engine, you can make a quick statement right before this one

create table dual (x char)

insert into dual values "x"
南笙 2024-08-01 10:03:50

您可以使用交叉表查询来完成此操作。 “访问查询”窗口中有一个向导将引导您创建一个。 只需单击“新建查询”按钮并选择“交叉表查询向导”即可。

You can do this with a Crosstab query. There is a wizard in the Access Queries window that will walk you through creating one. Just click the New Query button and select the Crosstab Query Wizard.

疯了 2024-08-01 10:03:50

这将获得您需要的数据:

select cost, sum(rate)

from mytable

group by cost

然后您可以以您喜欢的方式呈现它。

This will get the data you need:

select cost, sum(rate)

from mytable

group by cost

and then you can present it whichever way you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文