我想将2个表合并并转换为SQL Server中的1个表

时间:2018-06-25 09:04:00

标签: sql sql-server

declare @table table (groupname varchar(50),
                      credit numeric(18,2),
                      debit numeric(18,2),
                      drcreffect varchar(50),
                      undergroup varchar(50)
                     )

declare @table1 table (groupname varchar(50),
                       credit numeric(18,2),
                       debit numeric(18,2),
                       drcreffect varchar(50),
                       undergroup varchar(50)
                      )

insert into @table 
values ('Opening Balance', 0.00, 0.00, 'Dr', 'Master Group'),
       ('Opening Stock',   0.00, 0.00, 'Dr', 'Opening Stock'),
       ('Purchase',        0.00, 7800.00, 'Dr', 'Purchase'),
       ('Purchase Return', 2000.00, 0.00, 'Dr', 'Purchase'),
       ('Purchase Return', 3000.00, 0.00, 'Dr', 'Purchase'),
       ('Purchase Return', 3000.00, 0.00, 'Dr', 'Purchase')

insert into @table1 
values ('Sales', 0.00, 5000.00 ,'Dr', 'Sales'),
       ('Sales Return', 2000.00, 0.00,'Dr', 'Sales'),
       ('Sales Return', 2000.00, 0.00,'Dr', 'Sales')

SELECT 
    groupname, credit, debit, 
    LAG(debit,1,0) OVER (ORDER BY groupname) - credit AS Total 
FROM
    (SELECT DISTINCT 
         groupname, 
         SUM(credit) OVER (PARTITION BY groupname) AS Credit,
         SUM(debit) OVER (PARTITION BY groupname) AS debit,
         drcreffect, undergroup
     FROM 
         @table) X

SELECT 
    groupname, credit, debit, 
    LAG(debit, 1, 0) OVER (ORDER BY groupname) - credit AS Total 
FROM
    (SELECT DISTINCT 
         groupname,
         SUM(credit) OVER(PARTITION BY groupname) AS Credit,
         SUM(debit) OVER(PARTITION BY groupname) AS debit,
         drcreffect, undergroup
     FROM 
         @table1) X

如果在脚本上方运行,则为2个表

enter image description here

我想要像合并2表这样的输出并像下面的图像一样输出

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用行号联接表。应该像这样工作:

select t0.groupname, t0.credit, t0.debit, t0.Total ,t1.groupname as groupname1 , t1.credit as credit1, t1.debit as debit, t1.Total as Total1

from(
    SELECT row_number() as row_num, groupname,credit,debit, LAG(debit,1,0) OVER (ORDER BY groupname)-credit AS Total from (
        SELECT DISTINCT groupname,SUM(credit) OVER(PARTITION BY groupname) AS Credit,SUM(debit) OVER(PARTITION BY groupname) AS debit,drcreffect,undergroup
    FROM @table) AS t0
join
    (SELECT row_number() as row_num, groupname,credit,debit, LAG(debit,1,0) OVER (ORDER BY groupname)-credit AS Total from (
        SELECT DISTINCT groupname,SUM(credit) OVER(PARTITION BY groupname) AS Credit,SUM(debit) OVER(PARTITION BY groupname) AS debit,drcreffect,undergroup
    FROM  @table1) AS t1
on  t0.row_num=t1.row_num

PS:合并这些看似无关的查询似乎有些奇怪。如果我收到这样的请求,我会质疑动机,看看是否有更好的服务方式。这会尖叫带有内联查询的Report Builder或Excel模板

答案 1 :(得分:0)

不可能根据您的要求获得确切的输出,因为列名在每个Table \ View上都是唯一的,因此您不能使用相同的列名。但是你可以试试这个

;WITH T1
AS 
(
     SELECT 
          RN = ROW_NUMBER() OVER(ORDER BY groupname),
          groupname,
            credit,
            debit,
            LAG(debit, 1, 0) OVER(ORDER BY groupname)-credit AS Total
     FROM
    (
       SELECT DISTINCT
             groupname,
             SUM(credit) OVER(PARTITION BY groupname) AS Credit,
             SUM(debit) OVER(PARTITION BY groupname) AS debit,
             drcreffect,
             undergroup
       FROM @table
    )X
),T2
AS
(
     SELECT 
          RN = ROW_NUMBER() OVER(ORDER BY groupname),
          groupname,
            credit,
            debit,
            LAG(debit, 1, 0) OVER(ORDER BY groupname)-credit AS Total
     FROM
    (
       SELECT DISTINCT
             groupname,
             SUM(credit) OVER(PARTITION BY groupname) AS Credit,
             SUM(debit) OVER(PARTITION BY groupname) AS debit,
             drcreffect,
             undergroup
       FROM @table1
    ) X
),FNL
AS
(
    SELECT
       t1_groupname = T1.groupname,
       T1_Credit = T1.Credit,
       T1_debit = t1.debit,
       T1_Total = t1.Total,

       T2_groupname = T2.groupname,
       T2_Credit = T2.Credit,
       T2_debit = T2.debit,
       T2_Total = T2.Total

       FROM T1
          FULL JOIN T2
             ON T1.RN = T2.RN
)
SELECT 
    GroupName = t1_groupname,
    Credit = T1_Credit,
    Debit = T1_debit,
    Total = T1_Total,

    GroupName = T2_groupname,
    Credit = T2_Credit,
    Debit = T2_debit,
    Total = T2_Total

    FROM FNL

UNION

SELECT
    GroupName = 'Total',
    Credit = NULL,
    Debit = NULL,
    Total = SUM(T1_Total),

    GroupName = 'Total',
    Credit = NULL,
    Debit = NULL,
    Total = SUM(T2_Total)

    FROM FNL

UNION

SELECT
    GroupName = 'zGross Profit',
    Credit = NULL,
    Debit = NULL,
    Total = NULL,

    GroupName = 'Total',
    Credit = NULL,
    Debit = NULL,
    Total = SUM(T2_Total)-SUM(T1_Total)

    FROM FNL

结果

enter image description here