在临时表中使用where子句

时间:2015-09-03 11:14:02

标签: mysql sql where-clause temp-tables

我正在尝试在临时表中使用where子句。实际上我使用它但结果不正确。试图在两个日期之间找到产品,但是当我使用where子句时它没有数据。

CREATE TABLE #TEMPCOZGU
(
    ROW INT,
    TEZGAH VARCHAR(50),
    COZGU VARCHAR(50),  
    PERSONEL VARCHAR(50),
    DATE DATETIME,
    FIRE INT,
    FIRE_METRE NUMERIC(25,6)
)
    INSERT INTO #TEMPCOZGU

select row_number() over(order by TEZGAH) AS ROW, TEZGAH, COZGU, PERSONEL, TARIH, FIRE, FIRE_METRE  
from cozgu_fırelerı 
where tezgah = 'M01' and cozgu = 'SAĞ ALT BAĞLANTI'



SELECT  T1.TEZGAH, T1.COZGU, T1.PERSONEL,T1.FIRE, T1.FIRE_METRE, t1.DATE, T2.DATE AS PREVIOUSDATE, ISNULL(DATEDIFF(DAY, T2.DATE, T1.DATE), 0) AS GUNFARKI  , SUM(MKARE) AS TOPLAMMKARE
FROM #TEMPCOZGU as t1
left outer join #TEMPCOZGU as t2 on t1.row-1 = t2.row
left outer join HALI_TEZGAH AS TE ON T1.TEZGAH = TE.TEZGAH_NO
LEFT OUTER JOIN HALI_GUNLUK_URETIM AS U ON U.TEZGAH = TE.ID 
--WHERE U.TARIH BETWEEN T1.TARIH AND T2.TARIH
GROUP BY  T1.TEZGAH, T1.COZGU, T1.PERSONEL, T1.DATE,T1.FIRE, T1.FIRE_METRE, T2.DATE

drop table #TEMPCOZGU`

1 个答案:

答案 0 :(得分:0)

如果日期时间值始终将时间设置为“00:00:00”,请尝试

SELECT  T1.TEZGAH, T1.COZGU, T1.PERSONEL,T1.FIRE, T1.FIRE_METRE, t1.DATE, T2.DATE AS PREVIOUSDATE, ISNULL(DATEDIFF(DAY, T2.DATE, T1.DATE), 0) AS GUNFARKI  , SUM(MKARE) AS TOPLAMMKARE
FROM #TEMPCOZGU as t1
left outer join #TEMPCOZGU as t2 on t1.row-1 = t2.row
left outer join HALI_TEZGAH AS TE ON T1.TEZGAH = TE.TEZGAH_NO
LEFT OUTER JOIN HALI_GUNLUK_URETIM AS U ON U.TEZGAH = TE.ID 
WHERE U.TARIH >= T1.TARIH AND  U.TARIH < DateADD (T2.TARIH, INTERVAL 1 DAY)
GROUP BY  T1.TEZGAH, T1.COZGU, T1.PERSONEL, T1.DATE,T1.FIRE, T1.FIRE_METRE, T2.DATE

如果情况并非如此,那么你的中间人应该首先工作。