在水晶报告中生成报告,其中字段的计数放入列中

时间:2014-04-04 10:06:17

标签: sql oracle vb6 crystal-reports

我必须在Crystal报表和VB报表中生成一个报表,其中数据库是Oracle,如附带的屏幕截图:

enter image description here

我使用了以下查询并获得了如下屏幕截图的输出:

SELECT ins.ins, ins.ins_name, crdi.ct, crdi.bin,
(select count(*) from crdh where crd_st='CN') as CNcount, (select count(*) 
from crdh where crd_st='PO') as POcount
FROM crdh, crdi, ins
where crdh.bn=crdi.bn and crdi.ins=ins.ins and crdh.crd_st IN ('PO','CN')
GROUP BY ins.ins, crdi.bn, ins.ins_name,crdi
ORDER BY ins.ins, crdi.bn;

enter image description here

我有以下问题:

  1. 如何更正上述查询,以便它可以为crd_st分别为“CN”或“PO”的特定bn返回crd_st的计数。例如,这个查询应该告诉我,对于bn 123456,crd_st是PO的记录数是多少。
  2. 我是Crystal报告的新手。请帮助制作此报告文件和
  3. 还告诉我如何在此报告文件(.rpt)中为记录编号实施Sr No。
  4. 我运行了以下查询并获得了如下屏幕截图

    的输出
    SELECT ins.ins_name,ins.ins,
    crdi.crd_st, crdi.bin, crdh.crd_st, 
    COUNT(crdh.crd_st) as count
    FROM crdh, crdi, ins
    WHERE crdh.crd_st IN ('PO','CN') and crdi.bn in 
    (select unique bn from crdh)and crdh.bn=crdi.bn and 
    crdi.ins=ins.ins
    GROUP BY ins.ins, crdi.bn, ins.ins_name,
    crdi.crd_st,crdh.crd_st
    ORDER BY ins.ins, crdi.bn;
    

    enter image description here

    我想将count列显示为报告中各行bn的一行。

    我有Oracle 10g数据库并遵循以下链接

    link for pivote

    并进行以下查询并得到'ORA-00933:SQL命令未正确结束'错误。

    SELECT bn, CNCount, POCount
    FROM (
    SELECT bn, 
        crd_st, 
        ROW_NUMBER() OVER (PARTITION BY bn ORDER BY crd_st) AS cardRank
    FROM cardholder
    ) 
    pivot( count(crd_st) FOR cardRank IN ('CN' as CNCount, 'PO' as POCount));
    

    提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

由于你得到了一些输出,现在根据需要设计输出,使用水晶报告中的交叉表。

  1. 在列中使用bn
  2. 创建公式@Count并在该公式中只编写"Count"并在行中使用公式。
  3. 在汇总字段中使用数据库字段Count
  4. 让我知道它是怎么回事

    编辑---------------------------------------------- ----------------------------------------

    1. Drag the crosstab on to report.
    2. `Right click` on crosstab and go to `Crosstab expert`
    3. There you will find 3 options, `Rows`, `Columns` and `Summarized fileds`
    4. place the field what you want in row, place the filed what you want in column and same way place the column for summarized fields
    

答案 1 :(得分:1)

我通过运行以下查询获得了所需的输出:

SELECT ins.ins_name,ins.ins,crdi.ct, crdi.bn,
      sum(DECODE(cardh.crd_st, 'PO', 1, 0)) POCount,
      sum(DECODE(cardh.crd_st, 'CN', 1, 0)) CNCount
FROM crdh, crdi, ins
WHERE crdh.crd_st IN ('PO','CN') and crdi.bn in 
(select unique bn from crdh)and crdh.bn=crdi.bn and 
crdi.ins=ins.ins
GROUP BY ins.ins, crdi.bn, ins.ins_name,crdi.ct
ORDER BY ins.ins, crdi.bn;