我有一个动态的透视图,每一列的底部都有一个总计,但是我想在每一行的末尾添加一个总计。
我的SQL在下面
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Stats::create([
'user_id' => $user->id
]);
return $user;
}
谢谢!
答案 0 :(得分:0)
我认为最好的解决方案是创建一个UNION,在查询末尾进行求和。我在下面给你一个例子。我不确定我是否为您提供了所有正确的语法,而不知道您的表。另外,日期不能总计,因此我假设您想要天数,因此我使用了一个函数来计算天数。
SELECT cardtype as [Card Type],' + @ColsWithNoNulls + ' FROM
(SELECT ISNULL(CAST(Cardtype AS VARCHAR(30)),''TOTAL'') CardType,
ISNULL(SUM (AllEntries),0) as AllEntries, PRD_ValidDate FROM #Table
GROUP BY cardtype, prd_Validdate
WITH CUBE) src
PIVOT
( SUM(AllEntries)
For PRD_ValidDate IN ('+@Cols+')) piv
UNION
select sum((SELECT ISNULL(CAST(Cardtype AS VARCHAR(30)),''TOTAL'')),
sum(ISNULL(SUM (AllEntries),0)) as AllEntries, days(PRD_ValidDate) FROM
SELECT cardtype as [Card Type],' + @ColsWithNoNulls + ' FROM
(SELECT ISNULL(CAST(Cardtype AS VARCHAR(30)),''TOTAL'') CardType,
ISNULL(SUM (AllEntries),0) as AllEntries, PRD_ValidDate FROM #Table
GROUP BY cardtype, prd_Validdate
WITH CUBE) src
PIVOT
( SUM(AllEntries)
For PRD_ValidDate IN ('+@Cols+')) piv