如何在sql语句中选择特定列

时间:2012-01-03 08:42:10

标签: c# sql sql-server sql-server-2005 dataset

我需要删除两行中值为'0'的列

select  DateString as 'Date',machine, 
sum (case when vfrm.job_id = '105' then (total_hours) else '0' end)as A,           
sum (case when vfrm.job_id = '100' then (total_hours) else '0' end) as B,        
sum (case when vfrm.job_id = '101' then (total_hours) else '0' end) as C,         
sum (case when vfrm.job_id = '102' then (total_hours) else '0' end) as D,          
sum (case when vfrm.job_id = '103' then (total_hours) else '0' end) as E,              
sum (case when vfrm.job_id = '134' then (total_hours) else '0'  end) as F,             
sum (case when vfrm.job_id = '135' then (total_hours) else '0' end) as G ,
sum (case when vfrm.job_id = '9995' then (total_hours) else '0' end) as OTHERS     
 from    ven_fullreportmaster vfrm                    
             INNER JOIN ven_descriptionmaster VDM ON VDM.description_id = vfrm..description_id                    
             inner join ven_machinemaster vm on  vm.machine_id = vfrm..machine_id        
             inner join ven_jobcodemaster vjm on vjm.job_id = vfrm.job_id            
             inner join Dim_Time dt on dt.Date_Id = vfrm.date_id                  
             where  vfrm.entry_date = '20111208'                   
                     and  vfrm.shift_id =2     
                     and vfrm.description_id in (1,3,5)     
                     and vfrm.job_id not in (9999,9998,9996,9994)                              
                     and vjm.job_status ='ACTIVE'                      
    group by   description_name, DateString  

输出:

enter image description here
请帮帮我解决这个问题..

此致

T.Navin

更新

我将上面的sql查询结果保存到c#中的数据集中。 是否可以过滤两行中包含'0'的列...因此数据集看起来像快照中的所需输出

2 个答案:

答案 0 :(得分:0)

这可能是最好留给SQL Server Reporting Services,Crystal Reports等报告工具的东西。但是,如果它需要完成并且您没有使用报告工具,那么至少是可能的。< / p>

在C#端,您可以通过循环访问DataTable的Columns集合,从DataSet中的基础DataTable中删除列。这将需要查看每个字段的两行(或使用Linq查询),然后删除列。我没有试过这个,所以我不确定它的有效性或它是多么简单/复杂。您可以在此处找到有关DataTable结构的更多详细信息:

http://msdn.microsoft.com/en-us/library/system.data.datatable.columns.aspx

如果您可以选择调用存储过程,则可以执行@Mikael在问题的第一条评论中建议的多步操作。以下是我将如何从T-SQL方面处理这个问题:

CREATE TABLE #TempResults ([Date] VARCHAR(30), Machine VARCHAR(50),
    [A] INT, [B] INT, [C] INT, [D] INT, [E] INT, [F] INT, [G] INT,
    [OTHERS] INT)

INSERT INTO #TempResults ([Date], Machine, A, B, C, D, E, F, G, OTHERS)
    SELECT  DateString AS [Date],
            machine, 
        SUM(CASE WHEN vfrm.job_id = '105' THEN (total_hours) ELSE '0' END) AS [A],
        SUM(CASE WHEN vfrm.job_id = '100' THEN (total_hours) ELSE '0' END) AS [B],
        SUM(CASE WHEN vfrm.job_id = '101' THEN (total_hours) ELSE '0' END) AS [C],
        SUM(CASE WHEN vfrm.job_id = '102' THEN (total_hours) ELSE '0' END) AS [D],
        SUM(CASE WHEN vfrm.job_id = '103' THEN (total_hours) ELSE '0' END) AS [E],
        SUM(CASE WHEN vfrm.job_id = '134' THEN (total_hours) ELSE '0'  END) AS [F],
        SUM(CASE WHEN vfrm.job_id = '135' THEN (total_hours) ELSE '0' END) AS [G],
        SUM(CASE WHEN vfrm.job_id = '9995' THEN (total_hours) ELSE '0' END) AS [OTHERS]
    FROM    ven_fullreportmaster vfrm
    INNER JOIN  ven_descriptionmaster vdm
            ON  vdm.description_id = vfrm.description_id
    INNER JOIN  ven_machinemaster vm
            ON  vm.machine_id = vfrm.machine_id
    INNER JOIN  ven_jobcodemaster vjm
            ON  vjm.job_id = vfrm.job_id
    INNER JOIN  Dim_Time dt
            ON  dt.Date_Id = vfrm.date_id
    WHERE   vfrm.entry_date = '20111208'
    AND     vfrm.shift_id = 2
    AND     vfrm.description_id IN (1, 3, 5)
    AND     vfrm.job_id NOT IN (9999, 9998, 9996, 9994)
    AND     vjm.job_status = 'ACTIVE'
    GROUP BY    description_name, DateString

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = N'SELECT [Date], Machine, '

IF (EXISTS (SELECT * FROM #TempResults WHERE [A] > 0))
BEGIN
    SET @SQL = @SQL + N'[A], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [B] > 0))
BEGIN
    SET @SQL = @SQL + N'[B], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [C] > 0))
BEGIN
    SET @SQL = @SQL + N'[C], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [D] > 0))
BEGIN
    SET @SQL = @SQL + N'[D], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [E] > 0))
BEGIN
    SET @SQL = @SQL + N'[E], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [F] > 0))
BEGIN
    SET @SQL = @SQL + N'[F], '
END

IF (EXISTS (SELECT * FROM #TempResults WHERE [G] > 0))
BEGIN
    SET @SQL = @SQL + N'[G], '
END

SET @SQL = @SQL + N'[OTHERS] FROM #TempResults'

EXEC (@SQL)
GO

P.S。您的GROUP BY子句似乎存在问题,因为我希望在其中需要[Machine]字段。

答案 1 :(得分:0)

我已经过滤了我的数据集,其中包含了&#39; 0&#39;在列.. 我引用了这个网站,以获得输出

http://geekswithblogs.net/dotNETvinz/archive/2009/08/11/removing-columns-in-datatable.aspx