Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Combine Multiple Select Statements - response (8) by dnoeth

$
0
0

A Derived Table (aka Inline View in Oracle) is a nested SELECT, i.e. a SELECT statement instead of table/view reference in the FROM clause, e.g.

SELECT t1.datetm, t1,cnt, t2.cnt
FROM
 (
   SELECT COUNT (DISTINCT RP.REGISTER_ID) AS cnt,
       CAST (RPE.CEMAIL_DATETM AS DATE) AS datetm
   FROM
       SCHEMA_1.REGISTER_TBL RP
   INNER JOIN     
       SCHEMA_2.MPROGRAM MP
       ON RP.MPRG_NBR= MP.MPRG_NBR
   INNER JOIN
       SCHEMA_2.RPEMAIL_ADDR RPE
       ON RP.REGISTER_ID=RPE.REGISTER_ID
       AND RP.MPRG_NBR=RPE.MPRG_NBR
   WHERE   RP.MPRG_NBR IN (100)
       AND RPE.SUBSET_ID ='O'
       AND RPE.CEMAIL_DATETM BETWEEN (CURRENT_DATE -7) AND CURRENT_DATE         
   GROUP BY CAST ( RPE.CEMAIL_DATETM  AS DATE)
 ) AS t1
JOIN
 (
   SELECT COUNT (DISTINCT RP.MCPR_ID) AS cnt,
       CAST (CA.CAS_DATETM AS DATE) AS datetm
   FROM
       SCHEMA_1.REGISTER_TBL RP
   INNER JOIN
       SCHEMA_1.Customer_TBL CA
       ON RP.REGISTER_ID = CA.REGISTER_ID
   WHERE  
           CA.MPRG_NBR IN (100)
       AND CA.L_NBR IN (215)
       AND CA.T_CODE IN ('Rose_B')
       AND CA.CAS_DATETM BETWEEN (CURRENT_DATE -7) AND CURRENT_DATE
   GROUP BY CAST (CA.CAS_DATETM AS DATE)
 ) AS t2
ON t1.datetm = t2.datetm

You have to follow some rules:

  • any calculated column must have an alias: e.g. col1*col2 AS col3
  • the Derived Table must have an alias, e.g. FROM (SELECT ...) AS t1 
  • no ORDER BY within the Derived Table

Dieter


Viewing all articles
Browse latest Browse all 14773

Trending Articles



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