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

cara membuat data Anda horizontal

SQL Fiddle

Pengaturan Skema MS SQL Server 2017 :

create table MyTable(Name varchar(max),BStatus varchar(max),Bank varchar(max))
insert into MyTable (Name,BStatus,Bank)values('Thung','Active', 'ABC Bank')
insert into MyTable (Name,BStatus,Bank)values('Thung','Hold', 'ABC Bank')

Kueri 1 :

with CTE AS (select *,
(CASE WHEN BStatus='Active' THEN BStatus  END) AS Status1,
(CASE WHEN BStatus = 'Hold' THEN BStatus END) AS Status2,
(CASE WHEN Bank='ABC Bank' THEN Bank END) AS Bank1,
(CASE WHEN Bank='ABC Bank' THEN Bank END) AS Bank2,
ROW_NUMBER() OVER (PARTITION BY BStatus,Bank Order By Name) as rn
from MyTable
group by Name,BStatus,Bank              )

select c.Name
,max(c.Status1) AS Status1
,max(c.Status2) AS Status2
,max(c.Bank1) AS Bank1
,max(c.Bank2) AS Bank2
from cte c
where rn=1
group by c.Name,c.Bank

Hasil :

|  Name | Status1 | Status2 |    Bank1 |    Bank2 |
|-------|---------|---------|----------|----------|
| Thung |  Active |    Hold | ABC Bank | ABC Bank |



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Script Cepat yang Mengembalikan Semua Properti dari SERVERPROPERTY() di SQL Server 2017/2019

  2. SQL Pilih Ulang Tahun Mendatang

  3. Tips Cepat untuk Memperbaiki dan Memulihkan Database SQL Tanpa Cadangan

  4. Menghubungkan aplikasi inti ASP.NET Anda ke instance lokal SQLServer

  5. Waktu Query SQL Server habis tergantung pada Where Clause