1

我有一个表格,其中包含pipes安装在某个位置的表格。安装日期始终填写,删除日期可以为 NULL

Type   Installed     Removed     Length
PT2    01/01/2011    NULL        2000
PT2    01/01/2011    NULL        2000
PT1    01/01/2011    NULL        1200
PT1    01/01/2011    NULL        1200
PT1    15/02/2011    25/02/2011  1000
PT1    15/02/2011    25/02/2011  1000

现在我需要一个给定月份安装的每种类型的总长度的概述,所以结果应该是例如 from 01/02/2011to 28/02/2011

 Type    From         To            Length
 PT2     01/02/2011   28/02/2011    4000
 PT1     01/02/2011   14/02/2011    2400 
 PT1     15/02/2011   24/02/2011    4400 Edit: (starts on 15 not 14)
 PT1     25/02/2011   28/02/2011    2400

编辑:澄清这个预期的结果。最后,这将用于查看请求月份中任何给定时刻的管道总长度。因此,如果您查看上表,则在 2 月 2 日期PT2间安装了管道。它们是在一月份安装的,但仍然存在,因此整个月的总长度为 4000。
同样适用于PT1:从 2 月开始安装 2 个 PT1 管道,所以 2400。但是在 15 日,额外的 2 个 PT1 管道安装长度为 1000,因此在 15 日至 25 日期间,PT1 管道的总长度为 4400。
这 2 条管道在 25 日拆除,因此 25 日至月底又是 2400。
我希望这现在更有意义了。

我正在为如何在 SQL 中执行此操作而苦苦挣扎,这是为了报告,通常在任何给定的月份都安装了数百个这样的管道。

它用于 Powerbuilder 应用程序,因此,如果您知道任何可能有助于分享的数据窗口技巧,请随时分享。

4

3 回答 3

1

玩得太开心了!我采取了一些自由,例如假设排序顺序,并将您的查询条件更改为日期范围而不是声明一个月。

首先,我将数据集从日期范围更改为更改日期和更改数量(安装日期为正值,移除日期为负值)。

SELECT type,
    installed as date_of_change,
    length change_of_length 
FROM pipes 
WHERE (installed BETWEEN :date_start AND
         :date_end) OR
     ((installed < :date_start) AND
         (isnull (removed, :date_end) >= :date_start)) 
UNION  ALL 
SELECT type,
    isnull (removed, dateadd (day, 1, :date_end)),
    (length * -1) 
FROM pipes 
WHERE (installed BETWEEN :date_start AND
         :date_end) OR
     ((installed < :date_start) AND
         (isnull (removed, :date_end) >= :date_start)) 
ORDER BY type,
    date_of_change 

而且,是的,参数前面的那些冒号(我已经将你切换到开始和结束日期......对我来说更容易,现在你可以做六个月的报告)意味着我正在利用数据窗口。(我打赌有人可以通过上述概念更改创建一个纯 SQL 方法,但我会使用我所知道的。)

将 SQL 加载到 DataWindow(我使用自由格式)中,并可选择按类型和 date_of_change(腰带和吊带)设置客户端排序。数据集包括 null 删除日期作为查询范围结束后第二天的更改,因此创建一个过滤器以排除这些空值:

 date_of_change <=  date_end 

根据类型创建一个组,并将类型放入组头。

在详细信息带(所有后续控件都在其中)中,使用以下表达式创建一个名为 date_from 的计算:

if (date_of_change < date_start, date_start, date_of_change)

使用以下表达式创建一个名为 date_to 的计算:

if (type = type[1] and getrow() < rowcount() and date_of_change[1] <= date_end, 
RelativeDate(date_of_change[1], -1),  date_end )

使用以下表达式创建一个名为 installed_length 的计算:

cumulativesum ( change_of_length FOR GROUP 1)

选择细节带中的所有控件,并给它们一个可见的表达式:

if (date_of_change =  date_of_change [1] and  type = type[1], 0, 1)

如果下一行具有相同的 date_of_change 和类型,这将使​​它们不可见;您只希望最后一行显示今天所有活动的总和。

将细节带拖动到零高度,并使细节带自动调整高度。

这会给你一个你所追求的版本。FWIW,我试图让报告按日期排序(不清楚这是您想要的,还是首先按类型排序),但它破坏了 CumulativeSum() 功能。也许其他人可以弄清楚。

祝你好运,

特里。

PS如果让我放这么多,这是我的原型的导出。它可能对你有用,也可能没用。

release 11.5;
datawindow(units=0 timer_interval=0 color=1073741824 brushmode=0 transparency=0 gradient.angle=0 gradient.color=8421504 gradient.focus=0 gradient.repetition.count=0 gradient.repetition.length=100 gradient.repetition.mode=0 gradient.scale=100 gradient.spread=100 gradient.transparency=0 picture.blur=0 picture.clip.bottom=0 picture.clip.left=0 picture.clip.right=0 picture.clip.top=0 picture.mode=0 picture.scale.x=100 picture.scale.y=100 picture.transparency=0 processing=0 HTMLDW=no print.printername="" print.documentname="" print.orientation = 0 print.margin.left = 110 print.margin.right = 110 print.margin.top = 96 print.margin.bottom = 96 print.paper.source = 0 print.paper.size = 0 print.canusedefaultprinter=yes print.prompt=no print.buttons=no print.preview.buttons=no print.cliptext=no print.overrideprintjob=no print.collate=yes print.background=no print.preview.background=no print.preview.outline=yes hidegrayline=no showbackcoloronxp=no picture.file="" )
header(height=72 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
summary(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
footer(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
detail(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" height.autosize=yes )
table(column=(type=char(4) updatewhereclause=yes name=type dbname="pipes.type" )
 column=(type=datetime updatewhereclause=yes name=date_of_change dbname="pipes.date_of_change" )
 column=(type=long updatewhereclause=yes name=change_of_length dbname="pipes.change_of_length" )
 retrieve="SELECT type, installed as date_of_change, length change_of_length
from pipes
where (installed BETWEEN :date_start AND :date_end) OR 
    ((installed < :date_start) AND (isnull (removed, :date_end) >= :date_start))
union all
select type, isnull (removed, dateadd (day, 1, :date_end)), (length * -1)
from pipes
where (installed BETWEEN :date_start AND :date_end) OR 
    ((installed < :date_start) AND (isnull (removed, :date_end) >= :date_start))
order by date_of_change, type" filter=" date_of_change <=  date_end "arguments=(("date_start", date),("date_end", date))  sort="type A date_of_change A " )
group(level=1 header.height=76 trailer.height=0 by=("type" ) header.color="536870912" header.transparency="0" header.gradient.color="8421504" header.gradient.transparency="0" header.gradient.angle="0" header.brushmode="0" header.gradient.repetition.mode="0" header.gradient.repetition.count="0" header.gradient.repetition.length="100" header.gradient.focus="0" header.gradient.scale="100" header.gradient.spread="100" trailer.color="536870912" trailer.transparency="0" trailer.gradient.color="8421504" trailer.gradient.transparency="0" trailer.gradient.angle="0" trailer.brushmode="0" trailer.gradient.repetition.mode="0" trailer.gradient.repetition.count="0" trailer.gradient.repetition.length="100" trailer.gradient.focus="0" trailer.gradient.scale="100" trailer.gradient.spread="100" )
text(band=header alignment="2" text="Type" border="0" color="33554432" x="5" y="4" height="64" width="224" html.valueishtml="0"  name=type_t visible="1"  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
text(band=header alignment="2" text="From" border="0" color="33554432" x="334" y="4" height="64" width="137" html.valueishtml="0"  name=t_1 visible="1"  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
text(band=header alignment="2" text="To" border="0" color="33554432" x="814" y="0" height="64" width="96" html.valueishtml="0"  name=t_2 visible="1"  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
column(band=header.1 id=1 alignment="0" tabsequence=32766 border="0" color="33554432" x="9" y="0" height="64" width="224" format="[general]" html.valueishtml="0"  name=type visible="1" edit.limit=4 edit.case=any edit.autoselect=yes edit.autohscroll=yes  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
compute(band=detail alignment="0" expression="if (type = type[1] and getrow() < rowcount() and date_of_change[1] <= date_end, RelativeDate(date_of_change[1], -1),  date_end )"border="0" color="33554432" x="837" y="0" height="64" width="347" format="[SHORTDATE]" html.valueishtml="0"  name=date_to visible="1~tif (date_of_change =  date_of_change [1] and  type = type[1], 0, 1)"  resizeable=1  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0"  height.autosize=yes)
compute(band=detail alignment="0" expression="cumulativesum ( change_of_length FOR GROUP 1)"border="0" color="33554432" x="1417" y="0" height="64" width="215" format="[GENERAL]" html.valueishtml="0"  name=installed_length visible="1~tif (date_of_change =  date_of_change [1] and  type = type[1], 0, 1)"  resizeable=1  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0"  height.autosize=yes)
compute(band=detail alignment="0" expression="if (date_of_change < date_start, date_start, date_of_change)"border="0" color="33554432" x="320" y="8" height="64" width="311" format="[SHORTDATE]" html.valueishtml="0"  name=date_from visible="1~tif (date_of_change =  date_of_change [1] and  type = type[1], 0, 1)"  resizeable=1  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0"  height.autosize=yes)
column(band=detail id=2 alignment="0" tabsequence=32766 border="0" color="33554432" x="1915" y="8" height="64" width="352" format="[shortdate]" html.valueishtml="0"  name=date_of_change visible="0"  resizeable=1 height.autosize=yes edit.limit=0 edit.case=any edit.autoselect=yes edit.autohscroll=yes  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
column(band=detail id=3 alignment="1" tabsequence=32766 border="0" color="33554432" x="2304" y="8" height="64" width="288" format="[General]" html.valueishtml="0"  name=change_of_length visible="0"  resizeable=1 height.autosize=yes edit.limit=0 edit.case=any edit.autoselect=yes edit.autohscroll=yes  font.face="Tahoma" font.height="-10" font.weight="400"  font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
htmltable(border="1" )
htmlgen(clientevents="1" clientvalidation="1" clientcomputedfields="1" clientformatting="0" clientscriptable="0" generatejavascript="1" encodeselflinkargs="1" netscapelayers="0" pagingmethod=0 generatedddwframes="1" )
xhtmlgen() cssgen(sessionspecific="0" )
xmlgen(inline="0" )
xsltgen()
jsgen()
export.xml(headgroups="1" includewhitespace="0" metadatatype=0 savemetadata=0 )
import.xml()
export.pdf(method=0 distill.custompostscript="0" xslfop.print="0" )
export.xhtml()
于 2011-04-13T03:44:53.633 回答
0

您的示例结果似乎没有意义。这

Type    From         To            Length
 PT2     01/02/2011   28/02/2011    4000
 PT1     01/02/2011   14/02/2011    2400 
 PT1     14/02/2011   24/02/2011    4400 
 PT1     25/02/2011   28/02/2011    2400

没有显示 1 月或 2 月安装了多少 PT1 管道。

以下查询显示每月安装的管道总长度,每种类型一行。

select type, 
       extract(year from installed) || '-' || extract(month from installed) as year_month, 
       sum(length) 
from pipes
group by type, year_month
order by year_month, type

返回

PT1   2011-1   2400
PT2   2011-1   4000
PT1   2011-2   2000

之后 。. .

对于任何给定日期的管道总长度,我可能会使用这样的东西。

select p.type, 
       '2011-02-28' as effective_date, 
       (select sum(length)
         from pipes 
         where installed <= '2011-02-28'
           and type = p.type) as installed,
       (select sum(length)
        from pipes
        where removed <= '2011-02-28'
          and type = p.type) as removed
from pipes p
group by p.type, effective_date
order by type

该查询返回

type  effective_date   installed  removed
PT1   2011-02-28       4400       2000
PT2   2011-02-28       4000
于 2011-04-12T11:09:32.053 回答
0

你的意思是在二月份(你的日期是 DDMMYYY 格式)PT1 的长度应该是一个负数,所以如果你要运行今年第一季度的报告,二月份删除的 2000 英尺将被减去从 1 月份安装的 2400 英尺 PT1 算起,PT1 在本季度的净安装长度为 400 英尺?如果是这样,您可以将其作为两个内联或持久视图的交集,一个用于安装,一个用于删除,类型和(提取的)时间段是连接列,从每个类型的安装长度中减去每个周期每个类型的删除长度每个时期以获得每个时期每种类型的净安装长度。安装和删除之间的左连接。

如果您还需要跟踪不一定由您的公司安装的管道的移除——例如,有移除 PT77 的记录但没有安装 PT77 的记录,您可以将其解决为两个内联或持久视图的 UNION ,一个用于安装,一个用于移除,移除被强制转换为负长度;然后你会将该 UNION ALL 查询本身放入一个内联视图中,你可以在其中按类型按句点分组并对长度求和。

于 2011-04-12T11:32:59.510 回答