使用access sql将总计分组为其他列值

时间:2015-11-11 15:04:00

标签: sql ms-access ms-access-2010

我有一张桌子A

 EK_ID  ECID    WCD    WCP
1004    1001    2        0
1004    1001    2        5
1004    1001    3        2
1005    1025    2        5
1005    1025    4        3
1006    1025    6        2
1006    1025    5        8

我尝试将WC_D,WC_P列(按EK_ID分组)的结果写为其他列值(根据EK_ID重复总计)。结果表应该是这样的。我有一种情况需要将WCD值除以总计。

EK_ID   ECID  WCD  WCP  Total_WCD  Total_WCP
1004    1001    2   0          7      7
1004    1001    2   5          7      7
1004    1001    3   2          7      7
1005    1025    2   5          6      8
1005    1025    4   3          6      8
1006    1025    6   2          11     10
1006    1025    5   8          11     10

2 个答案:

答案 0 :(得分:0)

select YT.*,T.*
from #YourTable YT
join (
    select EK_ID, SUM(WCD) as Total_WCD, SUM(WCP) as Total_WCP
    from #YourTable YT
    group by EK_ID
) T on T.EK_ID = YT.EK_ID

完整示例

    create table #YourTable(EK_ID int, ECID int, WCD int,  WCP int)
insert into #YourTable
select 1004, 1001, 2, 0
union select 1004, 1001, 2, 5
union select 1004, 1001, 3, 2
union select 1005, 1025, 2, 5
union select 1005, 1025, 4, 3
union select 1006, 1025, 6, 2
union select 1006, 1025, 5, 8



select YT.*,T.*
from #YourTable YT
join (
    select EK_ID, SUM(WCD) as Total_WCD, SUM(WCP) as Total_WCP
    from #YourTable YT
    group by EK_ID
) T on T.EK_ID = YT.EK_ID

答案 1 :(得分:0)

创建此查询:

DateFormat dateInstance = new SimpleDateFormat("EEE");

然后,您请求格式的表是:

SELECT EK_ID, Sum(WCD) AS Total_WCD, Sum(WCP) AS Total_WCP FROM table GROUP BY EK_ID