如何编写查询?

时间:2016-05-31 13:24:45

标签: sql sql-server

我有一个包含客户的表(此表的目标是能够添加没有DB-Update的字段)。该表如下所示:

CustId     Property     PropertyValue  

1          Name         Smith  
1          Email        smith@gmail.com  
2          Name         Donalds  
2          Email        donalds@gmail.com  
3          Name         john

(客户3在表格中没有“电子邮件”条目)

预期结果:我希望每个客户端获得一行(Mail),如果客户没有电子邮件,则仍然显示一行为NULL。

CustId     Property     PropertyValue  

1          Email        smith@gmail.com  
2          Email        donalds@gmail.com  
3          Email        NULL

有人有解决方案吗?

5 个答案:

答案 0 :(得分:5)

查询1

Select t1.CustId 
     , ISNULL(t2.Property ,'Email') AS Property 
     , t2.PropertyValue
FROM TableName t1 
LEFT JOIN TableName t2 ON t1.CustId = t2.CustId
                      AND t2.Property = 'Email'
WHERE t1.Property = 'Name'

结果集1

╔════════╦══════════╦═══════════════════╗
║ CustId ║ Property ║   PropertyValue   ║
╠════════╬══════════╬═══════════════════╣
║      1 ║ Email    ║ smith@gmail.com   ║
║      2 ║ Email    ║ donalds@gmail.com ║
║      3 ║ Email    ║ NULL              ║
╚════════╩══════════╩═══════════════════╝

查询2

对可读性更高的结果集的另一个查询看起来应该像......

Select t1.CustId 
     , t1.PropertyValue [CustomerName]
     , t2.PropertyValue [CustomerEmail]
FROM TableName t1 
LEFT JOIN TableName t2 ON t1.CustId = t2.CustId
                      AND t2.Property = 'Email'
WHERE t1.Property = 'Name'

结果集2

╔════════╦══════════════╦═══════════════════╗
║ CustId ║ CustomerName ║   CustomerEmail   ║
╠════════╬══════════════╬═══════════════════╣
║      1 ║ Smith        ║ smith@gmail.com   ║
║      2 ║ Donalds      ║ donalds@gmail.com ║
║      3 ║ john         ║ NULL              ║
╚════════╩══════════════╩═══════════════════╝

答案 1 :(得分:4)

DECLARE @t TABLE (
    CustId INT,
    Property VARCHAR(50),
    PropertyValue VARCHAR(50)
)
INSERT INTO @t (CustId, Property, PropertyValue)
VALUES
    (1, 'Name', 'Smith'),
    (1, 'Email', 'smith@gmail.com'),
    (2, 'Name', 'Donalds'),
    (2, 'Email', 'donalds@gmail.com'),
    (3, 'Name', 'john')

SELECT CustId
     , Name = 'Email'
     , Value = MAX(CASE WHEN Property = 'Email' THEN PropertyValue END)
FROM @t
GROUP BY CustId

答案 2 :(得分:3)

您可以使用包含所有可能的ID的派生表来执行此操作,然后只保留原始表格中的电子邮件:

SELECT t.custID,'EMAIL',s.PropertyValue
FROM(SELECT DISTINCT custID
     FROM YourTable) t
LEFT OUTER JOIN YourTable s
 ON(t.custID = s.custID and s.property = 'Email')

也可以使用相关查询来完成:

SELECT DISTINCT t.CustID,'EMAIL',
       (SELECT s.PropertyValue
        FROM YourTable s
        WHERE s.custID = t.custID and s.Property = 'Email')
FROM YourTable t

答案 3 :(得分:1)

使用相同的表进行自联接,通过变量传递属性

DECLARE @prop nvarchar(max) = 'Email'

SELECT DISTINCT c.CustId, @prop as Property, c1.PropertyValue
FROM yourtable c 
LEFT JOIN yourtable c1
    ON c.CustId = c1.CustId and c1.Property = @prop

输出将与您在问题中发布的内容相同。

答案 4 :(得分:1)

SELECT CustId
     , MIN(CASE WHEN Property IS NULL THEN 'Email' ELSE Property END) Property
     , MIN(PropertyValue) PropertyValue
FROM TableName
GROUP BY CustId
HAVING Property = 'Email';