In sensor networks, continuous query is commonly used for collecting periodical data from the objects under monitoring. This sort of queries needs to be carefully designed, in order to minimize the power consumption and maximize the lifetime of the sensor nodes. Data reduction techniques can be employed to decrease the size and frequency of data to be transferred in the network, and therefore save energy. This paper presents a novel method for optimizing sliding window based continuous queries. In particular, we deal with two categories of aggregation operations: stepwise aggregation (e.g. MAX, MIN, SUM, COUNT, AVERAGE, etc.) and direct aggregation (e.g. MEDIAN). Our approach is, by using packet merging or compression techniques, to reduce the data size to the best extent, so that the total performance is optimal. A QoS weight item is specified together with a query, in which the importance of the four factors, power, delay, accuracy and error rate can be expressed. Then an optimal que...