sql - Reduce database records -
i have following table:
groupid startdate time 1 2013-01-01 15:00 10 1 2013-01-01 16:00 10 1 2013-01-01 17:00 10 1 2013-01-02 08:00 10 1 2013-01-02 09:00 10 2 2013-01-01 15:00 2 2 2013-01-01 16:00 2
which has record every hour in above example (in real data record every minute).
i need have such format today , yesterday, after time starts redundant - day grouping more enough.
i ways thinking following way of reducing above records:
- run procedure every day copy data new "temp" table, grouping them together. drop original table , rename "temp" one. of course have restore relations (groupid column in above example).
- have 2 tables time, , query view instead of table (i insert , select records). every day copy grouped records "grouped" table , remove them "detailed" table
in opinion both options generate problems, maybe there exist better way?
update
because of first answer think might misunderstood. result records should following
groupid startdate time 1 2013-01-01 00:00 30 1 2013-01-02 00:00 20 2 2013-01-01 00:00 4
so 1 record per day , group, , time column sum every detailed record.
instead of creating additional tables, can add additional rowid column table(auto generated id), , group unique columns, , use following query:
delete table1 table1 left outer join ( select min(rowid) rowid, groupid, startdate, time table1 group groupid, startdate, time ) table2 on table1.rowid = table2.rowid table2.rowid null
here explanation: have table following data:
groupid startdate time rowid 1 2013-01-01 15:01:00.000 10 101 1 2013-01-01 15:09:00.000 10 102 1 2013-01-01 15:59:00.000 10 103 1 2013-01-01 16:42:00.000 10 104 1 2013-01-01 16:52:00.000 10 105 1 2013-01-01 17:21:00.000 10 106 1 2013-01-01 17:46:00.000 10 107 1 2013-01-01 17:56:00.000 10 108 1 2013-01-02 08:10:00.000 10 109 1 2013-01-02 09:20:00.000 10 110 2 2013-01-01 15:45:00.000 2 111 2 2013-01-01 15:35:00.000 2 112 2 2013-01-01 16:35:00.000 2 113
now after run following query left 1 row per day:
delete table1 test24 table1 left outer join ( select min(rowid) rowid, groupid, convert(varchar(10),startdate,120)startdate, time test24 group groupid, convert(varchar(10),startdate,120), time ) table2 on table1.rowid = table2.rowid table2.rowid null groupid startdate time rowid 1 2013-01-01 15:01:00.000 10 101 1 2013-01-02 08:10:00.000 10 109 2 2013-01-01 15:45:00.000 2 111
now, can run update statement change date per required format.
update test24 set startdate=convert(varchar(10),startdate,120) groupid startdate time rowid 1 2013-01-01 10 101 1 2013-01-02 10 109 2 2013-01-01 2 111
Comments
Post a Comment