SELECT
CAST(c.DT AS DATE) AS 'Date'
, COUNT(p.PatternID) AS 'Count'
FROM CalendarMain c
LEFT OUTER JOIN Pattern p
ON c.DT = p.PatternDate
INNER JOIN Result r
ON p.PatternID = r.PatternID
INNER JOIN Detail d
ON p.PatternID = d.PatternID
WHERE r.Type = 7
AND d.Panel = 501
AND CAST(c.DT AS DATE)
BETWEEN '20190101' AND '20190201'
GROUP BY CAST(c.DT AS DATE)
ORDER BY CAST(c.DT AS DATE)
上面的查询不适用于我。它仍然会跳过c.DT的COUNT为NULL的日子。
c.DT和p.PatternDate都是时间DateTime,尽管c.DT不能为NULL。它实际上是表的PK。从2015年到2049年的每一天,它都被填充为DateTimes,因此存在这些天的记录。
我注意到的另一个奇怪的事情是,当我加入C.DT = p.PatternDate而没有CAST或CONVERT转换为Date样式时,什么也没有返回。不确定为什么它们都是DateTimes。
答案 0 :(得分:0)
这里有几件事要谈。目前尚不清楚您实际要计算的内容。如果它是2019年1月的每日“模式”数,则:
我要假设:
data = [['Thursday 4 January', 11.58, 0.38], ['Thursday 4 January', 16.95, 0.73], ['Thursday 4 January', 23.68, 0.02], ['Friday 5 January', 6.48, 0.83], ['Friday 5 January', 12.42, 0.33]]
dic={'day':[],'min':[],'max':[]}
for i in data :
dic['day']+=[i[0]]
dic['min']+=[i[1]]
dic['max']+=[i[2]]
from collections import defaultdict
x = defaultdict(dict)
for i in dic['day']:
x[i]={'min':[],'max':[]}
for i in range(len(dic['min'])):
x[dic['day'][i]]['min']+=[dic['min'][i]]
x[dic['day'][i]]['max']+=[dic['max'][i]]
print(dict(x))
"""
structure data for future use
{'Friday 5 January': {'max': [0.83, 0.33], 'min': [6.48, 12.42]},
'Thursday 4 January': {'max': [0.38, 0.73, 0.02],
'min': [11.58, 16.95, 23.68]}}
"""
result =[]
for i in x:
result.append(r'{}: {} meters at lowest an {} meters at highest'.format(i,min(x[i]['min']),max(x[i]['max'])))
print(result)
"""output
['Thursday 4 January: 11.58 meters at lowest an 0.73 meters at highest', 'Friday 5 January: 6.48 meters at lowest an 0.83 meters at highest']
""""
--Set up some dummy data