2015-06-09 69 views
0

我有一個查詢,檢查是否至少有一行存在於三個表A,B和C中,我想檢查theDate = myDate。我希望能夠查看這些表是否包含特定日期的數據(myDate)。 我希望用戶在運行查詢時選擇變量myDate。我怎麼做?供參考:我使用的Teradata:用戶運行查詢時設置的變量,Teradata

查詢:

SELECT 'A' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?" 
WHERE EXISTS (SELECT * FROM A WHERE theDate=myDate) 

UNION ALL 

SELECT 'B', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
WHERE EXISTS (SELECT * FROM B WHERE theDate=myDate) 

UNION ALL 

SELECT 'C', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
WHERE EXISTS (SELECT * FROM C WHERE theDate=myDate) 

回答

0

我認爲一個動態的過程/宏將最適合您的需求。 編譯以下程序:

REPLACE PROCEDURE your_proc (IN in_mydate DATE) 
READS SQL DATA 
DYNAMIC RESULT SETS 1 
BEGIN 
DECLARE stmt_str VARCHAR(60000); 
DECLARE RESPONSE CURSOR WITH RETURN ONLY TO CLIENT FOR STMT; 

----------------------------------------------------------------------------------------------- 
-------------------------------- PROGRAM LOGIC ------------------------------------------------ 
----------------------------------------------------------------------------------------------- 

     SET stmt_str =   'SELECT ''A'' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?"' 
           ||'WHERE EXISTS (SELECT * FROM A WHERE theDate='''||in_mydate||''')' 
           ||'UNION ALL' 
           ||'SELECT ''B'', CASE WHEN COUNT(*) = 1 THEN ''Y'' ELSE ''N'' END' 
           ||'WHERE EXISTS (SELECT * FROM B WHERE theDate='''||in_mydate||''') ' 
           ||'UNION ALL' 
           ||'SELECT ''C'', CASE WHEN COUNT(*) = 1 THEN ''Y'' ELSE ''N'' END' 
           ||'WHERE EXISTS (SELECT * FROM C WHERE theDate='''||in_mydate||''');'; 



PREPARE STMT FROM stmt_str; 
OPEN RESPONSE; 
DEALLOCATE PREPARE STMT; 

END; 

在這之後你調用語句只是把你的約會(作爲輸入參數)。輸出將是您所需的選擇查詢的結果。

如果您使用的是宏下面的代碼會給你的輸出:

 REPLACE MACRO your_macro (mydate DATE) 
     AS (
     SELECT 'A' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END   AS "True?" 
     WHERE EXISTS (SELECT * FROM A WHERE theDate=:myDate) 

     UNION ALL 

     SELECT 'B', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
     WHERE EXISTS (SELECT * FROM B WHERE theDate=:myDate) 

     UNION ALL 

     SELECT 'C', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
     WHERE EXISTS (SELECT * FROM C WHERE theDate=:myDate); 
     ); 

感謝

0

可以刪除你的「變量」中單列揮發性表,並加入到在您的查詢表:

create volatile table varTable 
as (select <your date value> as thedate) 
with data 
on commit preserve rows 
; 

SELECT 'A' AS "Table", 
CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?" 
from 
a inner join vartable 
    on a.<date column> = vartable.thedate 
0

我發現一個在我看來簡單的解決方案,工作得很好。 ?theDate是在運行查詢之前由用戶設置的可變權限。 (對於其他解決方案,我的問題是我無法創建存儲過程或宏)

SELECT 'A' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?" 
WHERE EXISTS (SELECT * FROM A WHERE theDate=?theDate) 

UNION ALL 

SELECT 'B', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
WHERE EXISTS (SELECT * FROM B WHERE theDate=?theDate) 

UNION ALL 

SELECT 'C', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END 
WHERE EXISTS (SELECT * FROM C WHERE theDate=?theDate)