SQL Server数据行转列案例实践

SQL Server数据行转列案例实践

数据库某表行转列案例实践

--第一步,创建测试表[StudentsScore]及测试数据

CREATE TABLE [dbo].[StudentsScore]( [Student] varchar NOT NULL, [Subject] varchar NOT NULL, [Score] [int] NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生A', N'中文', 80) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生A', N'数学', 90) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生A', N'英语', 95) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生B', N'中文', 89) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生B', N'数学', 86) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生B', N'英语', 90) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生C', N'中文', 75) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生C', N'数学', 86) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生C', N'英语', 90) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生D', N'中文', 86) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生D', N'数学', 98) INSERT [dbo].[StudentsScore] ([Student], [Subject], [Score]) VALUES (N'学生D', N'英语', 85)

实现效果如下

www.zeeklog.com  - SQL Server数据行转列案例实践

--第二步,用两种方法实现行转列

--方法一:利用sql2005中的新功能 PIVOT(可以将数据值转换为数据列)

select A.,B.[总分],B.[平均分] from (select Student,[中文],[数学],[英语] from (select * from StudentsScore) as PS PIVOT (max(score) for [Subject] in([中文],[数学],[英语]) )as pivotTable) A, (select Student,SUM(score)[总分],cast(AVG(score1.0)as decimal(18,2))[平均分] from StudentsScore group by Student) B where A.Student=B.Student

--方法二:用传统的case方法

select Student, max(case [subject] when '中文' then Score else 0 end) [中文], max(case [subject] when '数学' then Score else 0 end) [数学], max(case [subject] when '英语' then Score else 0 end) [英语], SUM(Score)[总分], cast(AVG(score*1.0)as decimal(18,2))[平均分] from StudentsScore group by Student

Read more

超快速,使用ChatGPT编写回归和分类算法

超快速,使用ChatGPT编写回归和分类算法

本文将使用一些 ChatGPT 提示,这些提示对于数据科学家在工作时非常重要。 微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩 以下是一些示例ChatGPT 提示的列表以及数据科学家的响应。 ChatGPT 提示 为决策树回归算法生成 python 代码。 下面是使用scikit-learn在 Python 中进行决策树回归的示例代码: import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Generate random data rng = np.random.default_rng() x = 5 * rng.random(100) y = np.sin(x) + 0.

By Ne0inhk
力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

993.二叉树的堂兄弟节点 难度:简单 题目: 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。 如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。 只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。 示例: 示例 1: 输入:root = [1,2,3,4], x = 4, y = 3 输出:false

By Ne0inhk
1239.串联字符串的最大长度 关于字符串的回溯算法!

1239.串联字符串的最大长度 关于字符串的回溯算法!

题目: 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串, 如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。 请返回所有可行解 s 中最长长度。 提示: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] 中只含有小写英文字母 示例: 示例 1: 输入:arr = ["un","iq","ue"] 输出:4 解释:所有可能的串联组合是

By Ne0inhk