﻿<?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>C++博客-Hello World-随笔分类-Teradata</title><link>http://www.cppblog.com/hxxrainbow/category/20671.html</link><description>Best wishes</description><language>zh-cn</language><lastBuildDate>Mon, 16 Sep 2013 15:30:36 GMT</lastBuildDate><pubDate>Mon, 16 Sep 2013 15:30:36 GMT</pubDate><ttl>60</ttl><item><title>Teradata preceeding/flowing</title><link>http://www.cppblog.com/hxxrainbow/archive/2013/09/16/203272.html</link><dc:creator>hxxrainbow</dc:creator><author>hxxrainbow</author><pubDate>Mon, 16 Sep 2013 15:15:00 GMT</pubDate><guid>http://www.cppblog.com/hxxrainbow/archive/2013/09/16/203272.html</guid><wfw:comment>http://www.cppblog.com/hxxrainbow/comments/203272.html</wfw:comment><comments>http://www.cppblog.com/hxxrainbow/archive/2013/09/16/203272.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hxxrainbow/comments/commentRss/203272.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hxxrainbow/services/trackbacks/203272.html</trackback:ping><description><![CDATA[<div>表示以前没玩过，智商捉急啦。<br />Description:找出任意连续三个月有操作而其第四个月没有操作的用户，时间范围一年之内。<br />Table: user<br />Column:month_id（一年之内）,user_id, action(抽象化为1,0)
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">SELECT</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">DISTINCT</span><span style="color: #000000">&nbsp;d.</span><span style="color: #ff00ff">user_id</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">FROM</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />(<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">SELECT</span><span style="color: #000000">&nbsp;c.</span><span style="color: #ff00ff">user_id</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />,</span><span style="color: #ff00ff">AVG</span><span style="color: #000000">(&nbsp;c.action&nbsp;)&nbsp;</span><span style="color: #0000ff">OVER</span><span style="color: #000000">&nbsp;(&nbsp;PARTITION&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;c.</span><span style="color: #ff00ff">user_id</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">ORDER</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;c.month_id&nbsp;ROWS&nbsp;</span><span style="color: #808080">BETWEEN</span><span style="color: #000000">&nbsp;</span><span style="color: #800000; font-weight: bold">2</span><span style="color: #000000">&nbsp;PRECEDING&nbsp;</span><span style="color: #808080">AND</span><span style="color: #000000">&nbsp;</span><span style="color: #800000; font-weight: bold">0</span><span style="color: #000000">&nbsp;FOLLOWING)&nbsp;&nbsp;m3<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />,</span><span style="color: #ff00ff">AVG</span><span style="color: #000000">(&nbsp;c.action&nbsp;)&nbsp;</span><span style="color: #0000ff">OVER</span><span style="color: #000000">&nbsp;(&nbsp;PARTITION&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;c.</span><span style="color: #ff00ff">user_id</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">ORDER</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;c.month_id&nbsp;ROWS&nbsp;</span><span style="color: #808080">BETWEEN</span><span style="color: #000000">&nbsp;</span><span style="color: #800000; font-weight: bold">3</span><span style="color: #000000">&nbsp;PRECEDING&nbsp;</span><span style="color: #808080">AND</span><span style="color: #000000">&nbsp;</span><span style="color: #800000; font-weight: bold">0</span><span style="color: #000000">&nbsp;FOLLOWING)&nbsp;&nbsp;m4<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">FROM</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #ff00ff">user</span><span style="color: #000000">&nbsp;c<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />)&nbsp;d<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008080">--</span><span style="color: #008080">&nbsp;no&nbsp;action&nbsp;on&nbsp;4th&nbsp;month</span><span style="color: #008080"><br /></span><span style="color: #008080">11</span><span style="color: #008080"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">QUALIFY&nbsp;</span><span style="color: #ff00ff">SUM</span><span style="color: #000000">&nbsp;(d.m4)&nbsp;</span><span style="color: #0000ff">OVER</span><span style="color: #000000">&nbsp;(PARTITION&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;d.</span><span style="color: #ff00ff">user_id</span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #0000ff">ORDER</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">BY</span><span style="color: #000000">&nbsp;d.month_id&nbsp;&nbsp;&nbsp;&nbsp;ROWS&nbsp;</span><span style="color: #808080">BETWEEN</span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;FOLLOWING&nbsp;&nbsp;</span><span style="color: #808080">AND</span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;FOLLOWING&nbsp;)&nbsp;&nbsp;</span><span style="color: #808080">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000"><br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008080">--</span><span style="color: #008080">did&nbsp;something&nbsp;continuesly&nbsp;in&nbsp;3&nbsp;months</span><span style="color: #008080"><br /></span><span style="color: #008080">13</span><span style="color: #008080"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #808080">AND</span><span style="color: #000000">&nbsp;m3&nbsp;</span><span style="color: #808080">=</span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000"><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #808080">AND</span><span style="color: #000000">&nbsp;m4</span><span style="color: #808080">&lt;&gt;</span><span style="color: #800000; font-weight: bold">1</span></div><br />m3算出前三个月的average（moving average的标准算法in Teradata）,同理m4算出前四个月moving average。<br />最后选择前三个月有过action，即m3=1且第四个月即qualify中m4&lt;1的结果集。<br />另外排除四个月连续具有action而第五个月没有的用户m4&lt;&gt;1，此类用户不属于需要被选择的用户。<br /><br />对于PRECEDING和FOLLOWING不熟悉，2 PRECEDING 前两行，0 FOLLOWING后0行，即到当前行，也可以直接不写between，直接写2 PRECEDING，默认表示到当前位置作一个计算。<br /><br /><br /></div> <img src ="http://www.cppblog.com/hxxrainbow/aggbug/203272.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hxxrainbow/" target="_blank">hxxrainbow</a> 2013-09-16 23:15 <a href="http://www.cppblog.com/hxxrainbow/archive/2013/09/16/203272.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>