線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1944
推到 Plurk!
推到 Facebook!

在特定時間自動記錄,及計算某時間區段的量

答題得分者是:小傑克
g9614721
一般會員


發表:27
回覆:51
積分:15
註冊:2008-07-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-03-28 12:48:29 IP:140.125.xxx.xxx 訂閱
我希望得到每日最後的庫存量,且能取某天至某天之間的庫存量來做平均。

可是卻不知道怎麼做...

因為程式並沒有天天開啟

也沒有一直運作

我目前會算好庫存量

但不知道如何在程式啟動時

將前幾天程式沒運作的庫存量寫入資料表

例如:
3/1 庫存400
3/1 12:00 進貨600 庫存1000
3/1 14:00 領用100 庫存900
3/2 13:00 進貨500 庫存1400
3/3 程式未啟動 庫存1400
3/4 程式未啟動 庫存1400
3/5 17:00 領用300 庫存 1100

當我選擇 datetimepicer1.date(3/1) 至 datetimepicker2.date(3/5)
該怎麼去抓取3/1 的 900
及自動紀錄3/3,3/4 的 1400
或是有什麼更好的方法嗎?


可以計算
3/1 900
3/2 1400
3/3 1400
3/4 1400
3/5 1100
-----------------
6200 除於5 =1240



IN

Table1.Append;
Table1.FieldByName('日期').Asdatetime:=DateTimePicker1.Date;
Table1.FieldByName('物品名稱').AsString:=edit1.Text;
Table1.FieldByName('貯存地點').AsString:=edit3.Text;
Table1.FieldByName('進貨數量').AsString:=edit5.Text;
table1.post;

OUT

Table2.Append;
Table2.FieldByName('日期').Asdatetime:=DateTimePicker1.Date;
Table2.FieldByName('物品名稱').AsString:=edit1.Text;
Table2.FieldByName('領用地點').AsString:=edit3.Text;
Table2.FieldByName('領用數量').AsString:=edit5.Text;
Tble2.post;

庫存量

form1.Query3.SQL.Add('Select sum(進貨數量) as 進貨累計量 from A_IN where 物品名稱="' dblookupcombobox1.text '" and 貯存地點="' edit3.text '"');
form1.Query4.SQL.Add('Select sum(領用數量) as 領用累計量 from A_out where 物品名稱="' dblookupcombobox1.text '" and 領用地點="' edit3.text '"');
label8.caption:= formatfloat('#########0',query1.fieldbyname('進貨累計量').asfloat-query2.fieldbyname('領用累計量').asfloat);
table3.append;
table3.fieldbyname('庫存量').asstring:=label8.caption;
table3.post;
編輯記錄
g9614721 重新編輯於 2009-03-28 12:59:46, 註解 無‧
g9614721 重新編輯於 2009-03-28 13:23:47, 註解 無‧
st33chen
尊榮會員


發表:15
回覆:591
積分:1201
註冊:2005-09-30

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-03-29 09:06:25 IP:122.116.xxx.xxx 未訂閱
您好,

這題可能用 sotred procedure 去做會比較直觀.
( 先用變數放剛剛處理的日期(Ldate)及庫存量(Lqty),
每處理一筆即先比較本筆日期與Ldate 間是否連續, 若不連續則 以 Lqty 為庫存量 insert 這些不連續日
其餘應該會處理了吧)

如果要用 sql statement 解決, 那可能和 db server 有關, 若為 oracle 則可用 last_value, 參考一下

http://blog.xuite.net/st33chen/oracle/21917683


------
IS IT WHAT IT IS
我是 李慕白 請倒著唸.
又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦);
都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲.
g9614721
一般會員


發表:27
回覆:51
積分:15
註冊:2008-07-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-03-29 14:26:35 IP:220.131.xxx.xxx 訂閱
st33chen 您好
我不太懂 sotred procedure
但我看了板上一些日期的計算的相關文章
想了一個辦法,不知是否可行
關於如何補上沒有運作的那天的庫存量
var
MyString1,MyString2 :String;
MyDate1, MyDate2 ,MYDATE3: TDate;
begin
MyString1 := Edit1.Text; //表單啟動的日期
MyString2 := dbEdit2.Text; //資料表內的日期
MyDate1 := StrToDate(MyString1);
MyDate2 := StrToDate(MyString2);
if mydate1 > mydate2 then begin
mydate3:=mydate2 1 ;
table1.Append;
table1.FieldByName('日期').AsDateTime:=mydate3 ;
table1.FieldByName('物品名稱').AsDateTime:=.......... ;
table1.FieldByName('儲存地點').AsDateTime:=.......... ;
table1.FieldByName('庫存量').AsDateTime:=.............. ;
table1.Post; (請問這邊要如何讓他再回到 if mydate1>mydate2)
end
else if mydate1=mydate2 then
showmessage('日期相同');
end;

另外關於日期的加減...
var
mydate1,mydate2,mydate3:Tdate;
假設我的
mydate1 是 2009/3/30
mydate2 是 2009/3/29
mydate3:=mydate1-mydate2
ShowMessage(DateToStr(MyDate3));
show出來的訊息怎麼會變成 1899/12/31 = =?



===================引 用 st33chen 文 章===================
您好,

這題可能用 sotred procedure 去做會比較直觀.
( 先用變數放剛剛處理的日期(Ldate)及庫存量(Lqty),
每處理一筆即先比較本筆日期與Ldate 間是否連續, 若不連續則 以 Lqty 為庫存量 insert 這些不連續日
其餘應該會處理了吧)

如果要用 sql statement 解決, 那可能和 db server 有關, 若為 oracle 則可用 last_value, 參考一下

http://blog.xuite.net/st33chen/oracle/21917683
編輯記錄
g9614721 重新編輯於 2009-03-30 02:06:14, 註解 無‧
g9614721 重新編輯於 2009-03-30 02:08:49, 註解 無‧
小傑克
資深會員


發表:5
回覆:209
積分:357
註冊:2009-02-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-03-30 13:09:18 IP:59.112.xxx.xxx 訂閱
您好
因為在 delphi 中日期型態也是一個數字 , 如果 Date1 := now; Date1 1 就是明天, Date1 -1 就是昨天, 詳情看日期型態的說明
也就是說如果 FormatDateTime ('yyyymmdd' , Date ) = '20090330' 那麼 FormatDateTime ('yyyymmdd' , Date 2 ) = '20090401'
EncodeDate 可以把字串再組成日期, 用法請看help 不贅述
您可以先找出最後資料日期 和 今天 各自轉成 TDatetime 型態,然後相減 就可以知道兩個相差幾天, 該補多少資料
var i:integer;
D1,D2 :TDateTime;

// 假設您已經找出D2, D1 , D2 > D1
For i := 0 to (D2-D1) do
begin
ShowMessage( FormatDateTime('yyyymmdd' , D1 i) );
//變成 1899/12/31 是因為兩個日期相減得到的是日期的差,還要加回原來的Date1 才能得到您要的結果
// 假設D2 是 2009/04/01 D1 是 2009/03/30 D2 - D1 是 2 實際上 20090330在delphi中是 39902 , 04/01 是 39904
end;
------
額有朝天骨,眼中有靈光
編輯記錄
小傑克 重新編輯於 2009-03-30 13:21:12, 註解 無‧
g9614721
一般會員


發表:27
回覆:51
積分:15
註冊:2008-07-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-03-30 13:55:47 IP:140.125.xxx.xxx 訂閱
For i := 0 to (D2-D1) do 有紅字[Error] Unit1.pas(64): Incompatible types: 'Integer' and 'Extended'

所以我d2-d1=2 而秀出來 1899/12/01 是因為 delphi 內建的第一天 是1899/12/01 是嗎?




==============引 用 小傑克 文 章===================
您好
因為在 delphi 中日期型態也是一個數字 , 如果 Date1 := now; Date1 1 就是明天, Date1 -1 就是昨天, 詳情看日期型態的說明
也就是說如果 FormatDateTime ('yyyymmdd' , Date ) = '20090330' 那麼 FormatDateTime ('yyyymmdd' , Date 2 ) = '20090401'
EncodeDate 可以把字串再組成日期, 用法請看help 不贅述
您可以先找出最後資料日期 和 今天 各自轉成 TDatetime 型態,然後相減 就可以知道兩個相差幾天, 該補多少資料
var i:integer;
D1,D2 :TDateTime;

// 假設您已經找出D2, D1 , D2 > D1
For i := 0 to (D2-D1) do
begin
ShowMessage( FormatDateTime('yyyymmdd' , D1 i) );
//變成 1899/12/31 是因為兩個日期相減得到的是日期的差,還要加回原來的Date1 才能得到您要的結果
// 假設D2 是 2009/04/01 D1 是 2009/03/30 D2 - D1 是 2 實際上 20090330在delphi中是 39902 , 04/01 是 39904
end;
小傑克
資深會員


發表:5
回覆:209
積分:357
註冊:2009-02-16

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-03-30 14:22:46 IP:59.112.xxx.xxx 訂閱
你猜的沒錯, 日期是個實數, 整數部分是天, 小數部分是 天的百分比,比如說 0.3 表示 0.3 天就是 24小時 * 0.3 的意義
所以 D2-D1 是實數, 可以取Round變整數

Following are some examples of TDateTime values and their corresponding dates and times:
0 12/30/1899 12:00 am
2.75 1/1/1900 6:00 pm
-1.25 12/29/1899 6:00 am
35065 1/1/1996 12:00 am
To find the fractional number of days between two dates, simply subtract the two values, unless one of the TDateTime values is negative. Similarly, to increment a date and time value by a certain fractional number of days, add the fractional number to the date and time value if the TDateTime value is positive.
When working with negative TDateTime values, computations must handle time portion separately. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the TDateTime value. For example, 6:00 am on 12/29/1899 is ?.25, not ? 0.25, which would be ?.75. There are no TDateTime values between ? and 0.
Note: Delphi 1.0 calculated the date from year 1 instead of from 1899. To convert a Delphi 1.0 date to a TDateTime value in later versions of the Delphi language, subtract 693594.0 from the Delphi 1.0 date.
------
額有朝天骨,眼中有靈光
g9614721
一般會員


發表:27
回覆:51
積分:15
註冊:2008-07-23

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-03-30 14:42:10 IP:140.125.xxx.xxx 訂閱
var
MyString1,MyString2 :String;
MyDate1, MyDate2 : TDatetime;
i,MYDATE3:integer;
begin
MyString1 := dbEdit1.Text; //表單啟動的日期
MyString2 := dbEdit2.Text; //資料表內的日期
MyDate1 := StrToDate(MyString1);
MyDate2 := StrToDate(MyString2);
if mydate1 > mydate2 then
mydate3:=strtoint(formatdatetime('yyyymmdd',MYDATE1))-strtoint(formatdatetime('yyyymmdd',MYDATE2));;
For I:= 0 to mydate3 do
begin
table2.Append;
table2.FieldByName('日期').asdatetime:=mydate2 1;
table2.post;

可以運算了!!
可是= =假設
mydate1 是2009/3/25
mydate2 是2009/3/30

跑出來的結果是 新增了5筆 2009/3/26...那A安捏

我要的是
3/26
3/27
3/28
3/29
3/30 阿~

編輯記錄
g9614721 重新編輯於 2009-03-30 15:07:34, 註解 無‧
小傑克
資深會員


發表:5
回覆:209
積分:357
註冊:2009-02-16

發送簡訊給我
#8 引用回覆 回覆 發表時間:2009-03-30 15:38:44 IP:59.112.xxx.xxx 訂閱
oh ~ my god 你還真粗心啊 , 是+ i 不是加1 

===================引 用 g9614721 文 章===================
var
MyString1,MyString2 :String;
MyDate1, MyDate2 : TDatetime;
i,MYDATE3:integer;
begin
MyString1 := dbEdit1.Text; //表單啟動的日期
MyString2 := dbEdit2.Text; //資料表內的日期
MyDate1 := StrToDate(MyString1);
MyDate2 := StrToDate(MyString2);
if mydate1 > mydate2 then
mydate3:=strtoint(formatdatetime('yyyymmdd',MYDATE1))-strtoint(formatdatetime('yyyymmdd',MYDATE2));;
For I:= 0 to mydate3 do
begin
table2.Append;
table2.FieldByName('日期').asdatetime:=mydate2 1;
table2.post;

可以運算了!!
可是= =假設
mydate1 是2009/3/25
mydate2 是2009/3/30

跑出來的結果是 新增了5筆 2009/3/26...那A安捏

我要的是
3/26
3/27
3/28
3/29
3/30 阿~

------
額有朝天骨,眼中有靈光
g9614721
一般會員


發表:27
回覆:51
積分:15
註冊:2008-07-23

發送簡訊給我
#9 引用回覆 回覆 發表時間:2009-03-30 16:11:46 IP:140.125.xxx.xxx 訂閱
謝謝 小傑克^^
真是幫了我ㄧ個大忙^^3Q
系統時間:2024-05-18 14:09:29
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!