SQL查询损益

时间:2017-12-15 09:58:03

标签: sql sql-server-2012

我在SQL Server中有两个表:TblGroup和TblAmount

TblGroup

Grp_Id  Grp_Name            GrpType
------  --------------      -----------
1       Direct Incomes      Income
2       Indirect Incomes    Income
3       Misc. Expences      Expence
4       Other Incomes       Income
5       Purchases           Expence
6       Selling Expences    Expence
7       Sales               Income

TblAmount

Grp_Id  Amount          
------  -------
1       2000
2       1500
3       3000
4       5000
5       4000
6       1000
7       4500

这是我需要的输出:

Income              Amount  Expence             Amount
-------------       -----   -------------       ----------
Direct Incomes      2000    Misc. Expences      3000
Indirect Incomes    1500    Purchases           4000
Other Incomes       5000    Selling Expences    1000
Sales               4500

这是我的查询,但没有得到正确的结果:

declare @TblGroup as table (Grp_Id int, Grp_Name varchar(50), GrpType varchar(20))
insert into @TblGroup values    
     (1,'Direct Incomes','Income')
    ,(2,'Indirect Incomes','Income')
    ,(3,'Misc. Expences','Expence')
    ,(4,'Other Incomes','Income')
    ,(5,'Purchases','Expence')
    ,(6,'Selling Expences','Expence')
    ,(7,'Sales','Income')

declare @TblAmount as table(Grp_Id int,Amount int)
insert into @TblAmount values
     (1,2000)   
    ,(2,1500)   
    ,(3,3000)   
    ,(4,5000)   
    ,(5,4000)   
    ,(6,1000)   
    ,(7,4500)   

select * from (
    SELECT Grp_Name AS Income, Amount AS Amount
    FROM @TblGroup g
    LEFT JOIN @TblAmount a ON g.Grp_Id = a.Grp_Id
    WHERE GrpType = 'Income') as Income 
JOIN (
    SELECT Grp_Name AS Expence, Amount AS Amount
    FROM @TblGroup g
    LEFT JOIN @TblAmount a ON g.Grp_Id = a.Grp_Id
    WHERE GrpType = 'Expence') as Expence on  Income.Income<>Expence.Expence

如果输出中有重复的收入和支出,我该如何克服这个问题。

4 个答案:

答案 0 :(得分:0)

这是一个相当简单的左连接问题,这是解决方案的一半

SELECT GrpType AS Income, TblAmount.Amount AS Amount
FROM TblGroup
LEFT JOIN TblAmount
ON TblGroup.Grp_Id = TblAmount.Grp_Id
WHERE GrpType = "Income"

您应该能够猜到如何获得其他结果。

答案 1 :(得分:0)

SELECT TG.Grp_Name AS Income, TA.Amount 
FROM TblGroup TG, TblAmount TA 
WHERE TA.Grp_Id = TG.Grp_Id 
  AND TG.GrpType = 'Income';

SELECT TG.Grp_Name AS Expence, TA.Amount 
FROM TblGroup TG, TblAmount TA 
WHERE TA.Grp_Id = TG.Grp_Id 
  AND TG.GrpType = 'Expence';
     

第一个查询将为您提供grpType收入的详细信息,第二个查询将提供grpType费用的详细信息。   where子句中的简单语句可以为您提供输出。您也可以使用SQL连接尝试它。但我更愿意放松一下。

答案 2 :(得分:0)

CREATE TABLE TblGroup ( Grp_Id int, Grp_Name varchar(200),
                                             GrpType varchar(200));


INSERT INTO TblGroup
VALUES(1,
       'Direct Incomes',
       'Income');


INSERT INTO TblGroup
VALUES(2,
       'Indirect Incomes',
       'Income');


INSERT INTO TblGroup
VALUES(3,
       'Misc. Expences',
       'Expence');


INSERT INTO TblGroup
VALUES(4,
       'Other Incomes',
       'Income');


INSERT INTO TblGroup
VALUES(5,
       'Purchases',
       'Expence');


INSERT INTO TblGroup
VALUES(6,
       'Selling Expences',
       'Expence');


INSERT INTO TblGroup
VALUES(7,
       'Sales',
       'Income');


CREATE TABLE TblAmount ( Grp_Id int, amount int);


INSERT INTO TblAmount
VALUES(1,
       2000);


INSERT INTO TblAmount
VALUES(2,
       1500);


INSERT INTO TblAmount
VALUES(3,
       3000);


INSERT INTO TblAmount
VALUES(4,
       5000);


INSERT INTO TblAmount
VALUES(5,
       4000);


INSERT INTO TblAmount
VALUES(6,
       1000);


INSERT INTO TblAmount
VALUES(7,
       4500);


SELECT DISTINCT Income,
                c.amount,
                Expence,
                d.amount from
  (SELECT a.Grp_Id, CASE
                        WHEN GrpType='Income' THEN Grp_Name
                    END AS Income, b.amount
   FROM TblGroup a
   INNER  JOIN TblAmount b ON a.Grp_Id=b.Grp_Id)c
LEFT OUTER JOIN
  (SELECT a.Grp_Id,
          CASE
              WHEN GrpType='Expence' THEN Grp_Name
          END AS Expence,
          amount
   FROM TblGroup a
   INNER  JOIN TblAmount b ON a.Grp_Id=b.Grp_Id)d ON c.Grp_Id=d.Grp_Id;

答案 3 :(得分:0)

最后我得到了答案,这是解决方案。

        declare @TblGroup as table (Grp_Id int, Grp_Name varchar(50), GrpType varchar(20))
insert into @TblGroup values    
     (1,'Direct Incomes','Income')
    ,(2,'Indirect Incomes','Income')
    ,(3,'Misc. Expences','Expence')
    ,(4,'Other Incomes','Income')
    ,(5,'Purchases','Expence')
    ,(6,'Selling Expences','Expence')
    ,(7,'Sales','Income')

declare @TblAmount as table(Grp_Id int,Amount int)
insert into @TblAmount values
     (1,2000)   
    ,(2,1500)   
    ,(3,3000)   
    ,(4,5000)   
    ,(5,4000)   
    ,(6,1000)   
    ,(7,4500)   

        SELECT Row_Number() OVER (ORDER BY (SELECT 1)) as SNo, TG.Grp_Name AS Income, TA.Amount  into #Incme
        FROM @TblGroup TG, @TblAmount TA 
        WHERE TA.Grp_Id = TG.Grp_Id 
          AND TG.GrpType = 'Income';

        SELECT  Row_Number() OVER (ORDER BY (SELECT 1)) as SNo , TG.Grp_Name AS Expence, TA.Amount  into #Expnse
        FROM @TblGroup TG, @TblAmount TA 
        WHERE TA.Grp_Id = TG.Grp_Id 
          AND TG.GrpType = 'Expence';

    DECLARE @TblMain as table(Income varchar(50), Amount int, Expense varchar(50), Amount2 int)
    DECLARE  @row INT = 1
            ,@ROWCOUNT int 
            ,@RowCount_Incme INT = (select COUNT(*) from #Incme)
            ,@RowCount_Expnse INT = (select COUNT(*) from #Expnse)

        set @ROWCOUNT = case when @RowCount_Incme > @RowCount_Expnse then @RowCount_Incme else @RowCount_Expnse end

    WHILE (@ROW <= @ROWCOUNT)
    BEGIN
            insert into @TblMain(Income, Amount,Expense,Amount2) (
                select Income, Amount
                            ,(select Expence from #Expnse where SNo=@row) 
                            ,(select Amount from #Expnse where SNo=@row) 
                            from #Incme where SNo=@row
                )
        SET @ROW = @ROW+1
    END
    select * from @TblMain

    drop table #Incme
    drop table #Expnse