4

抱歉标题混乱。请告诉我是否可以通过 db 请求进行操作。假设我们有下表

   ind_id      name                 value       date
----------- -------------------- ----------- ----------
1           a                    10          2010-01-01
1           a                    20          2010-01-02
1           a                    30          2010-01-03
2           b                    10          2010-01-01
2           b                    20          2010-01-02
2           b                    30          2010-01-03
2           b                    40          2010-01-04
3           c                    10          2010-01-01
3           c                    20          2010-01-02
3           c                    30          2010-01-03
3           c                    40          2010-01-04
3           c                    50          2010-01-05
4           d                    10          2010-01-05

我需要查询所有行以包含给定日期的每一行ind_id,如果没有ind_id给定日期,则取最近的较低日期,如果没有任何较低的日期,则返回 ind_id + name(name/ind_id 对相等)带空值。例如,日期是 2010-01-04,我希望得到以下结果:

ind_id      name                 value       date
----------- -------------------- ----------- ----------
1           a                    30          2010-01-03
2           b                    40          2010-01-04
3           c                    40          2010-01-04
4           d                    NULL        NULL

如果可能的话,如果有人帮助我构建查询,我将不胜感激。我正在使用 SQL Server 2008。

4

7 回答 7

5

检查这个SQL FIDDLE 演示


with CTE_test
as 
(
    select int_id,
       max(date) MaxDate
    from test 
    where date<='2010-01-04 00:00:00:000'
    group by int_id
)
select A.int_id, A.[Value], A.[Date]
from test A
    inner join CTE_test B
       on a.int_id=b.int_id 
          and a.date = b.Maxdate
union all
select int_id, null, null 
from test 
where int_id not in (select int_id from CTE_test)
于 2013-03-28T17:13:10.320 回答
2

(更新)尝试:

with cte as
(select m.*,
        max(date) over (partition by ind_id) max_date,
        max(case when date <= @date then date end) over 
           (partition by ind_id) max_acc_date
 from myTable m)
select ind_id,
       name,
       case when max_acc_date is null then null else value end value, 
       max_acc_date date
from cte c
where date = coalesce(max_acc_date, max_date)

(这里的 SQLFiddle )

于 2013-03-28T17:01:14.023 回答
1

你可以使用类似的东西:

declare @date date = '2010-01-04'

;with ids as
(
  select distinct ind_id
  from myTable
)
,ranks as
(
  select *
    , ranking = row_number() over (partition by ind_id order by date desc)
  from myTable
  where date <= @date
)
select ids.ind_id
  , ranks.value
  , ranks.date
from ids
  left join ranks on ids.ind_id = ranks.ind_id and ranks.ranking = 1

SQL Fiddle 与演示

理想情况下,您不会使用该DISTINCT语句来获取ind_id要包含的值,但我在这种情况下使用它来获得您需要的结果。

此外,此类查询的标准免责声明;如果您有重复的数据,您应该考虑在ORDER BY或 使用RANK而不是ROW_NUMBER.

OP 更新后编辑

只需将新列添加到现有查询中:

with ids as
(
  select distinct ind_id, name
  from myTable
)
,ranks as
(
  select *
    , ranking = row_number() over (partition by ind_id order by date desc)
  from myTable
  where date <= @date
)
select ids.ind_id
  , ids.name
  , ranks.value
  , ranks.date
from ids
  left join ranks on ids.ind_id = ranks.ind_id and ranks.ranking = 1

SQL Fiddle 与演示

与前一个一样,如果可用,最好通过加入常设数据表来获取ind_id/信息。name

于 2013-03-28T16:50:15.277 回答
1

这是一个返回您正在查找的结果的查询:

SELECT
    t1.ind_id
,   CASE WHEN t1.date <= '2010-01-04' THEN t1.value ELSE null END
FROM test t1
WHERE t1.date=COALESCE(
    (SELECT MAX(DATE)
    FROM test t2
    WHERE t2.ind_id=t1.ind_id AND t2.date <= '2010-01-04')
,   t1.date)

这个想法是在相关查询中选择一行,使其 ID 与当前行的 ID 匹配,并且日期是您的目标日期之前的最高日期'2010-01-04'

当这样的行不存在时,返回当前行的日期。此日期需要替换为null; 这就是CASE顶部的声明正在做的事情。

这是关于 sqlfiddle 的演示

于 2013-03-28T17:08:10.663 回答
1

将选项与EXISTS运算符一起使用

DECLARE @date date = '20100104'
SELECT ind_id,
       CASE WHEN date <= @date THEN value END AS value,
       CASE WHEN date <= @date THEN date END AS date
FROM dbo.test57 t
WHERE EXISTS (
              SELECT 1
              FROM dbo.test57 t2
              WHERE t.ind_id = t2.ind_id AND t2.date <= @date
              HAVING ISNULL(MAX(t2.date), t.date) = t.date
              )

SQLFiddle上的演示

于 2013-03-28T17:24:17.240 回答
1

尝试

DECLARE @date DATETIME;

SET @date = '2010-01-04';

WITH temp1 AS
(
SELECT t.ind_id
, t.name
, CASE WHEN t.date <= @date THEN t.value ELSE NULL END AS value
, CASE WHEN t.date <= @date THEN t.date ELSE NULL END AS date
FROM test1 AS t
),

temp AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY ind_id ORDER BY t.date DESC) AS rn
FROM temp1 AS t
WHERE t.date <= @date OR t.date IS NULL
)

SELECT *
FROM temp AS t
WHERE rn = 1
于 2013-03-28T17:49:33.093 回答
0

这不是确切的答案,但会给你这个概念,因为我只是在没有任何测试的情况下快速写下来。

use
go 
if
(Select value from table where col=@col1) is not null
--you code to get the match value
else if 
(Select LOWER(Date) from table ) is not null
-- your query to get the nerst dtae record
else
--you query withh null value
end
于 2013-03-28T16:55:20.927 回答