Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

मैं एकाधिक पंक्तियों से डेटा को एक में कैसे जोड़ूं?

PIVOT का उपयोग करके आप निम्न कार्य कर सकते हैं

With SampleData AS 
(
SELECT 'Team1' as Team , '[email protected]' as email, 'A' as Groups
UNION SELECT 'Team1' as Team , '[email protected]' as email, 'B' as Groups
UNION SELECT 'Team1' as Team , '[email protected]' as email, 'C' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'A' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'B' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'C' as Groups
)
SELECT Team, A, B,C FROM 
(SELECT * FROM SampleData) source
PIVOT
(MAX(email) FOR Groups IN ([A], [B], [C]) )as pvt

उत्पादन करता है

Team  A                B                C
----- ---------------- ---------------- ----------------
Team1 [email protected] [email protected] [email protected]
Team2 [email protected] [email protected] [email protected]

एक कार्यशील Data.SE उदाहरण देखें

एक डीबी में जो पिवोट का समर्थन नहीं करता है, इसके बजाय आप अपनी तालिका में एकाधिक शामिल हो सकते हैं। हालाँकि आप वैसे भी चाहते हैं, क्योंकि जैसा कि GBN ने बताया है, क्योंकि हम एक समुच्चय का उपयोग नहीं कर रहे हैं।

With SampleData AS 
(
SELECT 'Team1' as Team , '[email protected]' as email, 'A' as Groups
UNION SELECT 'Team1' as Team , '[email protected]' as email, 'B' as Groups
UNION SELECT 'Team1' as Team , '[email protected]' as email, 'C' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'A' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'B' as Groups
UNION SELECT 'Team2' as Team , '[email protected]' as email, 'C' as Groups
)

SELECT 
    source.Team,
    A.email,
    B.email,
    C.email
FROM 
    (SELECT DISTINCT TEAM From SampleData) source
    LEFT JOIN SampleData A 
    ON source.Team = A.Team
     AND A.GROUPS = 'A'
    LEFT JOIN SampleData B 
    ON source.Team = B.Team
    AND B.GROUPS = 'B'
    LEFT JOIN SampleData C 
    ON source.Team = C.Team
    AND C.GROUPS = 'C'

एक कार्यशील Data.SE उदाहरण देखें।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. पहली पंक्ति में कैसे जुड़ें

  2. SQL सर्वर 2005 में VARBINARY फ़ील्ड का आकार

  3. तीन आसान SQL सर्वर प्रदर्शन जीतता है

  4. अनुक्रम बनाम पहचान

  5. SQL सर्वर (T-SQL) में डेटाबेस मेल प्रोफ़ाइल को अपडेट करते समय "प्रोफ़ाइल नाम मान्य नहीं है" को कैसे ठीक करें