计算 Firebird 3.0 中的不同记录

问题描述 投票:0回答:1

我有数据如下:

  nota | produto | descricao | vendida | total | ...
     1         A         AAA       100    1000
     1         B         BBB        20     200
     1         A         AAA       200    2000
     1         C         CCC        50     500
     2         A         AAA       500    5000
     3         Z         ZZZ       100    1000
     4         X         XXX       100    1000
     4         B         BBB      1000   10000

我正在做一个“produto”排名,“vendida”(数量)减少。

我的选择如下,效果完美:

  with a as (
     select
        trim(ni.cd_produto) cd_produto,
        trim(p.ds_produto) ds_produto,
        sum(ni.qt_vendida) qt_vendida,
        sum(ni.vr_totalliquido) vr_totapagar
     from
        notaitem ni
     inner join nota n on n.cd_empresa = 1 and
                          n.cd_filial = 1 and
                          n.nr_nota = ni.nr_nota and
                          n.nr_desdobramento = ni.nr_desdobramento
     inner join produto p on p.cd_produto = ni.cd_produto
     where
        n.fg_situacao = 'N' and
        n.fg_situacaonfe = '1' and
        n.cd_tipomovto = 1 and
        n.dt_emissao between :pidt_inicio and :pidt_final
     group by
        ni.cd_produto,
        p.ds_produto
     order by
        qt_vendida desc
     )
  select
     dense_rank() over(order by qt_vendida desc) nr_rank,
     cd_produto,
     ds_produto,
     qt_vendida,
     vr_totapagar,
     cast(round(100 * qt_vendida / sum(qt_vendida) over(), 4) as numeric(7, 4)) pc_participacaoqtd,
     cast(round(100 * vr_totapagar / sum(vr_totapagar) over(), 4) as numeric(7, 4)) pc_participacaovr
  from
     a
  order by
     nr_rank,
     ds_produto

利用CTE能够使用dense_rank,我可以完美过滤给定时间段内销售数量最多的商品。

初始示例的输出将是:

  nr_rank | cd_produto | ds_produto | qt_vendida | vr_totapagar | ...
        1            B          BBB         1020          10200
        2            A          AAA          800           8000
        3            X          XXX          100           1000
        4            Y          YYY          100           1000
        5            C          CCC           50            500

现在需要显示“produto”在不同“nota”中出现了多少次,并且它可能在同一个“nota”中出现多次,但如果发生这种情况,则应将其视为仅出现一次。

对于“produto”A,它在“nota”1中出现两次,在“nota”2中出现一次,因此它将出现在两个不同的“nota”中,外观为2。

新输出将具有以下格式:

  nr_rank | cd_produto | ds_produto | qt_vendida | vr_totapagar | appearance | ...
        1            B          BBB         1020          10200            2
        2            A          AAA          800           8000            2
        3            X          XXX          100           1000            1
        4            Y          YYY          100           1000            1
        5            C          CCC           50            500            1

我尝试使用 count(1),但正如预期的那样,它会计算所有行,因此,例如“produto”A 的外观将是 3 而不是 2。我也尝试了 count(distint),但所有“produto”A ” 的外观为 1。如下:

  with a as (
     select
        trim(ni.cd_produto) cd_produto,
        trim(p.ds_produto) ds_produto,
        sum(ni.qt_vendida) qt_vendida,
        sum(ni.vr_totalliquido) vr_totapagar,
        count(1) appearance
     from
        notaitem ni
     inner join nota n on n.cd_empresa = 1 and
                          n.cd_filial = 1 and
                          n.nr_nota = ni.nr_nota and
                          n.nr_desdobramento = ni.nr_desdobramento
     inner join produto p on p.cd_produto = ni.cd_produto
     where
        n.fg_situacao = 'N' and
        n.fg_situacaonfe = '1' and
        n.cd_tipomovto = 1 and
        n.dt_emissao between :pidt_inicio and :pidt_final
     group by
        ni.cd_produto,
        p.ds_produto
     order by
        qt_vendida desc
     )
  select
     dense_rank() over(order by qt_vendida desc) nr_rank,
     cd_produto,
     ds_produto,
     qt_vendida,
     vr_totapagar,
     appearance,
     cast(round(100 * qt_vendida / sum(qt_vendida) over(), 4) as numeric(7, 4)) pc_participacaoqtd,
     cast(round(100 * vr_totapagar / sum(vr_totapagar) over(), 4) as numeric(7, 4)) pc_participacaovr
  from
     a
  order by
     nr_rank,
     ds_produto
sql select firebird firebird-3.0
1个回答
0
投票

我相信您正在寻找的是

COUNT(DISTINCT <expression>)
。对于此特定查询,您需要
COUNT(DISTINCT nota)

© www.soinside.com 2019 - 2024. All rights reserved.