English 中文(简体)
trrying to optimize the query
原标题:

I am just trying to left join and attach all the table but remaining table seems to be same is there any other way to attach the table like function or temp table to optimize it.

SELECT  Business Lines 
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Internal Fraud]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [External Fraud]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Employment Practices & Workplace Safety]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Clients, Products Business Practices]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Damage to Physical Assets]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Business Disruption and System Failures]
    , No Of Events 
    , Gross Loss 
    , Net Loss  as [Execution Delivery and Process Management]
UNION ALL
SELECT bbl.Name
    ,e1.e1 AS e1
    ,e1.n1 AS n1
    ,e1.g1 AS g1
    ,e2.e2 AS e2
    ,e2.n2 AS n2
    ,e2.g2 AS g2
    ,e3.e3 AS e3
    ,e3.n3 AS n3
    ,e3.g3 AS g3
    ,e4.e4 AS e4
    ,e4.n4 AS n4
    ,e4.g4 AS g4
    ,e5.e5 AS e5
    ,e5.n5 AS n5
    ,e5.g5 AS g5
    ,e6.e6 AS e6
    ,e6.n6 AS n6
    ,e6.g6 AS g6
    ,e7.e7 AS e7
    ,e7.n7 AS n7
    ,e7.g7 AS g7
FROM LossData.BaselBusinessLine bbl
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e1
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n1
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g1
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 1

    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e1 ON bbl.Id = e1.BaselBusinessLineId
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e2
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n2
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g2
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 2 
                    
    
    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e2 ON bbl.Id = e2.BaselBusinessLineId
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e3
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n3
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g3
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 3
            
    
    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e3 ON bbl.Id = e3.BaselBusinessLineId
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e4
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n4
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g4
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 4


    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e4 ON bbl.Id = e4.BaselBusinessLineId
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e5
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n5
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g5
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 5
            

    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e5 ON bbl.Id = e5.BaselBusinessLineId
LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e6
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n6
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g6
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 6


    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e6 ON bbl.Id = e6.BaselBusinessLineId
    
    LEFT JOIN (
    SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e7
        ,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n7
        ,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g7
        ,BaselBusinessLineId
        ,BaselEventTypeOneId
    FROM LossData.BaselBusinessLine bbl
    JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
    WHERE r.BaselEventTypeOneId = 7 


    GROUP BY 
        BaselBusinessLineId
        ,BaselEventTypeOneId
    ) e7 ON bbl.Id = e7.BaselBusinessLineId

here I am joining three column with the help of leftjoin

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签