﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-xfpx's blog</title><link>http://www.cnblogs.com/xfpx/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 05 Dec 2008 03:19:24 GMT</lastBuildDate><pubDate>Fri, 05 Dec 2008 03:19:24 GMT</pubDate><ttl>60</ttl><item><title>SQL查询备忘录</title><link>http://www.cnblogs.com/xfpx/archive/2008/08/24/1275195.html</link><dc:creator>西风飘雪</dc:creator><author>西风飘雪</author><pubDate>Sun, 24 Aug 2008 08:17:00 GMT</pubDate><guid>http://www.cnblogs.com/xfpx/archive/2008/08/24/1275195.html</guid><wfw:comment>http://www.cnblogs.com/xfpx/comments/1275195.html</wfw:comment><comments>http://www.cnblogs.com/xfpx/archive/2008/08/24/1275195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/xfpx/comments/commentRss/1275195.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/xfpx/services/trackbacks/1275195.html</trackback:ping><description><![CDATA[<h3><span style="color: red">一个关于中国式报表统计的SQL语句</span></h3>
<p>表tablename的内容如下： <br />
<br />
供应方式&nbsp; &nbsp; 供应时间&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 金额 <br />
方式1&nbsp; &nbsp; &nbsp; 2008-01-01&nbsp; &nbsp; &nbsp; &nbsp; 100 <br />
方式1&nbsp; &nbsp; &nbsp; 2008-04-01&nbsp; &nbsp; &nbsp; &nbsp; 200 <br />
方式1&nbsp; &nbsp; &nbsp; 2008-05-01&nbsp; &nbsp; &nbsp; &nbsp; 100 <br />
方式2&nbsp; &nbsp; &nbsp; 2008-01-01&nbsp; &nbsp; &nbsp; &nbsp; 100 <br />
方式2&nbsp; &nbsp; &nbsp; 2008-05-01&nbsp; &nbsp; &nbsp; &nbsp; 100 <br />
<br />
<br />
要实现的报表形式如下: <br />
----------------------------------- <br />
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#166;&nbsp; &nbsp; 数量&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &#166;&nbsp; &nbsp; 金额 <br />
供应方式&nbsp; &#166;------------- &#166;-------------- <br />
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&#166; 本季 &#166; 累计 &#166; 本季 &#166; 累计 &#166; <br />
----------------------------------- <br />
&nbsp;方式1&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;2&nbsp; &nbsp; 3&nbsp; &nbsp; 300&nbsp; 400&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
----------------------------------- <br />
&nbsp;方式2&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;1&nbsp; &nbsp; 2&nbsp; &nbsp; 100&nbsp; 200 <br />
----------------------------------- <br />
<br />
要求返回如下结果： <br />
<br />
供应方式&nbsp; 本季数量&nbsp; &nbsp; 累计数量&nbsp; 本季金额&nbsp; 累计金额 <br />
方式1&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; 300&nbsp; &nbsp; &nbsp; 400 <br />
方式2&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; 100&nbsp; &nbsp; &nbsp; 200 </p>
<p>&nbsp;</p>
<p>SQL语句如下：</p>
<p>&nbsp;</p>
<div class="cnblogs_code"><img id="Code_Closed_Image_161450" onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_161450').style.display='none'; document.getElementById('Code_Open_Image_161450').style.display='inline'; document.getElementById('Code_Open_Text_161450').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_161450" style="display: none" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_161450').style.display='none'; getElementById('Code_Closed_Image_161450').style.display='inline'; getElementById('Code_Closed_Text_161450').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span class="cnblogs_code_Collapse" id="Code_Closed_Text_161450">Code</span><span id="Code_Open_Text_161450" style="display: none"><br />
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">select</span><span style="color: #000000"><br />
&nbsp;&nbsp;&nbsp;&nbsp;供应方式,<br />
&nbsp;&nbsp;&nbsp;&nbsp;本季数量</span><span style="color: #808080">=</span><span style="color: #ff00ff">sum</span><span style="color: #000000">(</span><span style="color: #ff00ff">case</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">when</span><span style="color: #000000">&nbsp;</span><span style="color: #ff00ff">datediff</span><span style="color: #000000">(quarter,供应时间,</span><span style="color: #ff00ff">getdate</span><span style="color: #000000">())</span><span style="color: #808080">=</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">then</span><span style="color: #000000">&nbsp;</span><span style="font-weight: bold; color: #800000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">end</span><span style="color: #000000">),<br />
&nbsp;&nbsp;&nbsp;&nbsp;累计数量</span><span style="color: #808080">=</span><span style="color: #ff00ff">count</span><span style="color: #000000">(</span><span style="color: #808080">*</span><span style="color: #000000">),<br />
&nbsp;&nbsp;&nbsp;&nbsp;本季金额</span><span style="color: #808080">=</span><span style="color: #ff00ff">sum</span><span style="color: #000000">(</span><span style="color: #ff00ff">case</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">when</span><span style="color: #000000">&nbsp;</span><span style="color: #ff00ff">datediff</span><span style="color: #000000">(quarter,供应时间,</span><span style="color: #ff00ff">getdate</span><span style="color: #000000">())</span><span style="color: #808080">=</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">then</span><span style="color: #000000">&nbsp;金额&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">end</span><span style="color: #000000">),<br />
&nbsp;&nbsp;&nbsp;&nbsp;累计金额</span><span style="color: #808080">=</span><span style="color: #ff00ff">sum</span><span style="color: #000000">(金额)<br />
</span><span style="color: #0000ff">from</span><span style="color: #000000">&nbsp;#T<br />
</span><span style="color: #0000ff">group</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">by</span><span style="color: #000000">&nbsp;供应方式</span></span></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3><span style="color: red">一个查询语句</span> </h3>
<p>有一表的内容如下： <br />
name&nbsp; a&nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp; yongtu <br />
a1&nbsp; &nbsp; 1.20&nbsp; &nbsp; 5.00&nbsp; &nbsp; &nbsp; 用途1 <br />
a2&nbsp; &nbsp; 1.20&nbsp; &nbsp; 6.00&nbsp; &nbsp; &nbsp; 用途1 <br />
a2&nbsp; &nbsp; 1.20&nbsp; &nbsp; 6.00&nbsp; &nbsp; &nbsp; 用途1 <br />
a3&nbsp; &nbsp; 1.30&nbsp; &nbsp; 6.00&nbsp; &nbsp; &nbsp; 用途2 </p>
<p>a3&nbsp; &nbsp; 1.20&nbsp; &nbsp; 6.00&nbsp; &nbsp; &nbsp; 用途3</p>
<p>b3&nbsp; &nbsp; 1.10&nbsp;&nbsp;&nbsp; 8.00&nbsp; &nbsp; &nbsp; 用途3 <br />
<br />
要求按yongtu分组，返回各用途里name包含字符'a'的记录里b/a值最大的记录 <br />
name&nbsp; &nbsp; ba&nbsp; &nbsp; &nbsp; &nbsp; yongtu <br />
a2&nbsp; &nbsp; 5.00&nbsp; &nbsp; &nbsp; &nbsp; 用途1 <br />
a3&nbsp; &nbsp; 4.62&nbsp; &nbsp; &nbsp; &nbsp; 用途2 <br />
<br />
其中ba=b/a(保留两位),且a,b都是varchar类型。如果yongtu中有相同值，则取ba最大的一条记录（只取一条）。 <br />
</p>
<p>SQL语句如下：</p>
<p>&nbsp;</p>
<div class="cnblogs_code"><img id="Code_Closed_Image_164254" onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_164254').style.display='none'; document.getElementById('Code_Open_Image_164254').style.display='inline'; document.getElementById('Code_Open_Text_164254').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_164254" style="display: none" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_164254').style.display='none'; getElementById('Code_Closed_Image_164254').style.display='inline'; getElementById('Code_Closed_Text_164254').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span class="cnblogs_code_Collapse" id="Code_Closed_Text_164254">Code</span><span id="Code_Open_Text_164254" style="display: none"><br />
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">select</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">distinct</span><span style="color: #000000">&nbsp;a.name,b.ba,a.yongtu&nbsp;</span><span style="color: #0000ff">from</span><span style="color: #000000">&nbsp;#T&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;a<br />
</span><span style="color: #0000ff">inner</span><span style="color: #000000">&nbsp;</span><span style="color: #808080">join</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">select</span><span style="color: #000000">&nbsp;yongtu,</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(</span><span style="color: #ff00ff">max</span><span style="color: #000000">(</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(b&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">))</span><span style="color: #808080">/</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(a&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">)))</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">))&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;ba&nbsp;</span><span style="color: #0000ff">from</span><span style="color: #000000">&nbsp;#T&nbsp;</span><span style="color: #0000ff">where</span><span style="color: #000000">&nbsp;</span><span style="color: #ff00ff">charindex</span><span style="color: #000000">(</span><span style="color: #ff0000">'</span><span style="color: #ff0000">a</span><span style="color: #ff0000">'</span><span style="color: #000000">,name)</span><span style="color: #808080">&gt;</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">group</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">by</span><span style="color: #000000">&nbsp;yongtu)&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;b&nbsp;</span><span style="color: #0000ff">on</span><span style="color: #000000">&nbsp;a.yongtu</span><span style="color: #808080">=</span><span style="color: #000000">b.yongtu&nbsp;<br />
</span><span style="color: #808080">and</span><span style="color: #000000">&nbsp;</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(a.b&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">))</span><span style="color: #808080">/</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(a.a&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">))&nbsp;</span><span style="color: #0000ff">as</span><span style="color: #000000">&nbsp;numeric(</span><span style="font-weight: bold; color: #800000">18</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">))</span><span style="color: #808080">=</span><span style="color: #000000">b.ba&nbsp;</span><span style="color: #808080">and</span><span style="color: #000000">&nbsp;</span><span style="color: #ff00ff">charindex</span><span style="color: #000000">(</span><span style="color: #ff0000">'</span><span style="color: #ff0000">a</span><span style="color: #ff0000">'</span><span style="color: #000000">,a.name)</span><span style="color: #808080">&gt;</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000"><br />
</span></span></div>
<img src ="http://www.cnblogs.com/xfpx/aggbug/1275195.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43806/" target="_blank">[新闻]Python 3.0正式发布</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item></channel></rss>