How to convert row data to column display in Sql Server
This article mainly explains "how to convert row data to column display in Sql Server". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve row data to column display in Sql Server"!
Scenario: a column value of row data wants to be displayed as a field column
1. The effect is as follows
2. Solution
Use pivot to transfer rows and columns, and combine grouping
3. The code is as follows: select * from (select DeptName,InputCode from FWD_Department group by DeptName,InputCode) as apivot (max (InputCode) for DeptName in ([follow-up Center], [whole Hospital], [Family delivery Room], [Gynecology 2], Gynecology 1)) piv
Create a table:
USE [test] GO/* Object: Table [dbo]. [FWD_Department] Script Date: 14:50:39 on 2022-3-11 * / SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo]. [FWD_Department] ([id] [int] IDENTITY (1) NOT NULL, [DeptName] [varchar] (50) NOT NULL, [InputCode] [nchar] (10) NULL CONSTRAINT [PK_one] PRIMARY KEY CLUSTERED ([DeptName] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] GOSET ANSI_PADDING OFFGO
Table record add:
Insert into one (DeptName,InputCode) values ('follow-up center', 'SFZX') insert into one (DeptName,InputCode) values (' hospital', 'QY') insert into one (DeptName,InputCode) values (' family delivery room', 'JTHCF') insert into one (DeptName,InputCode) values (' gynaecology II', 'FKE') insert into one (DeptName,InputCode) values (' gynaecology one', 'FKY') I believe you have a deeper understanding of "how to convert row data to column display in Sql Server", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!