我需要指定不同的,其中的條款,以不同的列,例如,我的「PrinterCounter.EndPagesColor」和「PrinterCounter.EndPagesMono」我想用這個地方:SQL服務器 - 不同的WHERE的條款,以不同的列
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01'
AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND Printer.WasRemoved = 0
AND ClientId = 6
AND SubClientId = 752
ORDER BY
PrinterCounter.ReadTime DESC
但對於 「PrinterCounter.StartPagesColor」 和 「PrinterCounter.StartPagesMono」 我想這一個:
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01' AND
PrinterCounter.ReadTime <= '31/07/2017 23:59:59' AND
Printer.WasRemoved = 0 AND
ClientId = 6 AND
SubClientId = 752
ORDER BY
PrinterCounter.ReadTime ASC
下面是完整的代碼:
SELECT DISTINCT
Printer.[PrinterId],
Printer.[PlaceId],
PrinterCounter.ReadTime,
PrinterCounter.EndPagesColor,
PrinterCounter.EndPagesMono,
PrinterCounter.StartPagesColor,
PrinterCounter.StartPagesMono
FROM
[~client_database~].[dbo].[Printers] AS Printer
JOIN
[~system_database~].[dbo].[PrinterCounter] AS PrinterCounter ON PrinterCounter.PrinterId = Printer.PrinterId
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01'
AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND Printer.WasRemoved = 0
AND ClientId = 6
AND SubClientId = 752
ORDER BY
PrinterCounter.ReadTime ASC
我會爲你弄清楚,每臺打印機在一天開始時有一個計數器,一天在結束時,產量將減去開始。
每個客戶有幾臺打印機,我必須在提供的時間內收集每臺打印機的產量。
該連接是爲了確保SQL僅返回帶有計數器的打印機。
這是一個想:
SELECT
DISTINCT
Printer.PrinterId
,Printer.PlaceId
,PrinterCounter.ReadTime
,(
SELECT TOP 1
EndPagesMono
FROM
[system_mfpm].[dbo].[PrinterCounter]
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01' AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND PrinterCounter.ClientId = 6
AND PrinterCounter.SubClientId = 752
AND PrinterCounter.PrinterId = Printer.PrinterId
ORDER BY PrinterCounter.ReadTime DESC
)-(
SELECT TOP 1
StartPagesMono
FROM
[system_mfpm].[dbo].[PrinterCounter]
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01' AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND PrinterCounter.ClientId = 6
AND PrinterCounter.SubClientId = 752
AND PrinterCounter.PrinterId = Printer.PrinterId
ORDER BY PrinterCounter.ReadTime ASC
) AS Mono
,(
SELECT TOP 1
EndPagesColor
FROM
[system_mfpm].[dbo].[PrinterCounter]
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01' AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND PrinterCounter.ClientId = 6
AND PrinterCounter.SubClientId = 752
AND PrinterCounter.PrinterId = Printer.PrinterId
ORDER BY PrinterCounter.ReadTime DESC
)-(
SELECT TOP 1
StartPagesColor
FROM
[system_mfpm].[dbo].[PrinterCounter]
WHERE
PrinterCounter.ReadTime >= '01/07/2017 00:00:01' AND PrinterCounter.ReadTime <= '31/07/2017 23:59:59'
AND PrinterCounter.ClientId = 6
AND PrinterCounter.SubClientId = 752
AND PrinterCounter.PrinterId = Printer.PrinterId
ORDER BY PrinterCounter.ReadTime ASC
) AS Color
FROM [client_lfn_MTUXNJCZMJQA].[dbo].[Printers] AS Printer
JOIN [system_mfpm].[dbo].[PrinterCounter] AS PrinterCounter ON PrinterCounter.PrinterId = Printer.PrinterId
WHERE Printer.WasRemoved = 0
,但我敢肯定這是一個不好的做法,它可以提高
是不是這兩個'WHERE'子句相同? – sagi
我看到的唯一區別是'ORDER BY'方向 – Siyual
您可以做2個單獨的查詢並使用'Union'嗎? – Nope