1

我有这个 SQL 查询,我想要 Excel 2013 表/表中的结果

SELECT 
  [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
FROM 
  (
    SELECT 
      [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
    FROM 
      vwSPLAInformatie
    WHERE 
      (DATEPART(YEAR, fakdat) = '2013') 
      AND (DATEPART(QUARTER, fakdat) = '1')
      AND docnumber LIKE '%kwartaal%'
    GROUP BY 
      [MS Part Number], [Item Name], [Aantal], [Prijs]
    UNION ALL
    SELECT 
      [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
    FROM 
      vwSPLAInformatie
    WHERE 
      (DATEPART(YEAR, fakdat) = '2013') 
      AND (DATEPART(MONTH, fakdat) = '1')
      AND NOT docnumber LIKE '%kwartaal%'
    GROUP BY 
      [MS Part Number], [Item Name], [Aantal], [Prijs]
  ) AS Basis
GROUP BY 
  [MS Part Number], [Item Name], [Prijs]

它工作得很好,唯一的是我有 4 个参数要连接到 Excel 中的 3 个单元格值。(年 = 2013)(季度 = 1)(月 = 2)。但是当我将查询调整为以下内容时:

 WHERE (DATEPART(YEAR, fakdat) = '?')

我在 Excel 中选择了显示 2013 的单元格,所以基本上它应该可以工作,但它没有。我收到以下错误:

[Microsoft]ODBC SQL Server][SQL Server]Conversion failed when converting the varchar value '?' to data type in.

值是相同的,但它根本不起作用。我怎样才能解决这个问题?当这有效时,我会非常高兴!转发,非常感谢。

4

0 回答 0