简体   繁体   中英

Access query to partition data and sum each partition?

I have a query with the fields date hour and value .
It looks something like this

    date       hour   value 
    xx/xx/xx     15     100
    xx/xx/xx     30     122
    xx/xx/xx     45      50
    ...         100     100
    ...         115      23
    ...         ...     ...
    ...         ...     ...
    ...        2400     400
    ...          15      23

Basically, date is the date, hour is the hour, and value is the value for that particular 15 minute interval. What I have been trying to figure out is a way to take each hour (so 15, 30, 45, and 100) or (1015, 1030, 1045, 1100) [As you can see hours are military-esque 1:00pm is 1300 and midnight 2400], and sum their values together. So i am looking to return something like this:

    xx/xx/xx    100     372
    xx/xx/xx    200     23 + (130 data) + (145 data) + (200 data)

And so on... The table has on average around 100 days and they all start from 15 to 2400 incrementing by 15 with varying numbers for the value column.

I have thought about using a partition, group by, etc. with no real ideas how to tackle it. Essentially I have to take 4 rows (an hour), sum their values, spit out the date, hour, and summed value then repeat for every day. I am not asking for code, just some help with what i should be using since this seems like a simple problem minus the key to solving it.

Any help is greatly appreciated, Thank you!

I may have misremembered how you cast to int in Access, but this might work:

Select
  [date],
  100 * (1 + Cint(([Hour] - 1) / 100)),
  Sum(Value)
From
  Query
Group By
  [date],
  100 * (1 + Cint(([Hour] - 1) / 100))
Order By
  1, 2
SELECT
   DateCol,
   Int(HourCol \ -100) * -100 AS Hr,
   Sum(Value) AS Value
FROM
   YourTable
GROUP BY
   DateCol,
   Int(HourCol \ -100) * -100

Or you can use ((Hr + 99) \\ 100) * 100 .

Grouping by Hour/100 will almost get you there - subtract 1 from the hour will make 1 AM fall to 99, and get included in the grouping. This will give a query that looks like this:

SELECT Table1.Dte, Int(([tme]-1)/100) AS Hr, Sum(Table1.Val) AS TotVal
FROM Table1
GROUP BY Table1.Dte, Int(([tme]-1)/100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM