如何操纵值并在 SQL 上获得新结果?

How to manipulate values and get a new result on in SQL?

我有这个查询:

SELECT 
    c.ID, c.Firstname, c.lastname, c.BDaY, c.gender, 
    cp.code, cp.Citizenship, r.race, e.ethnicity 
FROM 
    Client AS C (nolock) 
JOIN
    Citizenship AS cp (nolock) ON c.ID = cp.client_ID
JOIN
    Race AS r (nolock) ON c.ID = R.Client_ID 
JOIN 
    Ethnicity AS E (nolock) ON E.Client_ID = c.ID

此查询将 return 一些客户的姓名重复,因为他们具有不同的种族和族裔。

示例:

    ID |FirstName|Lastname|  BDay    | gender | code |citizenship|    race    |    ethnicity 
    1   Pedram    Salamati 01-20-1998    M      1     US citizen   Middle-east     Spanish
    1   Pedram    Salamati 01-20-1998    M      1     US Citizen   Middle-east     unknown
    1   Pedram    Salamati 01-20-1998    M      1     US Citizen   Middle-east     Brazilian
    2   Jesse     Albert   03-05-1982    F      1     US Citizen   African         not Spanish
    2   Jesse     Albert   03-05-1982    F      1     US Citizen   American        not Spanish

我想知道是否有任何方法可以说明种族是否不等于种族应该是多种族的,如果种族不等于彼此对于相同的 ID 选择最后更新的那个。

PS.Ethnicity 有时间戳,我可以使用 Max(e.LastUpdate)

我认为也许子查询可以提供帮助!

任何帮助或想法将不胜感激!

这里有一些测试数据来模拟你的环境,以后你应该把涉及的table和测试数据分开。包括 DML 语句也是适当且有用的,因此人们可以在回答之前尝试他们的解决方案。

DECLARE @Client AS TABLE (ID INT, Firstname VARCHAR(25), LastName VARCHAR(25), BDay DATE, Gender CHAR(1))
INSERT INTO @Client VALUES (1,'Pedram','Salamati','01-20-1998','M')
,(2,'Jesse','Albert','03-05-1982','F')
DECLARE @Citizenship AS TABLE (Client_ID INT, Code INT, Citizenship VARCHAR(100))
INSERT INTO @Citizenship VALUES (1,1,'US citizen'),(2,1,'US citizen')
DECLARE @Ethnicity AS TABLE (Client_ID INT, Ethnicity VARCHAR(50))
INSERT INTO @Ethnicity VALUES (1,'Spanish'),(1,'unknown'),(1,'Brazilian'),(2,'not Spanish')
DECLARE @Race AS TABLE (Client_Id INT, Race VARCHAR(50), LastUpdate DATETIME)
INSERT INTO @Race VALUES (1,'Middle-east',GETDATE()),(2,'African',GETDATE()),(2,'American',GETDATE() -1)

使用这些变量,您可以执行以下操作,当然有不止一种方式,这只是我选择的一种方式,原因如下:

;WITH cteEthnicity AS (
    SELECT
       e.Client_ID
       ,CASE WHEN COUNT(DISTINCT e.Ethnicity) > 1 THEN 'Multiracial' ELSE MIN(e.Ethnicity) END as Ethnicity
    FROM
       @Ethnicity e
    GROUP BY
       e.Client_ID
)

, cteRace AS (
    SELECT
       r.Client_Id
       ,r.Race
       ,ROW_NUMBER() OVER (PARTITION BY r.Client_Id ORDER BY r.LastUpdate DESC) as RowNumber
    FROM
       @Race r
)

SELECT
    c.ID
    ,c.Firstname
    ,c.lastname
    ,c.BDaY
    ,c.gender
    ,cp.code
    ,cp.Citizenship
    ,r.race
    ,e.ethnicity
From
    @Client AS C --(nolock) 
    Join @Citizenship as cp --(nolock)
    on  c.ID = cp.client_ID
    Join cteRace as r --(nolock)
    ON c.ID = R.Client_ID
    AND r.RowNumber = 1
    Join cteEthnicity as E --(nolock)
    ON E.Client_ID = c.ID

您显示了 2 个问题 1 与种族和 1 与种族

  • 对于种族:您想使用聚合来确定分配哪个种族。这也可以用 window 函数来完成,但我在这里写的方式它会解释即使在种族 table.

  • 中也存在重复项
  • 对于 Race:您只需要由客户端分区的最新行,您可以使用 ROW_NUMBER() 函数生成它,然后 select 在它等于 1 的地方加入语句

您没有指出但无论如何在某些国家/地区可能存在的第三个问题是双重国籍。在那种情况下,您可以使用类似于 Race 的方法。

请注意,即使使用了通用 Table 表达式 [CTE],您实际上也可以将它们嵌套为子select。