我有以下查询
SELECT distinct DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0) as Date
FROM [Vente A]
union
SELECT distinct DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Vente B]
UNION
SELECT distinct DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Budget B]
UNION
SELECT distinct DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Budget A]
我需要选择Vente A和Vente B的日期 然后选择预算A和预算B中的选项,其中日期小于Vente A和Vente B的最大日期
我该怎么做?
答案 0 :(得分:1)
我假设你想要这个:从联合od Vente A和B中选择最大日期。然后显示预算A和B联合的所有行,其中数据小于上面选择的最大日期。
SELECT BudgetDate
FROM (
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0) AS BudgetDate
FROM [Budget B]
UNION
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Budget A]
) BudgetAB
WHERE BudgetDate < (
SELECT MAX(DATE)
FROM (
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0) AS DATE
FROM [Vente A]
UNION
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Vente B]
) AS VenteAB
)
或者,像这样
;WITH BudgetAB
AS (
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0) AS BudgetDate
FROM [Budget B]
UNION
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Budget A]
)
SELECT BudgetDate
FROM BudgetAB
WHERE BudgetDate < (
SELECT MAX(DATE)
FROM (
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0) AS DATE
FROM [Vente A]
UNION
SELECT DISTINCT DATEADD(hour, DATEDIFF(hour, 0, [Date Time]), 0)
FROM [Vente B]
) VenteAB
)