0

我有这个来自多表的视图

account_name.......奖金.............价值

客户1............A............14000

客户1............B............500

客户1............C............14500

客户2............A............20000

客户2............B............200

客户2............C............20200

http://im33.gulfup.com/Nt0mM.png

如何使用 LINQ 检索此视图并在 GridView 上显示此视图

....................A........B........C

客户1.......14000.......500.......14500

客户2.......20000......200.......20200

在此处输入图像描述

4

2 回答 2

1
SELECT account_name, 
SUM(CASE WHEN bonus = 'A' THEN value ELSE 0 END) AS A,
SUM(CASE WHEN bonus = 'B' THEN value ELSE 0 END) AS B,
SUM(CASE WHEN bonus = 'C' THEN value ELSE 0 END) AS C
FROM YouView
GROUP BY account_name
于 2013-04-17T23:59:16.667 回答
1

我使用 realnumber3012 推荐的动态 Pivots 解决了我的问题...

表格的演示和此链接上的结果

在此处输入图像描述


DECLARE @cols NVARCHAR(2000)
SELECT  @cols = COALESCE(@cols + ',[' + bounse_name+ ']','[' + bounse_name + ']')
FROM    dbo.tbl_bounse
DECLARE @query NVARCHAR(4000)
SET @query = N'SELECT account_name , '+ @cols +'
FROM
(SELECT     tbl_account.account_name, tbl_account.account_career,     
            tbl_bounse.bounse_name, tbl_detail.detail_value
FROM         tbl_account INNER JOIN
tbl_detail ON tbl_account.account_id = tbl_detail.detail_accound_id 
INNER JOIN
tbl_bounse ON tbl_detail.detail_bounce_id = tbl_bounse.bounse_id                     
                   )  p
PIVOT
(
MAX([detail_value])
FOR bounse_name IN
( '+
@cols +' )
) AS pvt;'

EXECUTE(@query)
于 2013-04-18T20:56:08.457 回答