SQL Server 创建一个伪列,以便在特定ID的一个列值存在于另一个列值中时显示“是/否”

d4so4syb  于 2022-11-21  发布在  其他
关注(0)|答案(3)|浏览(131)

我有一个具有以下结构的表:

create table test_18Nov ( account_id nvarchar(12)
                        , account_name nvarchar(25)
                        , zip_legacy_file nvarchar(5)
                        , Region_legacy_file nvarchar(30)
                        , zip_new_source nvarchar(5)
                        , Region_new_source nvarchar(30)
                        )

INSERT INTO test_18Nov VALUES ('S1018', 'John Smith',       '32221', 'R087-Jacksonville',   '33803', 'R026-Lakeland')
INSERT INTO test_18Nov VALUES ('S1018', 'John Smith',       '33606', 'R011-Tampa',          '32220', 'R087-Jacksonville')
INSERT INTO test_18Nov VALUES ('S1018', 'John Smith',       '33803', 'R026-Lakeland',       '33606', 'R011-Tampa')
INSERT INTO test_18Nov VALUES ('AC054', 'David Thompson',   '33606', 'R011-Tampa',          '32205', 'R087-Jacksonville')
INSERT INTO test_18Nov VALUES ('AC054', 'David Thompson',   '33870', 'R058-Sebring',        '33606', 'R011-Tampa')
INSERT INTO test_18Nov VALUES ('AC054', 'David Thompson',   '33610', 'R011-Tampa',          '33870', 'R058-Sebring')
INSERT INTO test_18Nov VALUES ('AC077', 'Stacey Leigh',     '34950', 'R043-Fort Pierce',    '34982', 'R043-Fort Pierce')
INSERT INTO test_18Nov VALUES ('AC077', 'Stacey Leigh',     '33610', 'R011-Tampa',          '34950', 'R043-Fort Pierce')

我必须为所有具有“是”或“否”的行生成一个伪列。如果原区域(或区域)存在于新源区域中(或区域),则虚拟字段会显示“否”。如果账户的新来源区域(或区域)不存在于旧区域中(或区域),则该帐户将被视为移动,并且伪列将显示“是”。在上述数据集中,AC 054是唯一应将伪列值设置为“是”的帐户标识。因为R 087-Jackson维尔(一个新的源区域)不在传统区域列表中。
预期输出应为:

account_id  | account_name      |  Region_legacy_file   | Region_new_source     | Will the account move?    |
-------------------------------------------------------------------------------------------------------------
S1018       | John Smith        | R087-Jacksonville     | R026-Lakeland         |       No                  |
-------------------------------------------------------------------------------------------------------------
S1018       | John Smith        | R011-Tampa            | R087-Jacksonville     |       No                  |
-------------------------------------------------------------------------------------------------------------
S1018       | John Smith        | R026-Lakeland         | R011-Tampa            |       No                  |
-------------------------------------------------------------------------------------------------------------
AC054       | David Thompson    | R011-Tampa            | R087-Jacksonville     |       Yes                 |
-------------------------------------------------------------------------------------------------------------
AC054       | David Thompson    | R058-Sebring          | R011-Tampa            |       Yes                 |
-------------------------------------------------------------------------------------------------------------
AC054       | David Thompson    | R011-Tampa            | R058-Sebring          |       Yes                 |
-------------------------------------------------------------------------------------------------------------
AC077       | Stacey Leigh      | R043-Fort Pierce      | R043-Fort Pierce      |       No                  |
-------------------------------------------------------------------------------------------------------------
AC077       | Stacey Leigh      | R011-Tampa            | R043-Fort Pierce      |       No                  |
-------------------------------------------------------------------------------------------------------------

我想使用NOT EXISTS子句,但这将仅返回在帐户标识的原区域列表中未找到新来源区域的行-在这种情况下,这对我没有任何帮助。我能想到的唯一方法是使用CASE WHEN EXISTS,但无法使其工作。如果有任何其它方法,请共享。
编辑:为什么AC 054的伪列为“是”?-帐户标识AC 054有3个新的来源区域,它们是R 087、R 011和R 058。在这3个区域中,有2个区域位于帐户标识AC 054的原区域中,它们是R 011和R 058。因此,R 087不是帐户标识AC 054的原区域列表的一部分,因此伪列的值为“是”。对于其它两个帐户标识,情况并非如此,因为所有新的来源区域都在原区域列表中。

polhcujo

polhcujo1#

我正在检查您的信息,我做了一个例子什么一个子查询CheckLegacy将返回什么应该被移动的帐户,下面的例子:

WITH test_18Nov  AS (
    SELECT * FROM (
    VALUES
        ('S1018', 'John Smith',       '32221', 'R087-Jacksonville',   '33803', 'R026-Lakeland'), 
        ('S1018', 'John Smith',       '33606', 'R011-Tampa',          '32220', 'R087-Jacksonville'), 
        ('S1018', 'John Smith',       '33803', 'R026-Lakeland',       '33606', 'R011-Tampa'), 
        ('AC054', 'David Thompson',   '33606', 'R011-Tampa',          '32205', 'R087-Jacksonville'), 
        ('AC054', 'David Thompson',   '33870', 'R058-Sebring',        '33606', 'R011-Tampa'),
        ('AC054', 'David Thompson',   '33610', 'R011-Tampa',          '33870', 'R058-Sebring'),
        ('AC077', 'Stacey Leigh',     '34950', 'R043-Fort Pierce',    '34982', 'R043-Fort Pierce'),
        ('AC077', 'Stacey Leigh',     '33610', 'R011-Tampa',          '34950', 'R043-Fort Pierce')
    ) AS _ (account_id,account_name, zip_legacy_file,Region_legacy_file,zip_new_source,Region_new_source)
),
--formatting the query for the field I need
IdAndNewLegacy as (
    SELECT account_id, Region_new_source FROM test_18Nov
),
--check if some new legacy region is not in Region_legacy_file
CheckLegacy as (
    SELECT I.account_id, T.account_id as id FROM IdAndNewLegacy as I
    LEFT JOIN test_18Nov as T ON I.account_id = T.account_id and I.Region_new_source = T.Region_legacy_file
    WHERE T.account_id is null
    GROUP BY I.account_id, T.account_id
)
--Query to present the data
SELECT 
    t.*,
    CASE WHEN c.account_id is not null then 'Yes' ELSE 'No' END as [Will the account move?]
FROM 
    test_18Nov  as t
    LEFT JOIN CheckLegacy as c ON T.account_id = C.account_id

结果:

此致

gajydyqb

gajydyqb2#

使用CASE表达式 (Or IIF() 获取每行的YES/NO(使用NOT EXISTS检查)。
然后使用MAX(expression) OVER (PARTITION BY account_id)将所有帐户行指定为YES is ANY of them is yes。

SELECT
  t.*,
  MAX(
    IIF(
      NOT EXISTS (
        SELECT *
          FROM Test_18Nov
         WHERE account_id         = t.account_id
           AND region_legacy_file = t.region_new_source
      ),
      'YES',
      'NO'
    ) 
  ) OVER (PARTITION BY t.account_id)
FROM
  test_18Nov   AS t
ORDER BY
  t.account_id,
  t.region_new_source

演示:https://dbfiddle.uk/rOS1OoFe

qyyhg6bp

qyyhg6bp3#

您可以使用SQL子查询,这意味着对于每一行,您都要执行另一个查询来查找结果,例如:

SELECT name, id, location, (SELECT TOP 1 'exists' FROM location l WHERE l.location = c.location) 
FROM customer c

相关问题