【问题标题】:Using UNION ALL on 2 tables to obtain specific results在 2 个表上使用 UNION ALL 以获得特定结果
【发布时间】:2021-12-21 13:44:29
【问题描述】:

我正在尝试查询唯一结果,但遇到 Query 2 问题,因为它从我的 select 语句中提取了任何 ItemNumber 的前 1 个结果,而不是我想要的 ItemNumber。我想显示来自 Query 1 的所有唯一结果,并且我只想显示来自 Query 2 的 1 个唯一结果,其中 NULL 位置被优先考虑。 (每个 ItemNumber 关联 1 个“Y”SpecialItem。我已经模拟了我的问题的一个小示例,以防止问题过于复杂。

正在使用的表:

CREATE TABLE QA_TESTING (
    ItemNumber varchar(255),
    ItemName varchar(255),
    Location varchar(255)
)

正在使用的数据:

Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('333', 'Apple', 'USA')
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('501', 'Apple', NULL)
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('501', 'Apple', NULL)
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('501', 'Apple', 'USA')
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('501', 'Apple', NULL)
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('405', 'Apple', 'USA')
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('405', 'Orange', 'USA')
Insert into QA_Testing (ItemNumber, ItemName, Location) Values ('501', 'Apple', 'USA')

我的观点:

    IF EXISTS (SELECT TABLE_NAME 
           FROM   INFORMATION_SCHEMA.VIEWS 
           WHERE  TABLE_NAME = N'QA_TESTING_VW')
        DROP VIEW QA_TESTING_VW
    GO
    
    CREATE VIEW dbo.QA_TESTING_VW
    AS
    (
    
    (
    --Query 1
    Select DISTINCT QAT.ItemNumber, QAT.ItemName, Null AS SpecialItem, QAT.Location
    From QA_Testing QAT
    Where ItemName = 'Apple'
    )
    UNION ALL
    (
    --I Want to prioritize to show NULL over a non-null location in second query. If there is a null, show the null, otherwise, show the populated location. Only 1 row should be returned.
    --Query 2 
    Select DISTINCT TOP 1 QAT.ItemNumber, QAT.ItemName, 'Y' AS SpecialItem, QAT.Location
    From QA_Testing QAT
    Where ItemName = 'Apple'
    --ORDER BY Location ASC    --ORDER BY TAKES TOO LONG
    )
    
    ) 
    GO

我的选择声明:

--This has to stay in a simple format like so, with no additional unions, joins, etc.
Select * from QA_TESTING_VW where ItemNumber in ('501','830')

结果:

预期结果:

【问题讨论】:

  • 工会的两半不只是在做同样的事情吗?他们有相同的WHERE 过滤器
  • @Charlieface 这只是一个模拟示例,两个 where 子句有很大不同。我只是不想用大量代码使任何人过于复杂。

标签: sql sql-server select view union-all


【解决方案1】:

有评论“ORDER BY TAKES TOO LONG”

这就是为什么我不太确定。 作为思考的燃料:

Select distinct QAT.ItemNumber, QAT.ItemName, O.SpecialItem, QAT.[Location]
From
    (
        Select QAT.ItemNumber
            ,QAT.ItemName
            ,QAT.[Location]
            ,row_number() over(partition by QAT.ItemNumber, QAT.ItemName order by QAT.[Location]) as LocationPriority
        From QA_Testing QAT
        Where ItemName = 'Apple'
    ) QAT
    left join
    (
        select 'Y' AS SpecialItem
        union all
        select null
    ) O
    on QAT.LocationPriority = 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多