你想在这里做两件事 . 我假设您正在使用Oracle(因为您正在使用Java) .
首先,您希望对每个用户的每日交易进行分组 .
创建一个名为 tempTable 的临时表 .
使用 to_char(currentdate, 'yyyy/mm/dd') 对它们进行分组 .
INSERT INTO tempTable
SELECT
userid,
resourceid,
doc_name,
trans_id,
to_char(currentdate, 'yyyy/mm/dd') as currentday,
to_char(currentdate, 'MM') as month_value,
count(*) as daily_count
FROM
usertransaction
GROUP BY
userid,
resourceid,
doc_name,
trans_id,
to_char(currentdate, 'yyyy/mm/dd')
如果你有太多的数据行,我建议你这样做;每天做一次 .
INSERT INTO tempTable
SELECT
userid,
resourceid,
doc_name,
to_char(currentdate, 'yyyy/mm/dd') as currentday,
to_char(currentdate, 'MM') as month_value,
count(*) as daily_count
FROM
usertransaction
WHERE
to_char(currentdate, 'yyyy/mm/dd') = to_char(SYSDATE, 'yyyy/mm/dd')
GROUP BY
userid,
resourceid,
doc_name,
trans_id
其次,您需要根据 month_value 进行多次插入 .
INSERT INTO txn_jan
SELECT
userid,
resourceid,
doc_name,
to_date(current_day, 'yyyy/mm/dd') as current_day,
daily_count
FROM
tempTable
WHERE month_value='01'
INSERT INTO txn_feb
SELECT
userid,
resourceid,
doc_name,
to_date(current_day, 'yyyy/mm/dd') as current_day,
daily_count
FROM
tempTable
WHERE month_value='02'
并继续..