sqlserver 行列互转实现小结
列转行比较经典,需要的朋友可以参考下。
代码如下:--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂
整理人:中国风(Roy)
日期:2008.06.06
******************************************************************************************************************************************************/
--1、行互列
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
生成静态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]
GO
动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
生成静态:
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78
(2 行受影响)
*/
------------------------------------------------------------------------------------------
go
--加上总成绩(学科平均分)
--2000方法:
动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score]) from Class group by [Student]')--加多一列(学科平均分用avg([Score]))
生成动态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end),
[总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
from
Class
group by [Student]
go
--2005方法:
动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')
生成静态:
select
[Student],[数学],[物理],[英语],[语文],[总成绩]
from
(select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student 数学 物理 英语 语文 总成绩
------- ----------- ----------- ----------- ----------- -----------
李四 77 85 65 65 292
张三 87 90 82 78 337
(2 行受影响)
*/
go
--2、列转行
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go
--2000:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序
生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]
go
--2005:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78
(8 行受影响)
*/

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











インポート手順は次のとおりです。 MDF ファイルを SQL Server のデータ ディレクトリ (通常は C:\Program Files\Microsoft SQL Server\MSSQL\DATA) にコピーします。 SQL Server Management Studio (SSMS) でデータベースを開き、[アタッチ] を選択します。 「追加」ボタンをクリックして、MDF ファイルを選択します。データベース名を確認し、「OK」ボタンをクリックします。

SQL Server データベースに既に存在する同じ名前のオブジェクトについては、次の手順を実行する必要があります。 オブジェクトの種類 (テーブル、ビュー、ストアド プロシージャ) を確認します。 IF NOT EXISTS を使用すると、オブジェクトが空の場合に作成をスキップできます。オブジェクトにデータがある場合は、別の名前を使用するか、構造を変更してください。既存のオブジェクトを削除するには、DROP を使用します (注意してください。バックアップを推奨します)。スキーマの変更をチェックして、削除または名前変更されたオブジェクトへの参照がないことを確認します。

SQL Server のポート番号を表示するには: SSMS を開いてサーバーに接続します。オブジェクト エクスプローラーでサーバー名を見つけ、右クリックして [プロパティ] を選択します。 「接続」タブで、「TCP ポート」フィールドを表示します。

SQL Server データベースを誤って削除した場合は、次の手順を実行して回復できます: データベース アクティビティの停止、ログ ファイルのバックアップ、データベース ログの確認、回復オプション: バックアップからの復元、トランザクション ログからの復元、DBCC CHECKDB の使用、3 番目の使用パーティーツール。データ損失を防ぐために、データベースを定期的にバックアップし、トランザクション ログを有効にしてください。

SQL Server データベース ファイルは、通常、次のデフォルトの場所に保存されます。 Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data データベース ファイルの場所は、データベース ファイル パスを変更することでカスタマイズできます。設定。

SQL Server サービスの開始に失敗した場合の解決手順は次のとおりです。 エラー ログを確認して、根本原因を特定します。サービス アカウントにサービスを開始する権限があることを確認してください。依存関係サービスが実行されているかどうかを確認します。ウイルス対策ソフトウェアを無効にします。 SQL Server のインストールを修復します。修復が機能しない場合は、SQL Server を再インストールします。

今回問題が判明したのは、これまで使用したことのない SqlServer データベースを使用していたことですが、問題は深刻ではありませんでした。要件文書の手順に従って SqlServer に接続した後、SpringBoot プロジェクトを開始すると、次のようなエラーが発生しました: 最初は SqlServer 接続だと思いました。問題があったので、データベースを確認しに行きましたが、すべてが正常であることがわかりました。最初に同僚にそのような問題があるかどうか尋ねたところ、彼らがそうでなかったので、Baidu プログラミングに取り組むという私の最も得意な部分を始めました。私が解決し始めた具体的なエラー メッセージは次のとおりでした。そこで、Baidu のエラー レポートを開始しました: ERRORc.a.d.p.DruidDataSource$CreateCo

SQL Server のインストールが失敗した場合は、次の手順に従ってクリーンアップできます。 SQL Server をアンインストールする レジストリ キーを削除する ファイルとフォルダーを削除する コンピューターを再起動する
