很抱歉标题混乱。请告诉我,是否可以通过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
,则如果没有任何更低的日期,则采用最接近的较低日期,然后使用null返回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。
答案 0 :(得分:4)
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)
答案 1 :(得分: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 here)
答案 2 :(得分: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
理想情况下,您不会使用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
与前一个一样,最好通过加入常设数据表获取ind_id
/ name
信息。
答案 3 :(得分: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
语句正在做的事情。
答案 4 :(得分: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上的演示
答案 5 :(得分: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
答案 6 :(得分: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