DataTable和数据透视表和过滤器

时间:2013-06-14 13:00:24

标签: c# linq datatable pivot

我有一个像这样的DataTable:

SELECT TOP 1000 [ID]
  ,[NameMachine]
  ,[Reparation]
  ,[Substitution]
  ,[DataIn]
  ,[DataOut]
  ,[Operator]
  ,[TypeOperation]
  ,[Note]
  ,[Department] FROM [Man].[dbo].[Sheets]

我必须过滤它,所以我添加了这个参数:

SELECT ID, NameMachine, Reparation, Substitution, DataIn, DataOut, Operator, TypeOperation, Note, Department FROM dbo.Sheets WHERE Department = @Department AND year(DataIn) = @Year AND Operator =@Operator

现在,我有我过滤的DataTable ......但问题是: 我怎样才能像这样制作一个支点呢?

SELECT NameMachine, Operator, Department, [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] AS Tot, [1] AS Gen, [2] AS Feb, [3] AS Mar, [4] AS Apr, [5] AS Mag, [6] AS Giu, [7] AS Lug, [8] AS Ago, [9] AS Sett, [10] AS Ott, [11] AS Nov, [12] AS Dic
FROM (SELECT ID, NameMachine, datepart(month, DataInizio) AS Month, Operator, Department FROM Sheets) 
m PIVOT (count(ID) FOR Month IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) pvt

我不知道在我的过滤的dataTable中进行类似的查询...如何查询DataTable?

感谢您的任何建议......

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

SELECT NameMachine, Operator, Department, [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] AS Tot, [1] AS Gen, [2] AS Feb, [3] AS Mar, [4] AS Apr, [5] AS Mag, [6] AS Giu, [7] AS Lug, [8] AS Ago, [9] AS Sett, [10] AS Ott, [11] AS Nov, [12] AS Dic
FROM (SELECT ID, NameMachine, datepart(month, DataInizio) AS Month, Operator, Department FROM Sheets WHERE Department = @Department AND year(DataIn) = @Year AND Operator =@Operator) 
m PIVOT (count(ID) FOR Month IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) pvt