Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Query Rewrite - How to Avoid Multiple Left Outer Join

$
0
0

Hi,

 

We have a database, using Entity–attribute–value model. So, we are using automatic SQL Query generations, which generates a lot of Left Outer Join. Is there a better way to write this query ?

 

I tried pivot and max case, but i can't get the logic or syntax. Appreciate some help here.

 

SQL Query

========

SELECT I.ITEM_ID AS ITEM_ID,

       IP_NAME.PROPERTY_STRING AS ITEM_NAME,

       IP_ACTIVE.PROPERTY_STRING AS ACTIVE,

       IP_TYPE.PROPERTY_STRING AS TYPE,

       CL_TYPE.CODE_TEXT AS TYPE_TEXT,

       IL_ITEM_BORE.TO_ITEM_ID AS BORE_ID,

       NVL (IP_BORE_NAME.PROPERTY_STRING, 'Not defined') AS BORE

  FROM ITEM I

       INNER JOIN DATE_INFO DI ON DI.DATETIME = TRUNC (SYSDATE)

       LEFT OUTER JOIN ITEM_PROPERTY IP_NAME

          ON     IP_NAME.ITEM_ID = I.ITEM_ID

             AND IP_NAME.START_DATETIME<= DI.DATETIME

             AND IP_NAME.END_DATETIME> DI.DATETIME

             AND IP_NAME.PROPERTY_TYPE = 'NAME'

       LEFT OUTER JOIN ITEM_PROPERTY IP_ACTIVE

          ON     IP_ACTIVE.ITEM_ID = I.ITEM_ID

             AND IP_ACTIVE.START_DATETIME<= DI.DATETIME

             AND IP_ACTIVE.END_DATETIME> DI.DATETIME

             AND IP_ACTIVE.PROPERTY_TYPE = 'ACTIVE'

       LEFT OUTER JOIN ITEM_PROPERTY IP_TYPE

          ON     IP_TYPE.ITEM_ID = I.ITEM_ID

             AND IP_TYPE.START_DATETIME<= DI.DATETIME

             AND IP_TYPE.END_DATETIME> DI.DATETIME

             AND IP_TYPE.PROPERTY_TYPE = 'TYPE'

       LEFT OUTER JOIN CODE_LIST CL_TYPE

          ON     CL_TYPE.CODE = IP_TYPE.PROPERTY_STRING

             AND CL_TYPE.LIST_TYPE = 'WT'

       LEFT OUTER JOIN ITEM_LINK IL_ITEM_BORE

          ON     IL_ITEM_BORE.TO_ITEM_ID = I.ITEM_ID

             AND IL_ITEM_BORE.LINK_TYPE = 'BORE_ITEM'

             AND DI.DATETIME >= IL_ITEM_BORE.START_DATETIME

             AND DI.DATETIME < IL_ITEM_BORE.END_DATETIME

       LEFT OUTER JOIN ITEM_PROPERTY IP_BORE_NAME

          ON     IL_ITEM_BORE.FROM_ITEM_ID = IP_BORE_NAME.ITEM_ID

             AND DI.DATETIME >= IP_BORE_NAME.START_DATETIME

             AND DI.DATETIME < IP_BORE_NAME.END_DATETIME

             AND IP_BORE_NAME.PROPERTY_TYPE = 'NAME'

WHERE     I.ITEM_TYPE = 'COMPLETION'

       AND I.START_DATETIME <= DI.DATETIME

       AND I.END_DATETIME > DI.DATETIME;

 

 

 

 

ITEM_PROPERTY Table

==================

 

 

ITEM_IDITEM_TYPEPROPERTY_TYPEPROPERTY_STRING
1COMPLETIONACTIVEFALSE
1COMPLETIONNAMELP 201 - SOLD
1COMPLETIONTYPEPROD
1COMPLETIONAB
1COMPLETIONCD
1COMPLETIONEF
1COMPLETIONGH
2BORENAMELP 201

 

 

CODE_LIST Table

==============

 

LIST_TYPECODECODE_TEXT
WTDISDisposal
WTOBObservation
WTPROD

Production

 

 

BORE Table

==========

 

LINK_TYPEFROM_ITEM_IDFROM_ITEM_TYPETO_ITEM_IDTO_ITEM_TYPE
BORE_ITEM2BORE1COMPLETION

 

 

 

 

END RESULT

===========

 

ITEM_IDITEM_NAMEACTIVETYPETYPE_TEXTBORE_IDBORE
1LP 201 - SOLDFALSEPRODProduction1LP 201

 

 

 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>