質問

I have following tables:

weighment_tran
    village_cd
    farmer_id
    registered_farmer_id
    plot_no
    out_date
    net_wt

village_dir
    village_cd
    village_name
    taluka_cd
    district_cd

taluka_dir
    taluka_cd
    taluka_name

district_dir
    district_cd
    district_name

farmer_dir
    farmer_id
    first_name
    middle_name

agreement_tran
    farmer_id
    registered_farmer_id
    payment_farmer_id
    plot_no
    main_sy_no
    payment_bank_cd
    payment_account_no

bank_dir
    bank_cd
    bank_name
    bank_branch

Mainly I have two transaction tables namely agreement_tran (agreements are stored) and weighment_tran (weighment of products stored only those present in agreement_tran) and others are directories those holds lookup for actual names for codes like bank_cd looks for actual bank_name in bank_dir. farmer_id, registered_farmer_id, payment_farmer_id are of same column values. What I need is as below with out_date range:

Sl.No  farmer_name  Sy_nos  village  taluka  district  payment_farmer_id  payment_account_no  bank_name  bank_branch  sum(net_wt)

Every plot_no has a sy_no, I need to concatenate all sy_nos as I am selecting sum(net_wt) as net_wt concerned to every plot_no.

I tried something like-

select row_number() over (order by a.payment_farmer_id),
        a.payment_farmer_id, 
(select f.first_name ||' '|| f.middle_name as name
        from farmer_dir f 
        where a.payment_farmer_id=f.farmer_id),
(select wm_concat(main_sy_no) from agreement_tran a 
        where a.plot_no=w.plot_no),
(select sum(net_wt)
        from weighment_tran w
        where (w.plot_no = a.plot_no)
        and (w.season_cd = 9) and trunc(w.out_date) between 
        to_date('22-12-2013','dd-mm-yyyy') and to_date('23-12-2013','dd-mm-yyyy') 
        group by a.payment_farmer_id)
from agreement_tran a

but didn't get what I want.

役に立ちましたか?

解決 2

SELECT Sum(WEIGHMENT_TRAN.NET_WT), 
AGREEMENT_TRAN.PAYMENT_FARMER_ID, 
wm_concat(Distinct (agreement_tran.main_sy_no)) as Sy_no,
wm_concat(Distinct (village_dir.village_name)) as Village,
farmer_dir.first_name || ' ' || farmer_dir.middle_name as Farmer_name,     taluka_dir.taluka_name, district_dir.district_name, bank_dir.bank_name, bank_dir.bank_branch,  agreement_tran.payment_account_no, weighment_tran.registered_farmer_id
FROM AGRI.AGREEMENT_TRAN AGREEMENT_TRAN,
AGRI.village_dir village_dir, 
AGRI.WEIGHMENT_TRAN WEIGHMENT_TRAN, 
agri.farmer_dir farmer_dir, agri.taluka_dir taluka_dir, 
agri.district_dir district_dir, agri.bank_dir bank_dir
WHERE AGREEMENT_TRAN.PLOT_NO = WEIGHMENT_TRAN.PLOT_NO AND
((AGREEMENT_TRAN.SEASON_CD=9) AND (WEIGHMENT_TRAN.SEASON_CD=9) AND 
(trunc(weighment_tran.out_date) Between to_date('22-12-2013','dd-mm-yyyy')
And to_date('22-12-2013','dd-mm-yyyy')) AND
(AGREEMENT_TRAN.PLOT_NO=weighment_tran.plot_no) AND     (WEIGHMENT_TRAN.VILLAGE_CD=village_dir.village_cd)) and   agreement_tran.payment_farmer_id=farmer_dir.farmer_id and    village_dir.taluka_cd=taluka_dir.taluka_cd and    village_dir.district_cd=district_dir.district_cd and    agreement_tran.payment_bank_cd=bank_dir.bank_cd
GROUP BY AGREEMENT_TRAN.PAYMENT_FARMER_ID, farmer_dir.first_name, farmer_dir.middle_name, taluka_dir.taluka_name, district_dir.district_name,bank_dir.bank_name, bank_dir.bank_branch, agreement_tran.payment_account_no, weighment_tran.registered_farmer_id
Order by bank_dir.bank_name, bank_dir.bank_branch

finally I rewarded for my long effort and thanking @Thorsten for his time n patience.

他のヒント

There seem to be some mistakes in your query:

  1. First subquery looks okay.
  2. Second subquery is wrong. You select agreement_tran a second time but use the same alias. Moreover you compare with w.plot_no, but there is no w table. (I suppose you meant to name the inner agreement_tran w?)
  3. In your second subquery the group by clause makes no sense. Remove it.
  4. Is it on purpose that you don't sort your results? Above you number your lines, but at the end of the query you have no order by, which may result in a random order.

Your query shows all agreements, their farmer's name, all sy nos of all agreements with the same plot number, and the sum of all net weights with the same plot number in a certain time slot. Please check if this is meets your expectations. Do you really want to select all Agreements or rather all plots for instance?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top