在没有“groupby”的情况下计入MS Access

时间:2013-11-27 10:59:55

标签: mysql ms-access

我在MS Access中有一个表,如下图所示。我想要在表中具有相同数据的记录计数与列“ID”和列“Date”以及列“Admit”对应每个记录。 在excel中我使用了“countifs”。 由于我需要所有行,因此不能在访问中使用“groupby”。 所需的结果显示在“count - admit”和“count-refund”

列中
Name    ID  Date        Admit/  Fees/      Count - Admit      Count - Refund
                               Refund   

ABC     11  11/11/2013  Admit   100         2                   1
BCD     12  11/11/2013  Admit   150         2                   0
CDE     13  12/11/2013  Admit   200         1                   1
DEF     14  13/11/2013  Admit   150         1                   0
ABC     11  11/11/2013  Refund  -100        2                   1
CDE     13  12/11/2013  Refund  -200        1                   1
ABC     11  11/11/2013  Admit   100         2                   1
BCD     12  11/11/2013  Admit   150         2                   0

3 个答案:

答案 0 :(得分:2)

在Access中,用于名为[AdmitRefund]

的表中的测试数据
Name  ID  Date        Admit_Refund  Fees  Count_Admit  Count_Refund
----  --  ----------  ------------  ----  -----------  ------------
ABC   11  2013-11-11  Admit          100                           
BCD   12  2013-11-11  Admit          150                           
CDE   13  2013-11-12  Admit          200                           
DEF   14  2013-11-13  Admit          150                           
ABC   11  2013-11-11  Refund        -100                           
CDE   13  2013-11-12  Refund        -200                           
ABC   11  2013-11-11  Admit          100                           
BCD   12  2013-11-11  Admit          150                           

以下查询

UPDATE AdmitRefund SET
    Count_Admit = DCount("*", "AdmitRefund", "[ID]=" & [ID] & " AND [Date]=#" & Format([Date], "yyyy-mm-dd") & "# AND [Admit_Refund]=""Admit"""), 
    Count_Refund = DCount("*", "AdmitRefund", "[ID]=" & [ID] & " AND [Date]=#" & Format([Date], "yyyy-mm-dd") & "# AND [Admit_Refund]=""Refund""")

填充像这样的列

Name  ID  Date        Admit_Refund  Fees  Count_Admit  Count_Refund
----  --  ----------  ------------  ----  -----------  ------------
ABC   11  2013-11-11  Admit          100            2             1
BCD   12  2013-11-11  Admit          150            2             0
CDE   13  2013-11-12  Admit          200            1             1
DEF   14  2013-11-13  Admit          150            1             0
ABC   11  2013-11-11  Refund        -100            2             1
CDE   13  2013-11-12  Refund        -200            1             1
ABC   11  2013-11-11  Admit          100            2             1
BCD   12  2013-11-11  Admit          150            2             0

编辑重新评论

上述查询假设[ID]是数字。对于[ID]作为文本,请使用:

UPDATE AdmitRefund SET
    Count_Admit = DCount("*", "AdmitRefund", "[ID]='" & [ID] & "' AND [Date]=#" & Format([Date], "yyyy-mm-dd") & "# AND [Admit_Refund]=""Admit"""), 
    Count_Refund = DCount("*", "AdmitRefund", "[ID]='" & [ID] & "' AND [Date]=#" & Format([Date], "yyyy-mm-dd") & "# AND [Admit_Refund]=""Refund""")

答案 1 :(得分:1)

      UPDATE Table1 t1
      JOIN (
         SELECT `name`,
                  `id`,
                  `date`,
                  `admit`,
                  (SELECT count(*)
                   FROM Table1 t1
                   WHERE t1.`Name` = t.`Name`
                   AND  t1.`ID` = t.`ID`
                   AND  t1.`Date` = t.`Date`
                   AND  t1.`Admit` = t.`Admit`
                   ) `CAdmit`
         FROM Table1 t
      ) t2
      ON t1.`Name` = t2.`Name`
      SET t1.`CAdmit` = t2.`CAdmit`
       WHERE     t1.`ID` = t2.`ID`
                   AND  t1.`Date` = t2.`Date`
                   AND  t1.`Admit` = t2.`Admit`
      ;

Working fiddle sample here

你也可以看一下  MySQL - Update with counter based on subquery

答案 2 :(得分:0)

我认为我看到了模式,但你仍然需要一个分组,但它将分为两部分......首先,你需要按ID查询,这样你就可以将计数应用于原始记录了。这是假设您的表格仅基于原始帖子中提供的前5列。

select
      YT.Name,
      YT.ID,
      YT.Date,
      YT.AdmitRefund,
      YT.Fees,
      PQ.AdmitCount,
      PQ.RefundCount
   from
      YourTable YT
         JOIN ( select YT2.ID
                       SUM( IIF( YT2.AdmitRefund = 'Admit', 1, 0 )) AdmitCount,
                       SUM( IIF( YT2.AdmitRefund = 'Refund', 1, 0 )) RefundCount
                   from
                      YourTable YT2
                   group by
                      YT2.ID ) PQ
           ON YT.ID = PQ.ID

内部PreQuery获取每个ID的计数,然后重新连接回原始原始数据,因此原始行的每个实例都具有相应的承认与退款计数总数。