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

ここで、特定の月にインストールされたタイプごとの全長の概要が必要なので、結果は次のよう01/02/2011になり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パイプが設置されました。それらは1月に設置されましたが、まだ存在しているため、月全体で全長は4000
です。同じPT1: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 

そして、はい、引数の前にあるこれらのコロン (開始日と終了日に切り替えました... 私にとってより簡単になり、6 か月のレポートを作成できるようになりました) は、データウィンドウを利用していることを意味します。(誰かが上記の概念を変更して純粋な SQL アプローチを作成できると確信していますが、私が知っていることを使用します。)

SQL をデータウィンドウ (私はフリーフォームを使用) にロードし、必要に応じてクライアント側で type と date_of_change (ベルトとサスペンダー) でソートを設定します。データ セットには、クエリ範囲の終了の翌日の変更として null の削除された日付が含まれているため、これらの 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)

詳細バンド内のすべてのコントロールを選択し、次の Visible 式を指定します。

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

次の行に同じ date_of_change と type がある場合、それらは非表示になります。今日のすべてのアクティビティの合計を最後の行に表示するだけです。

詳細バンドを高さ 0 までドラッグし、詳細バンドの高さを自動調整します。

それはあなたが求めているもののバージョンをあなたに与えるでしょう。FWIW、レポートを日付で並べ替えようとしましたが(それがあなたが望んでいたものなのか、最初にタイプで並べ替えられたのかは不明です)、CumulativeSum()機能が壊れています。多分他の誰かがそれを理解することができます。

幸運を、

テリー。

PS SOがこれだけ入れさせてくれるなら、これが私のプロトタイプのエクスポートです。あなたにとって役に立つかもしれないし、役に立たないかもしれません。

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

2 月 (日付は DDMMYYY 形式) では、PT1 の長さは負の数である必要があるということですか? その年の第 1 四半期のレポートを実行すると、2 月に削除された 2000 フィートが差し引かれます。 1 月に設置された 2400 フィートの PT1 から、この四半期に PT1 が設置された正味の長さは 400 フィートになりますか? その場合、2 つのインライン ビューまたは永続ビューの交差としてこれを行うことができます。1 つはインストール用で、もう 1 つは削除用です。タイプと (抽出された) 期間が結合列であり、タイプごとのインストール済みの長さから期間ごとのタイプごとの削除された長さを差し引きます。 per period を使用して、期間ごとのタイプごとの正味設置長さを取得します。インストールと削除の間の左結合。

必ずしも会社によってインストールされたわけではないパイプの削除も追跡する必要がある場合 (たとえば、PT77 が削除されたという記録があるが、PT77 がインストールされたという記録がない場合)、これを 2 つのインライン ビューまたは永続ビューの UNION として扱うことができます。 、1 つはインストール用、もう 1 つは削除用で、削除は負の長さにキャストされます。次に、その UNION ALL クエリ自体をインライン ビューにします。そこでは、タイプごとにグループ化し、長さを合計します。

于 2011-04-12T11:32:59.510 に答える
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 パイプの量は表示されません。

次のクエリは、毎月設置されたパイプの全長をタイプごとに 1 行で表示します。

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 に答える