SQL查询 - 使用保持分支结构的分组函数的多表连接

时间:2014-11-27 06:54:17

标签: sql-server join report branch aggregation

我已经用尽了我的搜索解决方案,并希望发布我的问题,看看是否存在解决方案。

我需要写一份报告来显示每个分支的借方和贷方。报告还需要显示分支机构是否没有DB或CR。

为了简单起见,我缩小了表格以尝试突出我的问题。

我的第一张表中包含我的分支数据

BranchNo    BranchName
1           Main
2           Mgorogoro
3           Arusha

我的第二张表包含所有借记交易

txid    Narrative   Amount  Date        BranchNo
1       Test 1      50.00   2014/11/26  1
2       Test 2      20.00   2014/11/27  3

我写了一个SQL语句,它给了我需要的结果: -

DECLARE @get_Dates CURSOR;
DECLARE @Date VarChar(10);

DECLARE @tbl TABLE
                (
                    DebitOutCount int, 
                    BranchCode VarChar(250),
                    TxDate VarChar(10)
                )
--DECLARE @tbl TABLE(Idx1 VarChar(50), Idx8 VarChar(50), Idx3 VarChar(50))

SET @get_Dates = CURSOR FOR 
    Select Debits_OUT.Date FROM Debits_OUT GROUP BY Debits_OUT.Date ORDER BY Debits_OUT.Date

OPEN @get_Dates;

FETCH NEXT FROM @get_Dates into @Date;

WHILE (@@FETCH_STATUS = 0)

    BEGIN 
        --INSERT INTO @tbl SELECT Idx1, Idx8, Idx3 FROM SessionDailyControl WHERE Idx1 = @sessionId
        INSERT INTO @tbl 
            SELECT 
            (SELECT ISNULL(SUM(DB_OUT.Amount), 0) FROM Debits_OUT AS DB_OUT WHERE B.BranchNo = DB_OUT.BranchNo AND DB_OUT.Date = @Date) AS DebitOutValue,
            CAST(B.BranchNo As VarChar(10)) + ' ' + B.BranchName As [Branch Names],
            @Date
            From exBranches As B

        FETCH NEXT FROM @get_Dates into @Date
    END 

CLOSE @get_Dates

DEALLOCATE @get_Dates

SELECT * FROM @tbl

结果是我需要的格式: -

DebitOutCount   BranchCode  TxDate
50              1 Main      2014/11/26
0               2 Mgorogoro 2014/11/26
0               3 Arusha    2014/11/26
0               1 Main      2014/11/27
0               2 Mgorogoro 2014/11/27
20              3 Arusha    2014/11/27

但是,报表工具和视图无法使用上述内容。我尝试过Left Joins - 但问题是结果集不会保留没有交易的日期的分支号码。例如: -

SELECT 
    ISNULL(SUM(B.Amount), 0),
    CAST(A.BranchNo As VarChar(10)) + ' ' + A.BranchName As [Branch Names],
    B.Date 
From exBranches As A
    LEFT JOIN Debits_OUT AS B ON A.BranchNo = B.BranchNo
GROUP BY B.Date, A.BranchNo, A.BranchName 
ORDER BY B.Date, A.BranchNo, A.BranchName

返回: -

DB_OUT  Branch Names    Date
0.00    2 Mgorogoro     NULL
50.00   1 Main          2014/11/26
20.00   3 Arusha        2014/11/27

在我尝试的所有JOIN组合中,我无法让分支显示debits表中每个日期的所有分支。

我是否完全错过了一个基本概念?我需要一个可以在视图中运行的查询,该视图返回与游标语句相同的数据。这可能吗?

2 个答案:

答案 0 :(得分:1)

我们的想法是首先生成分支和日期的可能组合:

create table exBranches(
    BranchNo int,
    BranchName varchar(20)
)
create table Debits_OUT(
    txId int,
    Narrative varchar(20),
    Amount decimal (6,2),
    [Date] date,
    BranchNo int
)
insert into exBranches values (1, 'Main'), (2, 'Mgorogoro'), (3, 'Arusha')
insert into Debits_OUT values (1, 'Test 1', 50.00, '20141126', 1), (2, 'Test 2', 20.00, '20141127', 3);

with BranchDate as(
    select
        b.BranchNo,
        b.BranchName,
        d.Date
    from exBranches b
    cross join (
        select distinct [Date] from Debits_OUT
    )d
)
    select
        isnull(DebitOutCount,0),
        cast(b.BranchNo as varchar(10)) + ' ' + b.BranchName as BranchName,
        b.Date
    from BranchDate b
    left join (
        select
            branchNo,
            [Date],
            sum(Amount) as DebitOutCount
        from Debits_OUT
        group by
            BranchNo, [Date]
    )d 
        on d.BranchNo = b.BranchNo
        and d.Date = b.Date
order by b.date, b.BranchNo asc

drop table exBranches
drop table Debits_OUT

答案 1 :(得分:0)

尝试这是有效的.....

  select BranchName,amount,date1,BranchNo into #temp from exBranches 
  cross join (select distinct date1,amount from Debits_OUT)a

  select isnull(t.amount,0),a.BranchName,a.date1  from #temp a
  left join Debits_OUT t on t.BNo=a.BranchNo and a.date1=t.date1
  order by date1

在这里查看..   http://sqlfiddle.com/#!3/ad815/1