1

我在下面有这个示例数据(表1):

PKEY   DATE
----   ----
ABC1   2/13
CFG5   1/05
POK9   1/10

我从另一个表(table2)中获得了这些日期:

PKEY   REF#   DATE
----   ----   ----
1      ABC1   2/14
2      ABC1   2/20
3      ABC1   3/01
4      CFG5   3/05
5      CFG5   2/10
6      ABC1   2/25

我想从table2每个 REF#)获取最大日期,并从table1获取与日期的差异。显示以下结果。

REF#   DIFF   
----   ----
POK9    55
CFG5    54
ABC1    21  [ *note: this is equal to datediff(day,table1.DATE,getdate()) ]

*另外,如果表 2 中不存在 REF#(如上例中的第 3 行),我想使用今天的日期(从表 1 中扣除日期)。希望这是有道理的..让我知道是否有任何不清楚的地方,以便我立即编辑..

**更新:这是我迄今为止尝试过的..但仍然不完整..感谢您的辛勤工作

select t1.[REF#]
from table1 as t1
join table2 as t2
on t1.[REF#] COLLATE DATABASE_DEFAULT = t2.[REF#] COLLATE DATABASE_DEFAULT
4

3 回答 3

2
WITH recorddates
AS
(
    SELECT  PKEY, REFNo, DATE,
            ROW_NUMBER() OVER (PARTITION BY RefNO
                                ORDER BY DATE DESC) rn
    FROM    table2
)
SELECT  a.PKEY, 
        datediff(day,a.DATE,b.date) 
FROM    table1 a
        INNER JOIN recorddates b
            ON a.PKey = b.RefNo
WHERE   b.rn = 1
于 2013-03-06T07:57:22.890 回答
1
SELECT t1.PKEY, CASE WHEN o.DATE IS NULL 
                     THEN DATEDIFF(DAY, t1.DATE, GETDATE()) 
                     ELSE DATEDIFF(DAY, t1.DATE, o.DATE) END AS DIFF
FROM dbo.table1 t1 OUTER APPLY (
                                SELECT MAX(t2.Date) AS Date
                                FROM dbo.table2 t2     
                                WHERE t1.PKEY = t2.REF# 
                                ) o

SQLFiddle上的演示

或没有 APPLY() 运算符

SELECT t1.PKEY, (SELECT CASE WHEN MAX(o.Date) IS NULL 
                             THEN DATEDIFF(DAY, t1.Date, GETDATE()) 
                             ELSE DATEDIFF(DAY, t1.Date, MAX(o.Date)) END AS DIFF
                 FROM dbo.test27 o
                 WHERE t1.PKEY = o.REF#
                 ) AS DIFF                               
FROM dbo.test26 t1  

用你的标准查询

SELECT *
FROM (
      SELECT t1.PKEY, (SELECT CASE WHEN MAX(o.Date) IS NULL 
                              THEN DATEDIFF(DAY, t1.Date, GETDATE()) 
                              ELSE DATEDIFF(DAY, t1.Date, MAX(o.Date)) END AS DIFF
                       FROM dbo.test27 o
                       WHERE t1.PKEY = o.REF#
                       ) AS DIFF                               
      FROM dbo.test26 t1
      ) s
WHERE s.DIFF BETWEEN 110 AND 120   
于 2013-03-06T08:14:33.570 回答
1

像这样的东西?

select a.pkey
      ,datediff(day, max(a.date), coalesce(max(b.date), getdate())) as diff 
  from table1      a
  left join table2 b on(b.ref# = a.pkey)
 group 
    by a.pkey;

编辑评论:您应该能够像这样过滤:

select a.pkey
      ,datediff(day, max(a.date), coalesce(max(b.date), getdate())) as diff 
  from table1      a
  left join table2 b on(b.ref# = a.pkey)
 group 
    by a.pkey
having datediff(day, max(a.date), coalesce(max(b.date), getdate())) between 110 
                                                                        and 120;

或通过像这样包装语句:

select *
  from (select a.pkey
              ,datediff(day, max(a.date), coalesce(max(b.date), getdate())) as diff 
          from table1      a
          left join table2 b on(b.ref# = a.pkey)
         group 
            by a.pkey
       )
 where diff between 110 and 120;
于 2013-03-06T08:35:22.297 回答