不是查詢總數,我需要查詢兩個字段,使用convert函數按時間查詢再分組,就得將兩個字段放入分組條件中,就錯亂了。
假設有表table,字段有時間精確到秒,ID,消費金額和消費地點,現在要查詢一段時間每天的消費金額和地點(時間可隨意指定)
圍繞convert函數進行時間的篩選,還是可以的但是效率不高,
select c.autoid,c.callerid,c.doorid,c.cardday,c.eventtype,d.doorid as max_doorId,d.eventType as max_eventSort,d.cardday as max_cardDay
from(select * from cr_cardevent a
WHERE EXISTS (SELECT 1
FROM(
SELECT convert(varchar(10),cardday,121) as td, MIN(cardday)min_time,callerid FROM cr_cardevent c,Com_EmpCard card
Where cardday between '2014-09-01' and '2014-09-03' and c.cardid=card.cardid and card.cardid in
(select fingure.cardid from Finger_Data fingure) GROUP BY convert(varchar(10),cardday,121), callerid)b
WHERE a.cardday=b.min_time AND a.callerid=b.callerid ))c ,
(select *
from cr_cardevent a WHERE EXISTS (SELECT 1
FROM(SELECT convert(varchar(10),cardday,121) as td, MAX(cardday)max_time,callerid FROM cr_cardevent c,Com_EmpCard card
where cardday between '2014-09-01' and '2014-09-03' and c.cardid=card.cardid and card.cardid in
(select fingure.cardid from Finger_Data fingure) GROUP BY convert(varchar(10),cardday,121), callerid)b
WHERE a.cardday=b.max_time AND a.callerid=b.callerid))d,
cr_caller_basicinfo e
where c.callerid = d.callerid and c.callerid = e.callerid and e.callertype=1
and convert(varchar(10),c.cardday,121)=convert(varchar(10),d.cardday,121)
需求是一段時間內每天每個用戶最大最小的兩筆數據