我有这个示例表,带有示例数据:
+-------------------+-----------------+------------+------------+
| OriginalBeginDate | OriginalEndDate | Start Date | End Date |
+-------------------+-----------------+------------+------------+
| 2015-06-01 | 2015-06-30 | 2015-08-01 | 2015-08-31 |
| 2015-06-01 | 2015-06-30 | 2015-09-01 | 2015-09-30 |
| 2015-06-01 | 2015-06-30 | 2015-10-01 | 2015-10-31 |
| 2015-06-01 | 2015-06-30 | 2015-11-01 | 2015-11-30 |
| 2015-06-01 | 2015-06-30 | 2015-12-01 | 2015-12-31 |
| 2015-07-01 | 2015-12-31 | 2015-08-01 | 2015-08-31 |
| 2015-07-01 | 2015-12-31 | 2015-09-01 | 2015-09-30 |
| 2015-07-01 | 2015-12-31 | 2015-10-01 | 2015-10-31 |
| 2015-07-01 | 2015-12-31 | 2015-11-01 | 2015-11-30 |
| 2015-07-01 | 2015-12-31 | 2015-12-01 | 2015-12-31 |
+-------------------+-----------------+------------+------------+
如果您注意到,则会有一组5'开始日期和结束日期重复,因为' OriginalBeginDate'和' OriginalEndDate'有两套'。
我需要只有一组最后两列(它们的不同值)。
这是输出需要的样子:
| OriginalBeginDate | OriginalEndDate | Start Date | End Date |
+-------------------+-----------------+------------+------------+
| 2015-06-01 | 2015-06-30 | 2015-08-01 | 2015-08-31 |
| 2015-06-01 | 2015-06-30 | 2015-09-01 | 2015-09-30 |
| 2015-06-01 | 2015-06-30 | 2015-10-01 | 2015-10-31 |
| 2015-06-01 | 2015-06-30 | 2015-11-01 | 2015-11-30 |
| 2015-06-01 | 2015-06-30 | 2015-12-01 | 2015-12-31 |
+-------------------+-----------------+------------+------------+
如果我跑
SELECT DISTINCT * FROM myTable
我得到了整张桌子。如果我跑
SELECT DISTINCT [START DATE], [END DATE] FROM myTable
我不能获得所有4列,我需要。
任何帮助将不胜感激!
答案 0 :(得分:5)
只需添加一些MIN
分组即可获取其他列的最低值,这看起来就像您所追求的那样:
SELECT MIN(OriginalBeginDate) as OriginalBeginDate,
MIN(OriginalEndDate) as OriginalEndDate,
[START DATE],
[END DATE]
FROM myTable
GROUP BY [START DATE], [END DATE]
答案 1 :(得分:4)
您可以尝试根据开始日期和结束日期对数据进行分组
DECLARE @mytable TABLE
(
OriginalBeginDate DATE ,
OriginalEndDate DATE NOT NULL ,
Start_Date DATE NOT NULL ,
End_Date DATE NOT NULL
);
INSERT INTO @mytable(OriginalBeginDate,OriginalEndDate,Start_Date,End_Date)
VALUES ('2015-06-01','2015-06-30','2015-08-01','2015-08-31')
, ('2015-06-01','2015-06-30','2015-09-01','2015-09-30')
, ('2015-06-01','2015-06-30','2015-10-01','2015-10-31')
, ('2015-06-01','2015-06-30','2015-11-01','2015-11-30')
, ('2015-06-01','2015-06-30','2015-12-01','2015-12-31')
, ('2015-07-01','2015-12-31','2015-08-01','2015-08-31')
, ('2015-07-01','2015-12-31','2015-09-01','2015-09-30')
, ('2015-07-01','2015-12-31','2015-10-01','2015-10-31')
, ('2015-07-01','2015-12-31','2015-11-01','2015-11-30')
, ('2015-07-01','2015-12-31','2015-12-01','2015-12-31')
SELECT MIN(OriginalBeginDate), MIN(OriginalEndDate), Start_Date, End_Date
FROM @mytable
GROUP BY Start_Date, End_Date
答案 2 :(得分:0)
您可以通过向查询添加条件来仅返回OriginalBeginDate
值最低的行来完成此操作。
SELECT *
FROM myTable
WHERE NOT EXISTS (SELECT TOP 1 OriginalBeginDate
FROM myTable t2
WHERE t2.[Start Date] = myTable.[Start Date]
AND t2.[End Date] = myTable.[End Date]
AND t2.OriginalBeginDate = myTable.OriginalBeginDate)
答案 3 :(得分:0)
您可以尝试使用:
编辑:在订购之前使用分区......
SELECT tbl.*
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY [Start Date] ORDER BY OriginalBeginDate) AS Inx1
,ROW_NUMBER() OVER(PARTITION BY [End Date] ORDER BY OriginalEndDate) AS Inx2
,*
FROM @myTable
) AS tbl
WHERE tbl.Inx1=1 OR tbl.Inx2=1
答案 4 :(得分:0)
group by min(或max)的问题是可以从不同的行得到min 有了这些数据,它没有任何区别,但也许这会更安全
SELECT tbl.*
FROM
( SELECT *, ROW_NUMBER() OVER(PARTITION BY [Start Date], [End Date]
ORDER BY [OriginalBeginDate], [OriginalEndDate]) AS rn
FROM myTable
) AS tbl
WHERE tbl.rn = 1