当前位置:主页>delphi源码/MIS软件> 文章内容

怎么在程序中改变表中字段的名称!

发布时间:2010-01-20 | QQ免费站
1楼: 各表说明如下: 1、商品数据文件,表名:Product MID 商品编号 varchar20= MName 商品名称 100= Mcost 成本 float8 Price 价格 float8 2、 订货单表,表名:Orders MID (自动增长主键) OrderID 订货单单号 10= CustomerID 客户编号 10= MDate 订货日期 8= MItem 项次 int4= ProductID 商品编号 varchar 20= Quantity 订货数量 int4= 3、 出仓单主表,表名:Delivery MID (自动增长主键) DeliveryID 出仓单单号 10= CustomerID 客户编号 10= MDate 出仓日期 8= MItem 项次 int4= ProductID 商品编号 varchar 20= Price 单价 Float8= Quantity 数量 int4= Moneys 金额 Float8= 我的目标是查询出某个客户在某段时间内的所订过的所有商品总数、出货总数、及未出货数。 如: 编码 名称 订量 已出 未出 23044020 风影 250M绿 2.0# 100 100 0 23044025 风影 250M绿 2.5# 100 50 50 23044030 风影 250M绿 3.0# 200 50 150 23044035 风影 250M绿 3.5# 200 50 150 23044040 风影 250M绿 4.0# 50 50 0 23044050 风影 250M绿 5.0# 50 0 50 23044060 风影 250M绿 6.0# 50 0 50 我用的查询语句是: (我是业务水平,只能写出这样的来,这句话查出的结果一定是错的) select O.ProductID as 编码,P.Mname as 名称,sum(O.Quantity) as 订量, sum(D.Quantity) as 已出, sum(O.Quantity-D.Quantity) as 未出 from Delivery D Right join (Orders O left join Product P on P.MId=O.ProductID) on D.ProductID=O.ProductID where O.MDate>=‘2005/1/1‘ and O.MDate<=‘2005/3/31‘and D.MDate>=‘2005/1/1‘ and D.MDate<=‘2005/3/31‘ and O.CustomerID =‘L99805‘and D.CustomerID =‘L99805‘ group by O.ProductID,P.Mname 现请求会写的同志帮我改一下怎么写才是正确的。 字串1

2楼: 我覺得你可以建一個臨時表分幾步算,不用一個sql語句就搞出來, 一個語句,又麻煩,又容易出錯,執行效率也不怎麼高 字串8

3楼: 首先你是不是有一个用户表,没有建议建一个, Tclient(表名) ,code(编码),name(名称) 语句如下: select code,Mname,sum(c.Quantity), sum(d.Quantity),(sum(c.Quantity)-sum(d.Quantity)) as z from Product as a,Tclient as b, Orders c,Delivery d where b.code=c.CustomerID and b.code=d.CustomerID and a.MID=c.ProductID and a.mid=d.productid and c.mdate>=‘2002-02-02‘and c.mdate>=‘2005-02-02‘ and d.mdate>=‘2002-02-02‘and d.mdate>=‘2005-02-02‘ group by code,mname
字串3

4楼: 用户=客户

字串3

5楼: select code,Mname,sum(c.Quantity), sum(d.Quantity),(sum(c.Quantity)-sum(d.Quantity)) as z from Product as a,Tclient as b, Orders c,Delivery d where b.code=c.CustomerID and b.code=d.CustomerID and a.MID=c.ProductID and a.mid=d.productid and c.mdate>=‘2002-02-02‘and c.mdate<=‘2005-02-02‘ and d.mdate>=‘2002-02-02‘and d.mdate<=‘2005-02-02‘ group by code,mname
字串3

6楼: select pd.MID as 商品编号,pd.MName as 商品名称,od.订量,dl.已出,(od.订量-dl.已出) as 未出 from Product left join (select ProductID,Sum(Quantity) as 订量 from Orders where (MDate between ‘2004-1-1‘ and ‘2004-5-1‘) and (CustomerID = 001) Group By ProductID) as od on pd.MID = od.ProductID left join (select ProductID,Sum(Quantity) as 已出 from Delivery where (MDate between ‘2004-1-1‘ and ‘2004-5-1‘) and (CustomerID = 001) Group By ProductID ) as dl on pd.MID = dl.ProductID
字串8

7楼: 我的做法:建两个临时储存过程: 1、Orders汇总(o1) SELECT Product.MID, Product.MName, Sum(Orders.Quantity) AS Qua FROM Orders INNER JOIN Product ON Orders.ProductID = Product.MID WHERE (Delivery.MDate Between #4/1/2005# And #4/5/2005#) and (CustomerID =“L99805“) GROUP BY Product.MID, Product.MName; 2、Delivery汇总(d1) SELECT Product.MID, Product.MName, Sum(Delivery.Quantity) AS Qua FROM Delivery INNER JOIN Product ON Delivery.ProductID = Product.MID WHERE (Delivery.MDate Between #4/1/2005# And #4/5/2005#) and (CustomerID =“L99805“) GROUP BY Product.MID, Product.MName; 3、 SELECT d1.MID as 编号, O1.MName as 名称 , O1.Qua AS 订量, d1.Qua AS 已出, [o1.Qua]-[d1.Qua] AS 未出 FROM d1 INNER JOIN O1 ON d1.MID = O1.MID; 在Access 2000 测试通过。

字串2

8楼: 此语句未加null判定 如果要更精细一点,把第一行改为: select pd.MID as 商品编号,pd.MName as 商品名称, (case when od.订量 is null then 0 else od.订量 end ) as 订量 (case when dl.已出 is null then 0 else dl.已出 end ) as 已出 (case when (od.订量 is null) and (dl.已出 is null) then 0 when (not od.订量 is null) and (dl.已出 is null) then od.订量 else od.订量-dl.已出 end) as 未出 字串1

9楼: TO qiangyao: 客户表已经有了。 O.CustomerID =‘L99805‘就是指ID为L99805的客户。 TO single: 你上面的它说有误法错误呀,错误如下: Server: Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword ‘left‘. Server: Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword ‘as‘. Server: Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword ‘as‘.

字串4

10楼: qiangyao和dawnsoft的做法可能会造成统计结果的遗漏。 字串9

11楼: lovewh119同学,不要光顾着copy,你有注意我写的第一行有一个逗号是全角的吗? 请帮我修正 字串5

12楼: 怎麼這麼多人喜歡直接產生數據阿, 又慢,設計又麻煩,又容易出錯,執行效率又不行 沒人支持我用臨時表的麼

字串4

13楼: qiangyao的一看就知道查出的数量会变多起来。 dawnsoft的还没试,不过结果应该能正确,只是我不想用存储过程所以求SQL语句。 single的正在测试,因为太复杂,看得不是很懂。可以的话帮看一下你给你语句里可能有哪里的语法有错。 Select * 完没有From表名之类的就直接left join好象不对哦。

字串5

14楼: to victor_zh: 别看我是用一句SQL实现的,但是,其实在此SQL运行过程中,后台已经生成两个临时表。 形如join (select * from aaa)的语句形式都是对结果集的二次处理,其实后台早有临时表生成。这种方法比你显式的使用临时表,自然要高效的多
字串3

15楼: to lovewh119: 我不知道你使用的是什么数据库,对SQL 92的支持到一个什么程度 我建议你在你的数据库里执行如下的测试语句 select * from (select * from aaa) as a aaa是一个任意的数据库,如果执行失败,那说明你的数据库不支持我写的组合SQL语句。 那你就只能乖乖的听victor_zh的话显示使用临时表

字串6

16楼: 组合后的最新语句应该是这样: select pd.MID as 商品编号,pd.MName as 商品名称, (case when od.订量 is null then 0 else od.订量 end ) as 订量 (case when dl.已出 is null then 0 else dl.已出 end ) as 已出 (case when (od.订量 is null) and (dl.已出 is null) then 0 when (not od.订量 is null) and (dl.已出 is null) then od.订量 else od.订量-dl.已出 end) as 未出 from Product pd left join (select ProductID,Sum(Quantity) as 订量 from Orders where (MDate between ‘2004-1-1‘ and ‘2004-5-1‘) and (CustomerID = 001) Group By ProductID) as od on pd.MID = od.ProductID left join (select ProductID,Sum(Quantity) as 已出 from Delivery where (MDate between ‘2004-1-1‘ and ‘2004-5-1‘) and (CustomerID = 001) Group By ProductID ) as dl on pd.MID = dl.ProductID

字串6

17楼: to single 你的那種join沒用過,正好學習一下,hehe to lovewh119 本來想寫個臨時表的方法給你,但是發現你數據庫構建的有問題,我寫了一半就寫不出來了 訂單和出倉單沒有辦法聯繫起來,你的訂單和出倉單是怎麼聯繫的阿 客戶=客戶 and 產品=產品? 這個客戶如果定了好幾張訂單,都有同樣的東西呢? 至少應該把訂單號碼,帶入出倉單吧 字串2

18楼: single:  呵呵,我当然知道有一个逗号啦。你看我上面写的报错没有以下这句话: Server: Msg 170, Level 15, State 1, Line 1 Line 1: Incorrect syntax near ‘,‘. 现测试你的说法。

字串5

19楼: to victor_zh 订单和出仓单确实没有办法联系起来,但是,事实上他们也不需要互相联起来 因为他们只要都和Product这张表联系起来就可以了,通过ProductID键,加油 字串3

20楼: to lovewh119 不好意思,前面在写的时候漏写了From Product pd这一行 请参见ID:3025017
字串2

21楼: ID:3025017里,在第二和第三行各加一个逗号后,语法检测正确,但运行起来报错: Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value ‘SBA0001‘ to a column of data type int. 我还以为是有中文不太对,所以把所有中文改成英文后,还是报这个错。 附,我用的是Sql Server 2000英文版。
字串6

22楼: to single 你的那種方法我消化了,不錯,道理和臨時表一樣,寫起來更方便,但是效率沒有試過,有機會試試 你說得關聯的我也知道了,對於他這個統計沒有問題,但總覺得他這種設計不妥,哪天要統計哪個訂單出多少的時候看他怎麼辦,hehe 字串9

23楼: 可以用下面的一条语句 SELECT X.Customerid as 客户编号, X.MID as 产品编号, Y.MName as 产品名称, X.OrderSum as 订量, X.OutSum as 已出, X.NotSum as 未出 FROM (select a.customerid,a.ProductID,isnull(a.ordersum,0) as ordersum, isnull(b.outsum ,0) as outsum , isnull(a.ordersum,0)-isnull(b.outsum ,0) as NotSum , from (select customerid,ProductID,SUM(Quantity) as orderSum from Orders group by customerid,ProductID) a left outer join (select customerid,ProductID,SUM(Quantity) as OutSum from Delivery group by customerid,ProductID) b on b.customerid=a.customerid and b.ProductID=ProductID ) X, Product Y WHERE y.MID=X.ProductID 也可以分成下面几个视图: view1 :客户每种产品订量视图 create view view_Orders as select customerid,ProductID,SUM(Quantity) as orderSum from Orders group by customerid,ProductID view2:客户每种产品出货视图: create view view_Delivery as select customerid,ProductID,SUM(Quantity) as OutSum from Delivery group by customerid,ProductID view3:客户定单情况视图,反映各个客户的定单定量及已出未出情况 Create view view_CustomerList as select a.customerid,a.ProductID,isnull(a.ordersum,0) as ordersum, isnull(b.outsum ,0) as outsum , isnull(a.ordersum,0)-isnull(b.outsum ,0) as NotSum , from view_Orders a left outer join view_Delivery b on b.customerid=a.customerid and b.ProductID=a.ProductID view4:总视图 SELECT A.Customerid as 客户编号, A.MID as 产品编号, B.MName as 产品名称, A.OrderSum as 订量, A.OutSum as 已出, ANotSum as 未出 FROM view_CustomerList A,Product b where b.MID=A.ProductID and a.CustomerID=‘‘ 字串9

24楼: TO victor_zh: 我原来是有把订单号码带入出仓单,而订单表里是有一个字段是记录已出货数量。 但当年我刚学编程,而且整个程序只我一个人在没人教的情况下写,没有考虑到太多。 所以出了个错误,那就是如它订200,第一次出了40时,我在出仓数量里记下40,第二次出仓出30时,那里应该是70的我却是记为30,公司在刚用的时候基本都是一次出货搞定,也没发现,只是现在久了,才发现未出货数不对。而又不可能一笔笔记录的去修改,所以,我才想出要找这么一条查询语句的。谢谢你的指点。 另外问一下你,你所说的订单号码带入出仓单怎么带? 因为他有的时候是第一次订了200,还没出,他又订了300,而它上个月订了500时只出了100,那现在一出就是出2+3+5-1=900给它。那你的“订单号码带入出仓单”是怎么处理这种情况的??呵呵,讨论一下嘛。 字串8

25楼: 终于看懂single写的句子了,懂了之后才觉得道理很简单。临时表的做法效率怎么样我就没有试过了。总之这种方法在我这个数据库里一点就出来了,应该不超过半秒时间。呵呵,我的产品表Product里有2万条以上记录,而订货表和出货表每天大约各增400条,现在用了一年多了应该是各有10万以上条记录,而且数据库不是本机而局域网里的另外一台电脑。看来这个速度不错。 本题正确答案如下: Select pd.MID as 编码,pd.MName as 名称, (case when od. MyOrder is null then 0 else od. MyOrder end ) as 订量, (case when dl. MyDelive is null then 0 else dl. MyDelive end ) as 已出, (case when (od. MyOrder is null) and (dl. MyDelive is null) then 0 when (not od. MyOrder is null) and (dl. MyDelive is null) then od. MyOrder else od. MyOrder -dl.MyDelive end) as 未出 from (select ProductID,Sum(Quantity) as MyOrder from Orders where (MDate between ‘2005/1/1‘ and‘2005/3/25‘) and (CustomerID =‘SW10015‘) Group By ProductID) as od left join Product pd on pd.MID = od.ProductID left join (select ProductID,Sum(Quantity) as MyDelive from Delivery where (MDate between ‘2005/1/1‘ and ‘2005/3/25‘) and (CustomerID =‘SW10015‘) Group By ProductID ) as dl on pd.MID = dl.ProductID order by pd.MID 我用Orders left join Product而不用Product left join Orders是因为我想没订过货的商品还是不要出现的好,嘻嘻,这样的话(case when od. MyOrder is null then 0 else od. MyOrder end )可能有些多余,不过我喜欢留着它。多谢single ! 本应结贴,但还想跟victor_zh讨论一下订单号码带入出仓单的问题,过两天再结吧,嘻嘻。 字串3

26楼: 呵呵,解决了就好 另外,象(case when od. MyOrder is null then 0 else od. MyOrder end ) 之类的语句,在SQL Server里可以写的更简单: isnull(od.MyOrder,0) 所以你的语句可以进一步简化
字串1

27楼: 好,谢谢你了!接受答案!
字串5