使用增量记录创建基于SQL查询的每日报告

时间:2019-07-04 07:00:31

标签: sql sql-server database cognos-10

我需要使用仅加载增量记录的数据库来创建交叉表。请考虑以下样本,表1:

DAY         ACCOUNT NO  SALES   GENDER
1-Apr-2019  ACC01       5000    MALE
1-Apr-2019  ACC02       6000    FEMALE
1-Apr-2019  ACC03       7000    FEMALE
1-Apr-2019  ACC04       8000    MALE
2-Apr-2019  ACC01       7500    MALE
2-Apr-2019  ACC02       4000    FEMALE
2-Apr-2019  ACC03       10000   FEMALE
2-Apr-2019  ACC04       20000   MALE
3-Apr-2019  ACC01       8500    FEMALE
3-Apr-2019  ACC05       3500    FEMALE
4-Apr-2019  ACC02       9500    FEMALE
4-Apr-2019  ACC06       3600    FEMALE
4-Apr-2019  ACC07       4800    MALE
5-Apr-2019  ACC01       15000   FEMALE
5-Apr-2019  ACC03       12500   FEMALE

表1条目说明如下:

4月1日,有4个帐户,其销售编号和帐户持有人的性别。
4月2日,所有帐户的销售编号有所变化,因此已加载。
但是在4月3日,我们仅加载增量帐户。可以看出,只有ACC01的销售发生了变化,并且还有一个新帐户ACC05。因此,只会加载这两个帐户。
再次在4月4日,现有帐户ACC02的销售量比之前的值增加,因此被加载。与此同时,还有2个新帐户ACC06和ACC07也已加载。
最终,在4月5日,我们加载现有帐户,这些帐户的销售编号从其最后一个值开始发生变化。

现在,我必须做一份每日MIS报告,考虑到我的所有帐户都已在2019年4月5日启用,结果应如下所示。

enter image description here

没有帐户数实际上是每天结束时活动帐户的计数。

以上列出的报表1,我能够通过Report Studio中的Cognos Query / SQL项目使用以下子查询来实现:

SELECT  DISTINCT D1.DAY, (SELECT COUNT(DISTINCT D2.ACCN_NO) FROM
DAILY_TABLE D2 WHERE D2.DAY <= D1.DAY) AS NO_OF_ACCOUNTS FROM
DAILY_TABLE D1 GROUP BY D1.DAY

现在,我的要求是生成带有性别标记的以下交叉表。

enter image description here

最好的问候

2 个答案:

答案 0 :(得分:1)

您可以通过自连接容易地将总数分割,但是我不知道如何设置no of accounts。您将不得不更好地解释。

要划分销售量,您可以使用此

declare @sales table (salesday date, account varchar(5), sales int, gender varchar(10))

insert into @sales (salesday, account, sales, gender)
values ('20190401', 'ACC01', 5000, 'MALE'),
       ('20190401', 'ACC02', 6000, 'FEMALE'),
       ('20190401', 'ACC03', 7000, 'FEMALE'),
       ('20190401', 'ACC04', 8000, 'MALE'),
       ('20190402', 'ACC01', 7500, 'MALE'),
       ('20190402', 'ACC02', 4000, 'FEMALE'),
       ('20190402', 'ACC03', 10000, 'FEMALE'),
       ('20190402', 'ACC04', 20000, 'MALE'),
       ('20190403', 'ACC01', 8500, 'FEMALE'),
       ('20190403', 'ACC05', 3500, 'FEMALE'),
       ('20190404', 'ACC02', 9500, 'FEMALE'),
       ('20190404', 'ACC06', 3600, 'FEMALE'),
       ('20190404', 'ACC07', 4800, 'MALE'),
       ('20190405', 'ACC01', 15000, 'FEMALE'),
       ('20190405', 'ACC03', 12500, 'FEMALE')

select s.salesday,
       sum(sm.sales) as sales_male,
       sum(sf.sales) as sales_female
from   @sales s
  left join @sales sm on s.salesday = sm.salesday
                     and s.account = sm.account
                     and sm.gender = 'MALE'
  left join @sales sf on s.salesday = sf.salesday
                     and s.account = sf.account
                     and sf.gender = 'FEMALE'
group by s.salesday
order by 1

结果将是

salesday    sales_male  sales_female    
--------    ----------  ------------    
2019-04-01  13000       13000   
2019-04-02  27500       14000   
2019-04-03  null        12000   
2019-04-04  4800        13100   
2019-04-05  null        27500   

答案 1 :(得分:0)

仅使用累计和。下面将生成男性和女性的所有每日和累积值:

select salesday, count(*) as daily_cnt, sum(sales) as daily_sales,
       sum(case when gender = 'MALE' then 1 else 0 end) as daily_male_cnt,
       sum(case when gender = 'MALE' then sales else 0 end) as daily_male_sales,
       sum(case when gender = 'FEMALE' then 1 else 0 end) as daily_female_cnt,
       sum(case when gender = 'FEMALE' then sales else 0 end) as daily_female_sales,
       sum(count(*)) over (order by salesday) as cumulative_count,
       sum(sum(sales)) over (order by salesday) as cumulative_sales,
       sum(sum(case when gender = 'MALE' then 1 else 0 end)) over (order by salesday) as cumulative_male_cnt,
       sum(sum(case when gender = 'MALE' then sales else 0 end)) over (order by salesday) as cumulative_male_sales,
       sum(sum(case when gender = 'FEMALE' then 1 else 0 end)) over (order by salesday) as cumulative_female_cnt,
       sum(sum(case when gender = 'FEMALE' then sales else 0 end)) over (order by salesday) as cumulative_female_sales
from @sales s
group by salesday;

您可以选择实际需要的列。

Here是db <>小提琴。