339 lines
96 KiB
HTML
339 lines
96 KiB
HTML
<div style="margin-bottom: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.2000007629395px; background-color: #ffffff;">
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';">PIVOT</span><span style="font-family: 宋体;">用于将列值旋转为列名(即行转列),在SQL Server </span><span style="font-family: 'Times new roman';">2000</span><span style="font-family: 宋体;">可以用聚合函数配合</span><span style="font-family: 'Times new roman';">CASE</span><span style="font-family: 宋体;">语句实现</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">PIVOT</span><span style="font-family: 宋体;">的一般语法是:</span><span style="font-family: 宋体;">PIVOT</span><span style="font-family: 宋体;">(</span><span style="font-family: 宋体;">聚合函数(列) FOR 列 in (…) )AS P</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-family: 宋体;"><span style="font-size: small;">完整语法:</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">table_source</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">PIVOT(</span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">聚合函数(</span><span style="font-family: 'Times new roman';">value_column</span><span style="font-family: 宋体;">)</span></span></span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small; font-family: 'Times new roman';">FOR pivot_column</span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small; font-family: 'Times new roman';">IN()</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">)</span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';">UNPIVOT</span><span style="font-family: 宋体;">用于将列明转为列值(即列转行),在</span><span style="font-family: 'Times new roman';">SQL Server 2000</span><span style="font-family: 宋体;">可以用</span><span style="font-family: 'Times new roman';">UNION</span><span style="font-family: 宋体;">来实现</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-family: 宋体;"><span style="font-size: small;">完整语法:</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">table_source</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">UNPIVOT(</span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small; font-family: 'Times new roman';">value_column</span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small; font-family: 'Times new roman';">FOR pivot_column</span></p>
|
||
<p style="margin: 10px auto; text-indent: 21pt;"><span style="font-size: small; font-family: 'Times new roman';">IN()</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">)</span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-family: 宋体;"><span style="font-size: small;"><span style="font-size: small;">注意:PIVOT、UNPIVOT是SQL Server 2005 </span><span style="font-size: small;">的语法,使用需修改数据库</span><span style="font-size: small;">兼容级别</span></span></span><span style="font-size: 9pt; font-family: Tahoma;"><br />
|
||
</span><span style="font-family: 宋体;"><span style="font-size: small;">在数据库属性->选项->兼容级别改为
|
||
90</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-family: 宋体;">典型实例</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-family: 宋体;"><span style="font-size: small;">一、行转列</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">1</span><span style="font-family: 宋体;">、建立表格</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">if</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: fuchsia;">object_id</span><span style="color: gray;">(</span><span style="color: red;">'tb'</span><span style="color: gray;">)</span><span style="color: gray;">is</span><span style="color: gray;">not</span><span style="color: gray;">null</span><span style="color: blue;">drop</span><span style="color: blue;">table</span>tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">create</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">table</span>tb<span style="color: gray;">(</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">varchar</span><span style="color: gray;">(</span>10<span style="color: gray;">),</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">varchar</span><span style="color: gray;">(</span>10<span style="color: gray;">),</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">int</span><span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">张三</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>74<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">张三</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>83<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">张三</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>93<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">李四</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>74<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">李四</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>84<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">李四</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">,</span>94<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">*</span><span style="color: blue;">from</span>tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">课程</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">分数</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">---------- ----------
|
||
-----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
83</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
93</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
84</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
94</span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">2</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--c</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';"> <span style="color: fuchsia;">max</span><span style="color: gray;">(</span><span style="color: blue;">case</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">when</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">else</span>0<span style="color: blue;">end<span style="color: gray;">)</span></span></span><span style="font-size: 10pt; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';"> <span style="color: fuchsia;">max</span><span style="color: gray;">(</span><span style="color: blue;">case</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">when</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">else</span>0<span style="color: blue;">end<span style="color: gray;">)</span></span></span><span style="font-size: 10pt; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';"> <span style="color: fuchsia;">max</span><span style="color: gray;">(</span><span style="color: blue;">case</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">when</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">else</span>0<span style="color: blue;">end<span style="color: gray;">)</span></span></span><span style="font-size: 10pt; font-family: 宋体;">物理</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">group</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">by</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">----------
|
||
----------- ----------- -----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74
|
||
84
|
||
94</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74
|
||
83
|
||
93</span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">3</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000动态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--SQL
|
||
SERVER 2000</span><span style="font-size: 10pt; color: green; font-family: 宋体;">动态</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">SQL,</span><span style="font-size: 10pt; color: green; font-family: 宋体;">指课程不止语文、数学、物理这三门课程。</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: green; font-family: 宋体;">以下同</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">)</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">变量按</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">sql</span><span style="font-size: 10pt; color: green; font-family: 宋体;">语言顺序赋值</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>500<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">',max(case</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">when
|
||
'''</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">''' then</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">else
|
||
0 end)['</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">']'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span><span style="color: blue;">select</span><span style="color: blue;">distinct</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">from</span>tb<span style="color: gray;">)a<span style="color: green;">--</span></span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">同</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">from
|
||
tb group by</span><span style="font-size: 10pt; color: green; font-family: 宋体;">课程,默认按课程名排序</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">'
|
||
from tb group by</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">使用</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">isnull(),</span><span style="font-size: 10pt; color: green; font-family: 宋体;">变量先确定动态部分</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">isnull</span><span style="color: gray;">(</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: gray;">+</span><span style="color: red;">'
|
||
max(case</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">when
|
||
'''</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: red;">'''
|
||
then</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">else
|
||
0 end) ['</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: red;">']'</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span><span style="color: blue;">select</span><span style="color: blue;">distinct</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">from</span>tb<span style="color: gray;">)<span style="color: blue;">as</span>a </span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,'</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">+</span><span style="color: red;">'
|
||
from tb group by</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">----------
|
||
----------- ----------- -----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
84
|
||
94
|
||
74</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
83
|
||
93
|
||
74</span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">4</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2005静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">*</span><span style="color: blue;">from</span>tb <span style="color: gray;">pivot</span><span style="color: gray;">(</span><span style="color: fuchsia;">max</span><span style="color: gray;">(</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">for</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">in</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span></span><span style="font-size: 10pt; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">))</span><span style="font-size: 10pt; font-family: 'Courier new';">a</span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; font-family: 'Courier new';">5</span><span style="font-size: 10pt; font-family: 宋体;">、</span><span style="font-family: 宋体;"><span style="font-size: small;">使用SQL Server
|
||
2005动态SQL</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">使用</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">stuff()</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">''</span> <span style="color: green;">--</span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">初始化变量</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">@sql</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">from</span>tb<span style="color: blue;">groupby</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">变量多值赋值</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">stuff</span><span style="color: gray;">(</span>@sql<span style="color: gray;">,</span>1<span style="color: gray;">,</span>1<span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: green;">--</span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">去掉首个</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">','</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select
|
||
* from tb pivot (max(</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
for</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">in
|
||
('</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span>@sql<span style="color: gray;">+</span><span style="color: red;">'))a'</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">或使用</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">isnull()</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">–-</span><span style="font-size: 10pt; color: green; font-family: 宋体;">获得课程集合</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">isnull</span><span style="color: gray;">(</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">from</span>tb<span style="color: blue;">groupby</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"> </span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select
|
||
* from tb pivot (max(</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
for</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">in
|
||
('</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span>@sql<span style="color: gray;">+</span><span style="color: red;">'))a'</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-family: 宋体;"><span style="font-size: small;">二、行转列结果加上总分、平均分</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">1</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--SQL
|
||
SERVER 2000</span><span style="font-size: 10pt; color: green; font-family: 宋体;">静态</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">SQL</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">max</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">case</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">when</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">else</span><span style="font-size: 10pt; font-family: 'Courier new';">0<span style="color: blue;">end</span><span style="color: gray;">)</span></span><span style="font-size: 10pt; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">max</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">case</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">when</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">else</span><span style="font-size: 10pt; font-family: 'Courier new';">0<span style="color: blue;">end</span><span style="color: gray;">)</span></span><span style="font-size: 10pt; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">max</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">case</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">when</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">then</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">else</span><span style="font-size: 10pt; font-family: 'Courier new';">0<span style="color: blue;">end</span><span style="color: gray;">)</span></span><span style="font-size: 10pt; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">sum</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span><span style="font-size: 10pt; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">cast</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">avg</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">*</span><span style="font-size: 10pt; font-family: 'Courier new';">1.0<span style="color: gray;">)</span><span style="color: blue;">as</span><span style="color: blue;">decimal</span><span style="color: gray;">(</span>18<span style="color: gray;">,</span>2<span style="color: gray;">))</span></span><span style="font-size: 10pt; font-family: 宋体;">平均分</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">group</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">by</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">总分</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">平均分</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">----------
|
||
----------- ----------- ----------- -----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74
|
||
84
|
||
94
|
||
252
|
||
84.00</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74
|
||
83
|
||
93
|
||
250
|
||
83.33</span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">2</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000动态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--SQL
|
||
SERVER 2000</span><span style="font-size: 10pt; color: green; font-family: 宋体;">动态</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">SQL</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>500<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">',max(case</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">when
|
||
'''</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">''' then</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">else
|
||
0 end)['</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">']'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span><span style="color: blue;">select</span><span style="color: blue;">distinct</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">tb<span style="color: gray;">)</span>a</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">',sum(</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)</span><span style="font-size: 10pt; color: red; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,cast(avg(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">*1.0)
|
||
as decimal(18,2))
|
||
</span><span style="font-size: 10pt; color: red; font-family: 宋体;">平均分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">from
|
||
tb group by</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">3</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2005静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">m<span style="color: gray;">.*,</span>n<span style="color: gray;">.</span></span><span style="font-size: 10pt; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 'Courier new';">n<span style="color: gray;">.</span></span><span style="font-size: 10pt; font-family: 宋体;">平均分</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">*</span><span style="color: blue;">from</span>tb <span style="color: gray;">pivot(</span><span style="color: fuchsia;">max</span><span style="color: gray;">(</span></span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">for</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">in(</span><span style="font-size: 10pt; font-family: 宋体;">语文</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 宋体;">数学</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 宋体;">物理</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">))</span><span style="font-size: 10pt; font-family: 'Courier new';">a<span style="color: gray;">)</span>m<span style="color: gray;">,</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">sum</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span><span style="font-size: 10pt; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">cast</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; color: fuchsia; font-family: 'Courier new';">avg</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">*</span><span style="font-size: 10pt; font-family: 'Courier new';">1.0<span style="color: gray;">)</span><span style="color: blue;">as</span><span style="color: blue;">decimal</span><span style="color: gray;">(</span>18<span style="color: gray;">,</span>2<span style="color: gray;">))</span></span><span style="font-size: 10pt; font-family: 宋体;">平均分</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">group</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">by</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span><span style="font-size: 10pt; font-family: 'Courier new';">n</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">where</span><span style="font-size: 10pt; font-family: 'Courier new';">m<span style="color: gray;">.</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">=</span><span style="font-size: 10pt; font-family: 'Courier new';">n<span style="color: gray;">.</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">4</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2005动态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">使用</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">stuff()</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">''</span> <span style="color: green;">--</span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">初始化变量</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">@sql</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">from</span>tb<span style="color: blue;">groupby</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">变量多值赋值</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">同</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">select
|
||
@sql = @sql + ','+</span><span style="font-size: 10pt; color: green; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">from
|
||
(select distinct</span><span style="font-size: 10pt; color: green; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">from
|
||
tb)a</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">stuff</span><span style="color: gray;">(</span>@sql<span style="color: gray;">,</span>1<span style="color: gray;">,</span>1<span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: green;">--</span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">去掉首个</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">','</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select
|
||
m.* , n.</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,n.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">平均分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">from</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">(select
|
||
* from (select * from tb) a pivot (max(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
for</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">in
|
||
('</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span>@sql<span style="color: gray;">+</span><span style="color: red;">')) b) m
|
||
,</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">(select</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,sum(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)</span><span style="font-size: 10pt; color: red; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,
|
||
cast(avg(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">*1.0)
|
||
as decimal(18,2))</span><span style="font-size: 10pt; color: red; font-family: 宋体;">平均分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">from
|
||
tb group by</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
n</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">where
|
||
m.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">=
|
||
n.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: green; font-family: 'Courier new';">--</span><span style="font-size: 10pt; color: green; font-family: 宋体;">或使用</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">isnull()</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">isnull</span><span style="color: gray;">(</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: gray;">+</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">tb<span style="color: blue;">group</span><span style="color: blue;">by</span></span><span style="font-size: 10pt; font-family: 宋体;">课程</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select
|
||
m.* , n.</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,n.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">平均分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">from</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">(select
|
||
* from (select * from tb) a pivot (max(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
for</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">in
|
||
('</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';"> @sql<span style="color: gray;">+</span><span style="color: red;">'))
|
||
b) m ,</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">(select</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,sum(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)</span><span style="font-size: 10pt; color: red; font-family: 宋体;">总分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,
|
||
cast(avg(</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">*1.0)
|
||
as decimal(18,2))</span><span style="font-size: 10pt; color: red; font-family: 宋体;">平均分</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">from
|
||
tb group by</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">)
|
||
n</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: red; font-family: 'Courier new';">where
|
||
m.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">=
|
||
n.</span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">(</span>@sql<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-family: 宋体;">二、列转行</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Courier new';">1</span><span style="font-family: 宋体;">、建立表格</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">if</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: fuchsia;">object_id</span><span style="color: gray;">(</span><span style="color: red;">'tb'</span><span style="color: gray;">)</span><span style="color: gray;">is</span><span style="color: gray;">not</span><span style="color: gray;">null</span><span style="color: blue;">drop</span><span style="color: blue;">table</span>tb</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">create</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">table</span>tb<span style="color: gray;">(</span></span><span style="font-size: 10pt; font-family: 宋体;">姓名</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">varchar</span><span style="color: gray;">(</span>10<span style="color: gray;">),</span></span><span style="font-size: 10pt; font-family: 宋体;">语文</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">int</span><span style="color: gray;">,</span></span><span style="font-size: 10pt; font-family: 宋体;">数学</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">int</span><span style="color: gray;">,</span></span><span style="font-size: 10pt; font-family: 宋体;">物理</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">int</span><span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">张三</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 'Courier new';">74<span style="color: gray;">,</span>83<span style="color: gray;">,</span>93<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">insert</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">into</span>tb<span style="color: blue;">values</span><span style="color: gray;">(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">李四</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">,</span><span style="font-size: 10pt; font-family: 'Courier new';">74<span style="color: gray;">,</span>84<span style="color: gray;">,</span>94<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">*</span><span style="color: blue;">from</span>tb</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-family: 'Courier new';"><span style="font-size: small;">go</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">----------
|
||
----------- ----------- -----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> 74
|
||
83
|
||
93</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';">
|
||
74
|
||
84
|
||
94</span></p>
|
||
<p style="margin: 10px auto;"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">2</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="color: teal;"><span style="font-family: 'Times new roman';">--SQL
|
||
SERVER 2000</span></span><span style="color: teal; font-family: 宋体;">静态</span><span style="color: teal;"><span style="font-family: 'Times new roman';">SQL</span></span><span style="color: teal; font-family: 宋体;">。</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"><span style="font-size: small;"><span style="color: blue;">select</span><span style="color: gray;">*<span style="color: blue;">from</span></span></span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">(</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"> <span style="color: blue;">select</span></span><span style="font-family: 宋体;">姓名</span><span style="font-family: 'Times new roman';">,</span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span><span style="color: red;">'</span></span><span style="color: red; font-family: 宋体;">语文</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span>,</span><span style="font-family: 宋体;">分数</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span></span><span style="font-family: 宋体;">语文</span><span style="font-family: 'Times new roman';"><span style="color: blue;">from</span>tb</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"><span style="font-size: small;"> <span style="color: blue;">union<span style="color: gray;">all</span></span></span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"> <span style="color: blue;">select</span></span><span style="font-family: 宋体;">姓名</span><span style="font-family: 'Times new roman';">,</span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span><span style="color: red;">'</span></span><span style="color: red; font-family: 宋体;">数学</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span>,</span><span style="font-family: 宋体;">分数</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span></span><span style="font-family: 宋体;">数学</span><span style="font-family: 'Times new roman';"><span style="color: blue;">from</span>tb</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"><span style="font-size: small;"> <span style="color: blue;">union<span style="color: gray;">all</span></span></span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"> <span style="color: blue;">select</span></span><span style="font-family: 宋体;">姓名</span><span style="font-family: 'Times new roman';">,</span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span><span style="color: red;">'</span></span><span style="color: red; font-family: 宋体;">物理</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span>,</span><span style="font-family: 宋体;">分数</span><span style="font-family: 'Times new roman';"><span style="color: gray;">=</span></span><span style="font-family: 宋体;">物理</span><span style="font-family: 'Times new roman';"><span style="color: blue;">from</span>tb</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small; font-family: 'Times new roman';">) t</span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"><span style="color: blue;">order</span><span style="color: blue;">by</span></span><span style="font-family: 宋体;">姓名</span><span style="font-family: 'Times new roman';">,<span style="color: fuchsia;">case</span></span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';"><span style="color: blue;">when</span><span style="color: red;">'</span></span><span style="color: red; font-family: 宋体;">语文</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span><span style="color: blue;">then<strong><span style="color: maroon;">1</span></strong>when<span style="color: red;">'</span></span></span><span style="color: red; font-family: 宋体;">数学</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span><span style="color: blue;">then<strong><span style="color: maroon;">2</span></strong>when<span style="color: red;">'</span></span></span><span style="color: red; font-family: 宋体;">物理</span><span style="font-family: 'Times new roman';"><span style="color: red;">'</span><span style="color: blue;">then<strong><span style="color: maroon;">3</span></strong>end</span></span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">姓名</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">课程</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">分数</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 'Courier new';">---------- ----
|
||
-----------</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';"> 74</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> 84</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">李四</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';"> 94</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">语文</span><span style="font-size: 9pt; font-family: 'Courier new';"> 74</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">数学</span><span style="font-size: 9pt; font-family: 'Courier new';"> 83</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 9pt; font-family: 宋体;">张三</span><span style="font-size: 9pt; font-family: 'Courier new';"> </span><span style="font-size: 9pt; font-family: 宋体;">物理</span><span style="font-size: 9pt; font-family: 'Courier new';"> 93</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 8pt; font-family: 'Courier new';"> </span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">2</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2000动态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="color: teal;"><span style="font-family: 'Times new roman';">--SQL
|
||
SERVER 2000</span></span><span style="color: teal; font-family: 宋体;">动态</span><span style="color: teal;"><span style="font-family: 'Times new roman';">SQL</span></span><span style="color: teal; font-family: 宋体;">。</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="color: teal;"><span style="font-family: 'Times new roman';">--</span></span><span style="color: teal; font-family: 宋体;">调用系统表动态生态。</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">varchar</span><span style="color: gray;">(</span>8000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">isnull</span><span style="color: gray;">(</span>@sql<span style="color: gray;">+</span><span style="color: red;">'
|
||
union all '</span><span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)</span><span style="color: gray;">+</span><span style="color: red;">'
|
||
select</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,
|
||
[</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">]='</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: fuchsia;">quotename</span><span style="color: gray;">(</span><span style="color: blue;">Name</span><span style="color: gray;">,</span><span style="color: red;">''''</span><span style="color: gray;">)</span><span style="color: gray;">+</span><span style="color: red;">'
|
||
, [</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">]
|
||
= '</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">+</span><span style="color: fuchsia;">quotename</span><span style="color: gray;">(</span><span style="color: blue;">Name</span><span style="color: gray;">)</span><span style="color: gray;">+</span><span style="color: red;">' from
|
||
tb'</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">syscolumns</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">where</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">Name</span><span style="color: gray;">!=</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: gray;">and</span>ID<span style="color: gray;">=</span><span style="color: fuchsia;">object_id</span><span style="color: gray;">(</span><span style="color: red;">'tb'</span><span style="color: gray;">)</span><span style="color: green;">--</span></span><span style="font-size: 10pt; color: green; font-family: 宋体;">表名</span><span style="font-size: 10pt; color: green; font-family: 'Courier new';">tb</span><span style="font-size: 10pt; color: green; font-family: 宋体;">,不包含列名为姓名的其他列</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">order</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">by</span>colid</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">+</span><span style="color: red;">'
|
||
order by</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; font-family: 'Courier new';">go</span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">3</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2005静态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="color: teal;"><span style="font-family: 'Times new roman';">--SQL
|
||
SERVER 2005</span></span><span style="color: teal; font-family: 宋体;">动态</span><span style="color: teal;"><span style="font-family: 'Times new roman';">SQL</span></span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 'Times new roman';"><span style="color: blue;">select</span></span><span style="font-family: 宋体;">姓名</span><span style="font-family: 'Times new roman';">,</span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';">,</span><span style="font-family: 宋体;">分数</span><span style="font-family: 'Times new roman';"><span style="color: blue;">from</span>tb unpivot (</span><span style="font-family: 宋体;">分数</span><span style="font-family: 'Times new roman';"><span style="color: blue;">for</span></span><span style="font-family: 宋体;">课程</span><span style="font-family: 'Times new roman';"><span style="color: gray;">in</span>(<span style="color: red;">[</span></span><span style="color: red; font-family: 宋体;">语文</span><span style="font-family: 'Times new roman';"><span style="color: red;">]</span>,<span style="color: red;">[</span></span><span style="color: red; font-family: 宋体;">数学</span><span style="font-family: 'Times new roman';"><span style="color: red;">]</span>,<span style="color: red;">[</span></span><span style="color: red; font-family: 宋体;">物理</span><span style="font-family: 'Times new roman';"><span style="color: red;">]</span>)) t</span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"> </p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="font-family: 宋体;">4</span><span style="font-family: 宋体;">、使用SQL
|
||
Server 2005动态SQL</span></span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: small;"><span style="font-size: small;"><span style="color: teal;"><span style="font-family: 'Times new roman';">--SQL
|
||
SERVER 2005</span></span><span style="color: teal; font-family: 宋体;">动态</span><span style="color: teal;"><span style="font-family: 'Times new roman';">SQL</span></span></span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">declare</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: blue;">nvarchar</span><span style="color: gray;">(</span>4000<span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">select</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: fuchsia;">isnull</span><span style="color: gray;">(</span>@sql<span style="color: gray;">+</span><span style="color: red;">','</span><span style="color: gray;">,</span><span style="color: red;">''</span><span style="color: gray;">)+</span><span style="color: fuchsia;">quotename</span><span style="color: gray;">(</span><span style="color: blue;">Name</span><span style="color: gray;">)</span></span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">from</span><span style="font-size: 10pt; font-family: 'Courier new';">syscolumns</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">where</span><span style="font-size: 10pt; font-family: 'Courier new';">ID<span style="color: gray;">=</span><span style="color: fuchsia;">object_id</span><span style="color: gray;">(</span><span style="color: red;">'tb'</span><span style="color: gray;">)</span><span style="color: gray;">and</span><span style="color: blue;">Name</span><span style="color: gray;">not</span><span style="color: gray;">in(</span><span style="color: red;">'</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">'</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">)</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">order</span><span style="font-size: 10pt; font-family: 'Courier new';"><span style="color: blue;">by</span>Colid</span></p>
|
||
<p style="margin: 10px auto;" align="left"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">set</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">=</span><span style="color: red;">'select</span></span><span style="font-size: 10pt; color: red; font-family: 宋体;">姓名</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">,[</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">],[</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">]
|
||
from tb unpivot ([</span><span style="font-size: 10pt; color: red; font-family: 宋体;">分数</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">]
|
||
for [</span><span style="font-size: 10pt; color: red; font-family: 宋体;">课程</span><span style="font-size: 10pt; color: red; font-family: 'Courier new';">]
|
||
in('</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">+</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">+</span><span style="color: red;">'))b'</span></span></p>
|
||
<p style="margin: 10px auto;"><span style="font-size: 10pt; color: blue; font-family: 'Courier new';">exec</span><span style="font-size: 10pt; color: gray; font-family: 'Courier new';">(</span><span style="font-size: 10pt; font-family: 'Courier new';">@sql<span style="color: gray;">)</span></span></p>
|
||
|
||
|
||
</div>
|
||
<div style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.2000007629395px; background-color: #ffffff;">
|
||
该文章经过本人整理所得,欢迎转载,转载时请加上本文地址;本文基于<a title="Creative Commons Attribution 2.5 China Mainland License" href="http://creativecommons.org/licenses/by/2.5/cn/" target="_blank"><span style="color: #6699cc;">署名
|
||
2.5 中国大陆</span></a>许可协议发布,请勿演绎或用于商业目的,但是必须保留本文的署名<a href="http://www.cnblogs.com/zhangzt/"><span style="color: #6699cc;">张志涛</span></a></div>
|
||
<p><br /><img src="http://simg.sinajs.cn/blog7style/images/special/1265.gif" alt="" /> <a href="http://sina.allyes.com/main/adfclick?db=sina&bid=204720,469641,474922&cid=0,0,0&sid=473458&advid=358&camid=37389&show=ignore&url=http://qing.blog.sina.com.cn/tag/%E5%86%99%E7%9C%9F" target="_blank">青春就应该这样绽放</a> <a href="http://sina.allyes.com/main/adfclick?db=sina&bid=204720,469645,474926&cid=0,0,0&sid=473464&advid=358&camid=37389&show=ignore&url=http%3A%2F%2Funion.9173.com%2Fpub%3Fp%3D1%26u%3D1008" target="_blank">游戏测试:三国时期谁是你最好的兄弟!!</a> <a href="http://sina.allyes.com/main/adfclick?db=sina&bid=204720,469646,474927&cid=0,0,0&sid=473465&advid=358&camid=37389&show=ignore&url=http://qing.blog.sina.com.cn/tag/%E6%98%9F%E5%BA%A7" target="_blank">你不得不信的星座秘密</a><img src="http://sina.allyes.com/main/adfclick?db=sina&bid=204720,470173,475454&cid=0,0,0&sid=474001&advid=358&camid=37389&show=ignore&url=http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif?t=2" alt="" /></p> |