如何将 2 SQL 个查询合并为一个
How to Combine 2 SQL queries into one
我有一个 table 如下图所示:
查询 1:
Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID=1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
Result: 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)
Result: 2017-01-02 (yyyy-MM-dd)
我想通过 1 个查询显示 2 个结果。
请帮我!谢谢大家!
像这样把两者都放在 select 上:
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)
如果您需要将两个结果放在一行中:
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)
)
对于您的具体情况,您可以使用 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;
我有一个 table 如下图所示:
Select MIN(SaleStartDate) as MinSaleDate
from Sales
where LessonID=1 and (DATEDIFF(day, CONVERT (char(10), GETDATE(), 126), Sales.SaleStartDate) > 0)
Result: 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)
Result: 2017-01-02 (yyyy-MM-dd)
我想通过 1 个查询显示 2 个结果。 请帮我!谢谢大家!
像这样把两者都放在 select 上:
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)
如果您需要将两个结果放在一行中:
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)
)
对于您的具体情况,您可以使用 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;