Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert row data to column display in Sql Server

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report