sql partiton путем подавления повторяющихся значений - PullRequest
0 голосов
/ 08 июня 2009

Использование SQL Server (2005/2008)

Есть ли простой способ подавить повторяющиеся промежуточные и общие итоги?

select h.salesmanno ,c.custname ,h.amt ,sum(h.amt) over (partition by salesmanno) as subtotal<br> ,sum(h.amt) over (partition by null) as grandtotal<br> from hdr h, cust c WHERE h.custno = c.custno and h.hdate = '6/8/2009'

в основном подавляет (или ноль), кроме последней позиции, до смены группы

Спасибо !!

Ответы [ 2 ]

1 голос
/ 08 июня 2009

Что это за база данных? На SQL Server что-то вроде этого будет делать:

select
  salesmanno, custname, amt
, case when subtotal_no = subtotal_count then subtotal end as subtotal
, case when total_no = total_count then subtotal end as total
from (
  select 
    h.salesmanno 
  , c.custname 
  , h.amt 
  , sum(h.amt) over (partition by salesmanno) as subtotal
  , sum(h.amt) over (partition by null) as grandtotal
  , row_number() over (partition by salesmanno order by custname) as subtotal_no
  , row_number() over (partition by null order by salesmanno, custname) as as total_no
  , count(*) over (parition by salesmanno) as subtotal_count
  , count(*) over (parition by null) as total_count
  from hdr h, cust c 
  WHERE h.custno = c.custno 
    and h.hdate = '6/8/2009'
  ) a
order by total_no

Более короткая версия, больше сортировки для базы данных, возможно, менее очевидно, что происходит:

select
  salesmanno, custname, amt
, case when subtotal_no = 1 then subtotal end as subtotal
, case when total_no = 1 then subtotal end as total
from (
  select 
    h.salesmanno 
  , c.custname 
  , h.amt 
  , sum(h.amt) over (partition by salesmanno) as subtotal
  , sum(h.amt) over (partition by null) as grandtotal
  , row_number() over (partition by salesmanno order by custname desc) as subtotal_no
  , row_number() over (partition by null order by salesmanno desc, custname desc) as as total_no
  from hdr h, cust c 
  WHERE h.custno = c.custno 
    and h.hdate = '6/8/2009'
  ) a
order by total_no desc

Альтернативно, используя ROLLUP для генерации промежуточных итогов и итоговых строк:

  select 
    h.salesmanno 
  , c.custname 
  , sum(h.amt) as amt
  from hdr h, cust c 
  WHERE h.custno = c.custno 
    and h.hdate = '6/8/2009'
  group by 
    h.salesmanno 
  , c.custname 
  with rollup 
  order by
    h.salesmanno 
  , c.custname 

Чтобы получить результаты в правильном порядке, измените порядок на что-то вроде этого:

  order by
    grouping(h.salesmanno)
  , h.salesmanno
  , grouping(c.custname)
  , c.custname 
0 голосов
/ 08 июня 2009

Вложите это в подзапрос и добавьте свой фильтр в WHERE:

SELECT *
FROM (
select h.salesmanno ,c.custname ,h.amt ,sum(h.amt) over (partition by salesmanno) as subtotal
,sum(h.amt) over (partition by null) as grandtotal
from hdr h, cust c WHERE h.custno = c.custno and h.hdate = '6/8/2009' 
) AS X
WHERE whatever
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...