Sql-Server

如何將此查詢從 MS Access 轉換為 SQL Server

  • October 4, 2014

如何將其轉換為 SQL Server:

select 
-Sum((12=[GroupNum])*[BoardStatus]) AS G12
FROM 
QryAllBoardsByPacketIDs 
INNER JOIN 
tBsReportGroups 
 ON QryAllBoardsByPacketIDs.NominalLengthMetric=tBsReportGroups.NomLenMet
GROUP BY 
QryAllBoardsByPacketIDs.ParentPacketID, 
QryAllBoardsByPacketIDs.GradeID, 
QryAllBoardsByPacketIDs.NominalWidthMetric,   
QryAllBoardsByPacketIDs.NominalThicknessMetric;

親切的問候,

馬丁

不太確定我是否遵循,因為我很少使用 Access,但我相信這就是您正在尋找的:

SELECT -SUM(
  CASE WHEN 12=[GroupNum] THEN 1 ELSE 0 END
  * [BoardStatus]
  ) AS G12
[...]

CASE…WHEN 是 T-SQL 的內聯“if”。

乾杯,為什麼 MS Access 必須與其他一切不同?嗯。就像你說的那樣,我使用:

G1 = SUM(CASE WHEN GroupNum = 1 THEN 1 ELSE 0 END)*BoardStatus,
G2 = SUM(CASE WHEN GroupNum = 2 THEN 1 ELSE 0 END)*BoardStatus,
.....
G10 = SUM(CASE WHEN GroupNum = 10 THEN 1 ELSE 0 END)*BoardStatus,
G11 = SUM(CASE WHEN GroupNum = 11 THEN 1 ELSE 0 END)*BoardStatus,
G12 = SUM(CASE WHEN GroupNum = 12 THEN 1 ELSE 0 END)*BoardStatus

這是 - 在總和之前讓我失望,因為輸出不是負數。問候,

馬丁

引用自:https://dba.stackexchange.com/questions/68459