2016-05-31 71 views
3

我有一個包含客戶的表(該表的目標是能夠添加沒有DB-Update的字段)。該表是這樣的:如何編寫查詢?

CustId  Property  PropertyValue 

1   Name   Smith 
1   Email  [email protected] 
2   Name   Donalds 
2   Email  [email protected] 
3   Name   john 

(客戶3對錶中的「電子郵件」不可進入)

預期結果:我想每個客戶一行(郵件),如果客戶沒有電子郵件,仍顯示NULL一行。

CustId  Property  PropertyValue 

1   Email  [email protected] 
2   Email  [email protected] 
3   Email  NULL 

有人解決嗎?

+4

你能分享到目前爲止你已經試過SQL? –

回答

4
DECLARE @t TABLE (
    CustId INT, 
    Property VARCHAR(50), 
    PropertyValue VARCHAR(50) 
) 
INSERT INTO @t (CustId, Property, PropertyValue) 
VALUES 
    (1, 'Name', 'Smith'), 
    (1, 'Email', '[email protected]'), 
    (2, 'Name', 'Donalds'), 
    (2, 'Email', '[email protected]'), 
    (3, 'Name', 'john') 

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

嗯,這不是他要求的 – sagi

+0

@sagi是的,從我身邊發生一個小錯誤:)已更新 – Devart

+0

更好的解決方案,然後我們所有人雖然:) – sagi

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 
+0

您的第一個查詢會導致以下錯誤:'無效的列名'屬性'。' 您的第二個查詢按預期工作。 – HappyCoding

+1

@HappyCoding謝謝,錯字。與't'混合's' – sagi

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 ║ [email protected] ║ 
║  2 ║ Email ║ [email protected] ║ 
║  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  ║ [email protected] ║ 
║  2 ║ Donalds  ║ [email protected] ║ 
║  3 ║ john   ║ NULL    ║ 
╚════════╩══════════════╩═══════════════════╝ 
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 

輸出應該是你在你的問題發佈過。

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'; 
+1

這是什麼添加到其他答案? – OrangeDog

+0

對不起,延誤了。此查詢過濾具有屬性值爲'Email'的行,並對cust ID進行分組。因此,如果任何custid沒有「電子郵件」作爲屬性值,它將返回爲空。 – PriVictor