选择大小写以在列中查找相同的数据

时间:2017-04-19 15:25:08

标签: tsql

我有这需要逻辑条件,希望有人可以帮我编码。

如果是personid> 1和ipeds列的值相同,然后报告值else报告' multiple'

(SELECT
  P.PersonId, CASE WHEN COUNT(P.PersonId) > 1 THEN 'Multiple' ELSE MIN(I.Description) END AS IPEDS_Ethnicity
FROM
  PersonEthnicity AS P 
INNER JOIN
  IpedsFederalCategory AS I ON P.IpedsFederalCategoryId = I.IpedsFederalCategoryId
GROUP BY P.PersonId) AS QS ON PEOPLE.PersonId = QS.PersonId

我对此进行了编码,但是' Multiple'即使ipeds列不相同,当personid中的记录重复时也存在条件。

{{1}}

1 个答案:

答案 0 :(得分:0)

如果你想要多个'要仅在iped不同时显示,您应该准备一个查询以消除重复。就在这时,你可以使用你的逻辑。请查看以下示例:

create table #t (PersonId int, ipeds int)

INSERT INTO #t VALUES (408, 3), (408, 3), (409, 1), (500, 3), (500, 4)

;WITH dt AS (
    SELECT DISTINCT PersonId, ipeds
    FROM #t
)
SELECT
  P.PersonId, CASE WHEN COUNT(P.PersonId) > 1 
                    THEN 'Multiple' 
                    ELSE CAST(MIN(P.ipeds) as varchar(5)) END AS IPEDS_Ethnicity
FROM
  dt AS P 
GROUP BY P.PersonId

结果如下:
408 3
409 1
500多个