分析查询

时间:2015-01-26 06:21:02

标签: sql oracle

我正在处理一个旧项目,我需要做一些改变。我在c#代码中遇到了一个查询,我无法理解它是如何工作的。

这是查询:

select 
   case 
      when f.charge_status_id =3 or f.charge_status_id =4 then 'sold' 
      when f.charge_status_id =2 then 'reserved'
      when f.charge_status_id =1 then 'remaining '
   End,
   f.mobile_company_type_id, f.company_batchid,
   sum(f.countt)  
from 
   (select  
       t.mobile_company_type_id, t.company_batchid, t.charge_status_id,
       count(t.serial_no) as countt
    from 
       css_charge_codes t 
    where 
       t.company_batchid <> '0'
    group by 
       t.mobile_company_type_id, t.company_batchid, t.charge_status_id) f 
group by 
    charge_status_id, f.mobile_company_type_id, f.company_batchid
order by 
    f.mobile_company_type_id, f.company_batchid, f.charge_status_id

和表格#34; css_charge_codes&#34;是这样的:

Mobile_company_type_Id,serial_no,amount,file_number,charge_code,    
help_desk,PAN,charge_status_ID,batch_ID,Company_batchID

当我使用PlSql运行此查询时,它返回一个包含4列的表:

   CASEWHENF.CHARGE_STATUS_ID=3OR,MOBILE_COMPANY_TYPE_ID,batch_ID,SUM(F.COUNTT)

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

“f”和“t”是数据源的别名。你也可以写“as f”或“as t”。

“f”用于获取子查询的引用,“t”只是数据源css_charge_codes的别名。

  count(t.serial_no) as countt

这会对查询中的所有searial_nos进行计数,并在不同的组中为结果提供别名“countt”(聚合函数):

group by 
charge_status_id, f.mobile_company_type_id, f.company_batchid

然后可以从外部查询=&gt;引用此“countt”。 “f.countt”

答案 1 :(得分:0)

countt是此sql结果的别名:

 select  
   t.mobile_company_type_id, t.company_batchid, t.charge_status_id,
   count(t.serial_no)

t是表格css_charge_codes

的别名

f是此sql结果的别名:

select  
   t.mobile_company_type_id, t.company_batchid, t.charge_status_id,
   count(t.serial_no) as countt
from 
   css_charge_codes t 
where 
   t.company_batchid <> '0'
group by 
   t.mobile_company_type_id, t.company_batchid, t.charge_status_id