我正在尝试在临时表中使用where子句。其实我用过,但结果不正确。试图寻找2日期之间的产品,但是当我使用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`

最佳答案

如果datetime值始终将时间设置为“ 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


如果不是这种情况,那么您之间应该首先工作。

关于mysql - 在临时表中使用where子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32374071/

10-09 20:09