访问查询 - 使用子查询计数器添加列

时间:2015-06-19 20:51:52

标签: sql ms-access

我需要添加一个合适的" needCounter"我在Access中查询的列代码。 它应该计算从1到#34; WorksPerWeekCounter"的行数。因为它显示在最后一个黄色列中。或者更好的0到" WorksPerWeekCounter-1" :)

我发现我的代码错了,因为我的"计数器"与我的" counterMin"

不对应

我们说我有一个表[WORKS(roboczy)],前4列就像下面[http://www.submitdigital.com/questions/magento-1-7-image-upload-issues-help-image-type-and-information-need-to-be-specified-for-each-store-view/节目一样。 (对不起,我的声誉太低,无法显示图片)

这是我使用的Access查询代码(week2date函数在VBA代码中):

SELECT   
    W.Id_work, W.Id_pracownika, W.vecka, W.Rok,
    week2date(W.vecka,W.Rok) AS dataWK,
    C.countWorksPerWeek as WorksPerWeekCounter,
    C.Id_workMin as id_workMin,
    (W.Id_work- C.Id_workMin) as AddDays,
    DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work]) AS counter,
    C.counterMin
FROM [WORKS(roboczy)] W
inner join 
( SELECT count(id_work) as countWorksPerWeek, Id_pracownika, vecka, Rok, min(id_work) as Id_workMin, min(DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work])) AS counterMin
    FROM [WORKS(roboczy)]
    GROUP BY Id_pracownika, vecka, Rok
) C ON W.Id_pracownika = C.Id_pracownika AND W.vecka = C.vecka AND C.Rok = W.Rok
GROUP BY W.Id_pracownika, W.vecka, Id_work, W.Rok, week2date(W.vecka,W.Rok), C.countWorksPerWeek, C.Id_workMin, C.counterMin

1 个答案:

答案 0 :(得分:0)

您可以一步使用一系列子查询:

SELECT w.Id_work, w.Id_pracownika, w.vecka, w.Rok, w.dataWK,

(SELECT count(w2.Rok) FROM WorkCounter w2 
WHERE w2.vecka = w.vecka GROUP BY w2.vecka) As WorksPerWeekCounter,

(SELECT min(w3.Id_work) FROM WorkCounter w3 
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As id_workMin, 

w.Id_work - 
(SELECT min(w3.Id_work) FROM WorkCounter w3 
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As AddDays,

(SELECT count(*) FROM WorkCounter w4 
WHERE w4.Id_work <= w.Id_work and w4.vecka = w.vecka) As Counter

FROM [WORKS(roboczy)] W