Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID=1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
结果:2017-02-10(yyyy-MM-dd)
查询2:
Select MAX(SaleEndDate) as MaxSaleDate
from Sales
where LessonID=1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleEndDate) < 0)
结果:2017-01-02(yyyy-MM-dd)
我想只用1个查询显示2个结果。 请帮我!感谢每一个人!
答案 0 :(得分:2)
只需选择这两个:
Select MIN(SaleStartDate) as MinSaleDate,
MAX(SaleEndDate) as MaxSaleDate
from Sales
where LessonID=1 and (DATEDIFF(day,
CONVERT (char(10),
GETDATE(),
126),
Sales.SaleStartDate) > 0)
答案 1 :(得分:1)
如果您需要在一行中同时获得两个结果:
Select
(
Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID=1
and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
),
(
Select MAX(SaleEndDate) as MaxSaleDate
from Sales
where LessonID=1
and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleEndDate) < 0)
)
如果您需要在不同行中使用UNION:
Select
(
Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID=1
and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
) UNION
(
Select MAX(SaleEndDate) as MaxSaleDate
from Sales
where LessonID=1
and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleEndDate) < 0)
)
答案 2 :(得分:1)
根据您的具体情况,您只需使用CROSS JOIN
即可获得结果
SELECT MinSaleDate, MaxSaleDate
FROM
(
Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID = 1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
),
(
Select MAX(SaleEndDate) as MaxSaleDate
from Sales
where LessonID = 1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleEndDate) < 0)
);
对于一般情况,您可以使用:
SELECT
MIN(CASE WHEN (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0
THEN SaleStartDate END) AS MinSaleDate,
MAX(CASE WHEN (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleEndDate) < 0
THEN SaleEndDate END) AS MaxSaleDate
from Sales
where LessonID = 1;