程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sql server-sqlserver查詢本周每天數據

sql server-sqlserver查詢本周每天數據

編輯:編程綜合問答
sqlserver查詢本周每天數據

不是查詢總數,我需要查詢兩個字段,使用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)
需求是一段時間內每天每個用戶最大最小的兩筆數據

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved