This paper presents a novel method for optimizing sliding window based continuous queries. We deal with two categories of aggregation operations: stepwise aggregation (e.g. COUNT) 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. An optimal query plan can be obtained by studying all the factors simultaneously, leading to the minimum cost. Experiments are conducted to validate the effectiveness of the proposed method.