2017-08-16 18 views
9

我有一個表格,其中一個字段是日期字段。從表格中選擇日期到兩列

我被要求編寫一個查詢,返回列A中不同日期(有序)的列表,然後有另一列,比如說B列中的日期在列B中的日期是小於列A

MyDateField 

    2017-01-01 
    2017-01-01 
    2017-01-01 
    2017-01-02 
    2017-01-02 
    2017-01-03 
    2017-01-04 
    2017-01-05 
    2017-01-05 
    2017-01-05 

要求回答

2017-01-05  2017-01-04 
    2017-01-04  2017-01-03 
    2017-01-03  2017-01-02 
    2017-01-02  2017-01-01 
    2017-01-01  
+0

這是否B柱也來自他的同桌,或者只是一個'date_column''1 day''就可以做到嗎? –

+0

它來自同一張表 – mHelpMe

回答

7

如果您使用的SQL服務器2012+,那麼你可以使用LAG()獲取最後最大的日期從表:

SELECT t.date, 
     LAG(t.date,1) OVER(ORDER BY t.date) as last_biggest_date 
FROM (SELECT DISTINCT s.dateField FROM YourTable s) t 
+1

只是注意到這是SQL Server 2012+,儘管他沒有發佈他的DBMS。 – justiceorjustus

+0

@justiceorjustus你是正確的,編輯。 – sagi

+1

這是明智的! – mHelpMe

1

您可以使用子查詢適用於第二列返回較小的日期:

select distinct t1.MyDateField, x.MyDateField 
from MyTable t1 
outer apply (select max(MyDateField) MyDateField 
      from MyTable t2 
      where t1.MyDateField> t2.MyDateField) x 
+0

只是一個頭......上面的'TOP 1'不能保證是你想要的(或者每次都是相同的),而不必在同一個子查詢中指定一個ORDER BY。 – scsimon

+0

你說的對,我用'MAX()'切換'TOP 1',所以它總是返回正確的日期 – Mat

2

您可以在其他事情的CTE做到這一點。這會得到不同日期的列表,然後使用自連接。

with cte as(
    select distinct 
     MyDateField 
    from 
     YourTable) 

select 
    c.MyDateField 
    ,max(c2.MyDateField) as MaxDate 
from 
    cte c 
left join cte c2 on c2.MyDateField < c.MyDateField 
group by 
    c.MyDateField 
order by 
    c.MyDateField 

還是一個沒有CTE

--in this case DISTINCT isn't really needed, but left in case there are other columns 
select distinct 
    c.MyDateField 
    ,max(c2.MyDateField) as MaxDate 
from 
    myTable c 
left join myTable c2 on c2.MyDateField < c.MyDateField 
group by 
    c.MyDateField 
order by 
    c.MyDateField 
+0

我不知道M $ -SQL CTE語法,但是這應該可以工作,即使沒有CTE ... –

+0

你是正確@UsagiMiyamoto,我發現很多人閱讀起來更容易。 – scsimon

0
SELECT d, LAG(d) OVER (ORDER BY d) AS d2 
    FROM (
      SELECT DISTINCT d 
       FROM (VALUES ('2017-01-01'), 
          ('2017-01-01'), 
          ('2017-01-01'), 
          ('2017-01-02'), 
          ('2017-01-02'), 
          ('2017-01-03'), 
          ('2017-01-04'), 
          ('2017-01-05'), 
          ('2017-01-05'), 
          ('2017-01-05')) AS dates(d) 
     ) AS d(d) 
ORDER BY d DESC; 

輸出簡單的自聯接:

d   d2 
---------- ---------- 
2017-01-05 2017-01-04 
2017-01-04 2017-01-03 
2017-01-03 2017-01-02 
2017-01-02 2017-01-01 
2017-01-01 NULL 
0
with CTE as(
select 
ROW_NUMBER() over (order by MyDateField) 'RN', 
MyDateField from TempTable 
group by MyDateField) 
select t2.MyDateField,t1.MyDateField from CTE t1 
right join CTE t2 
on t1.RN=t2.RN-1 
order by t2.MyDateField desc