Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Menggunakan PIVOT dan BERGABUNG bersama

WITH T
     AS (SELECT [Order Details].OrderID,
                c.CategoryName,
                c.CategoryID
         FROM   [Order Details]
                INNER JOIN Products p
                  ON p.ProductID = [Order Details].ProductID
                INNER JOIN Categories c
                  ON c.CategoryID = p.CategoryID)
SELECT *
FROM   T PIVOT ( COUNT (CategoryID) FOR CategoryName IN ( 
       [Beverages],
       [Condiments],
       [Confections], 
       [Dairy Products], 
       [Grains/Cereals],
       [Meat/Poultry],
       [Produce],
       [Seafood]) ) AS pvt
ORDER  BY OrderID  



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Urutan Sortir SQL dengan nilai Null terakhir

  2. datetime vs smalldatetime di SQL Server:Apa Bedanya?

  3. Bagaimana cara menambahkan jam ke tanggal saat ini di SQL Server?

  4. DATEDIFF() vs DATEDIFF_BIG() di SQL Server:Apa Bedanya?

  5. Perbedaan Antara Tabel DELETE dan TRUNCATE di SQL Server