简体   繁体   English

如何根据表1中的日期从表2中选择最小/最大日期?

[英]How do I select min/max dates from table 2 based on date in table 1?

I have a monthly table (only holds rows with first day of month, and unique constraint so only one of each) and a daily table with similar information for each day (same deal, only one per day): 我有一个月度表(仅包含一个月的第一天的行,并且具有唯一性约束,因此每个月只有一个),还有一个日表,每天的信息相似(同一笔交易,每天仅一个):

Monthly table       Daily table
-------------       -----------
2009-01-01          2009-01-01
2009-02-01          2009-01-02
: :                 : :
2009-09-01          2009-01-31
                    2009-02-01
                    : :
                    2009-02-28
                    2009-03-01
                    : :
                    2009-09-01

but there may be days missing. 但可能会缺少几天。

I want to run a query that returns, for each date in the monthly table, that date along with the minimum and maximum dates for that month in the daily table (using standard SQL preferably of DB2-specific if absolutely necessary). 我想运行一个查询,该查询针对月表中的每个日期返回该日期以及日表中该月的最小和最大日期(如果绝对必要,请使用标准SQL,最好是DB2专用的标准SQL)。

So, if the last week of January and first week of February is missing, I need: 因此,如果缺少一月的最后一周和二月的第一周,我需要:

MonthStart  FirstDay    LastDay
----------  ----------  ----------
2009-01-01  2009-01-01  2009-01-24
2009-02-01  2009-02-08  2009-02-28
: :
2009-09-01  2009-09-01  2009-01-30

Here's the query I have: 这是我的查询:

select m.date as m1,               
       dx.d1 as m2,                          
       dx.d2 as m3                           
from   monthly m,              
       ( select min(d.date) as d1,
                max(d.date) as d2        
         from   daily d            
         where  month(d.date) = month(m.date)
         and    year(d.date) = year(m.date)    
       ) as dx;

but I'm getting the error: 但我得到了错误:

DSNT408I SQLCODE = -206, ERROR: M.DATE IS NOT A COLUMN OF
AN INSERTED TABLE, UPDATED TABLE, OR ANY TABLE IDENTIFIED
IN A FROM CLAUSE, OR IS NOT A COLUMN OF THE TRIGGERING
TABLE OF A TRIGGER
DSNT418I SQLSTATE = 42703 SQLSTATE RETURN CODE

Does anyone have any advice on how best to do this? 有人对如何最好地做到这一点有任何建议吗?

SELECT m.date as m1,               
       MIN(d.date) as m2,                          
       MAX(d.date) as m3
       -- COUNT(d.date) as NbOfDays  -- if so desired...                          
FROM monthly m
JOIN daily d ON month(d.date) = month(m.date) and year(d.date) = year(m.date) 
WHERE -- some condition
GROUP BY m.date
ORDER BY m.date -- or something else...

Note that this type of query (as the other ones shown in the question and in responses so far, are relatively slow, as they imply a table scan so the month(x) and year(x) can be calculated for each [qualifying] row. (ie putting a range on date in the WHERE clause would of course help). If this type of query is ran frequently and if the size of table is significant, i t may be useful to add a column for these computed values (or possibly for the combined value (Year-Month), so that not only the calculation is unnecessary but the underlying column(s) can be indexed. 请注意,这种类型的查询(如问题和到目前为止的回答中所示的其他查询,相对较慢,因为它们暗示着进行表扫描,因此可以为每个[qualify]计算出month(x)和year(x)行(即在WHERE子句中放置日期范围当然会有所帮助)。如果这种查询类型经常运行,并且表的大小很大, 则为这些计算值添加一列可能会有用 (或可能是组合值(“年-月”),因此不仅不必进行计算,而且可以为基础列建立索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM