我在SQL Server中有一个表,其中包含两列Eff和Week,如下所示,
Eff Week
2014-01-11 2014-01-11
2014-01-11 2014-01-18
2014-01-11 2014-01-25
2014-01-25 2014-01-25
2014-01-11 2014-02-01
2014-01-25 2014-02-01
2014-01-11 2014-02-08
2014-01-25 2014-02-08
2014-01-11 2014-02-15
2014-01-25 2014-02-15
2014-01-11 2014-02-22
2014-01-25 2014-02-22
2014-01-11 2014-03-01
2014-01-25 2014-03-01
2014-02-28 2014-03-01
2014-01-11 2014-03-08
2014-01-25 2014-03-08
2014-02-28 2014-03-08
2014-01-11 2014-03-15
2014-01-25 2014-03-15
2014-02-28 2014-03-15
我想从这个表中创建一个忽略某些行的查询,并且只放出一个包含周日期的表,但是Eff日期可能会重复。基本上它应该选择一周的最大或最新的生效日期。新表应如下所示,
Eff Week
2014-01-11 2014-01-11
2014-01-11 2014-01-18
2014-01-25 2014-01-25
2014-01-25 2014-02-01
2014-01-25 2014-02-08
2014-01-25 2014-02-15
2014-01-25 2014-02-22
2014-02-28 2014-03-01
2014-02-28 2014-03-08
2014-02-28 2014-03-15
任何指针?
答案 0 :(得分:3)
你的意思是The maximum effective date for each week
吗?
SELECT
week,
MAX(eff) AS max_eff
FROM
yourTable
GROUP BY
week
ORDER BY
week
GROUP BY
获取具有相同week
的所有行,并将它们处理为单行输出。 (每周一个输出行,无论每周有多少输入行。)
然后,MAX(eff)
允许您在每个组中选择eff
的最高值。
ORDER BY
只是对输出进行排序,它不会以任何方式影响结果。
答案 1 :(得分:0)
试试这个:
SELECT MAX(Eff) Eff,Week FROM TABLE1 GROUP BY WEEK;
答案 2 :(得分:0)
这是你需要的吗?
SELECT
MAX(eff) AS MaxEff,
MIN(eff) AS MinEff,
week
FROM
TableDates
GROUP BY
week
ORDER BY
week ASC;
答案 3 :(得分:0)
试试这个,输出是一样的。
select * from
(select *,
ROW_NUMBER()over(partition by datepart(wk,weeks)order by Eff desc)rn from @t
)t4
where rn=1