iSQL 查询多次返回值

iSQL Query Returning Value Multiple Times

我的列“SettlementsPayable”的输出返回每个行项目的值,即使这不是我想要的结果。它应该只在 ratecode = SHUNTING 时返回值 也许这就是我将数据放入我的声明中的方式?我在 iSQL

中包含了我的数据结果的屏幕截图
     SELECT      ds_id AS TMP, ds_ship_date AS ShipDate, ds_ref1_text AS ContainerNumber,  

        (CASE ds_status WHEN 'A' THEN 'TEMPLATE'  

WHEN 'C' THEN 'CANCELLED'  

WHEN 'D' THEN 'DECLINED'    

WHEN 'E' THEN 'QUOTED'  

WHEN 'F' THEN 'OFFERED'   

WHEN 'H' THEN 'PENDING'  

WHEN 'K' THEN 'OPEN'  

WHEN 'N' THEN 'AUTHORIZED'  

WHEN 'Q' THEN 'AUDIT REQUIRED'  

WHEN 'T' THEN 'AUDITED'  

WHEN 'W' THEN 'BILLED'   

END) AS 'TMPStatus',  

        b.co_name as "BillTo", o.co_name AS Origin, o.co_city AS OriginCity, o.co_state AS 
OriginState,  

        de_arrdate AS DeliveryDate, de_arrtime AS ArrivalTime, de_deptime AS DepartureTime,    

        dba.disp_items.di_qty AS QTY, dba.disp_items.ratecodename AS RateCode, 
dba.disp_items.di_our_rate AS OURRATE, dba.disp_items.di_our_itemamt AS ITEMAMT, 
dba.disp_items.amounttype AS AMTTYPE, dba.disp_items.di_pay_itemamt AS CarrierPayables,  

( select sum ( amount ) from dba.amountowed where DBA.AmountOwed.Description = 'SHUNTING' and 
string ( ds_id ) = amountowed.shipment ) AS SettlementsPayable,  

        CASE ds_ship_type   

                WHEN '2201' THEN 'MONTREAL'   

                WHEN '2202' THEN 'DRYVAN'   

                WHEN '2203' THEN 'BROKERAGE'   

                WHEN '2204' THEN 'OLD BROKERAGE'   

                WHEN '2205' THEN 'LIFTING'   

                WHEN '2206' THEN 'WAREHOUSE'  
        END   

        AS Division  

FROM        dba.disp_ship   

JOIN        dba.disp_events ON de_shipment_id = ds_id   

JOIN        dba.disp_items ON dba.disp_items.di_shipment_id = dba.disp_ship.ds_id 

JOIN        dba.companies o ON o.co_id = ds_origin_id    

JOIN        dba.companies b on b.co_id = ds_billto_id   

WHERE       de_site = ds_findest_id   

AND de_event_type IN ('D','R','N')

and ds_id = '82261'

GROUP BY TMP, SHIPDATE, CONTAINERNUMBER, TMPSTATUS, BILLTO, ORIGIN, ORIGINCITY, ORIGINSTATE, 
DELIVERYDATE, ARRIVALTIME, DEPARTURETIME, QTY, RATECODE, OURRATE, ITEMAMT, AMTTYPE, 
CARRIERPAYABLES, SETTLEMENTSPAYABLE, DIVISION 

ORDER BY SETTLEMENTSPAYABLE DESC  

结果:

子查询有一个 where 子句,用于查找具有 DBA.AmountOwed.Description = 'SHUNTING' ...

的行
( select sum ( amount ) from dba.amountowed where DBA.AmountOwed.Description = 'SHUNTING' and
string ( ds_id ) = amountowed.shipment ) AS SettlementsPayable,  

... 但我在父查询中看不到任何地方,您可以将结果限制为 SHUNTING 的行(显示 RateCode 值的图像支持 PORTRATESSHUNTING) 所以我猜您真正想看到的是 SettlementsPayable 列下的值,该值以 [=15 中的值为条件=]列。

假设:

    如果父行有 RateCode = 'SHUNTING'
  • SettlementsPayable 应该显示 sum(result),否则显示 0.00(当 RateCode != 'SHUNTING'
  • 10.00 的当前 SettlementsPayable 值是 RateCode = 'SHUNTING' 行的 正确 值(即,没有逻辑问题当前子查询)
  • string() 不是 Sybase ASE 中的有效(系统提供的)函数,所以我猜 OP 正在使用 Sybase SQLAnywhereSybase IQ;反过来,我认为我已经为 case 语句
  • SQLAnywhere/IQ 版本找到了正确的语法

使用 case 语句的一个想法:

select ... snip ...
       dba.disp_items.ratecodename AS RateCode,             # no change here, just displaying so we know where 'RateCode' comes from
       ... snip ...

       case when dba.disp_items.ratecodename != 'SHUNTING' then 0.00

            else (select sum(amount) 
                  from   dba.amountowed
                  where  DBA.AmountOwed.Description = 'SHUNTING'
                  and    string(ds_id) = amountowed.shipment)

       end case AS SettlementsPayable, 

       ... snip ...
from   ... snip ...