如何在SQL中操作值并获得新结果?

时间:2016-09-20 22:20:15

标签: sql sql-server subquery subquery-factoring

我有这个问题:

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

此查询将返回一些重复的客户名称,因为它们具有不同的种族和民族。

示例:

    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)

我想也许子查询可以帮助!

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

1 个答案:

答案 0 :(得分:1)

以下是一些模拟您的环境的测试数据,您应该将所涉及的表和测试数据分开。包括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个种族

  • 对于种族:您希望使用聚合来确定要分配的种族。这也可以通过窗口函数来完成,但是我在这里编写它的方式,即使在种族表中也会存在重复项。

  • 对于Race:您只需要客户端分区的最新行,您可以使用ROW_NUMBER()函数生成该行,然后在连接语句中选择它等于1的位置

你没有指出的第三个问题,但无论如何在一些国家都是可能的是双重公民身份。在这种情况下,您可以使用类似于Race的方法。

请注意,即使使用了通用表格式[CTE],您也可以将它们嵌套为subselect。