答案 0 :(得分:0)
尝试以下方法:
select date,
'Completed: '+sum(case [Visit Type] when 'Completed' then [Count] else 0 end)
+' '+char(10)+'Late Cancelled: '+sum(case [Visit Type] when 'Late Cancelled' then [Count] else 0 end)
+' '+char(10)+'Bumped: '+sum(case [Visit Type] when 'Bumped' then [Count] else 0 end)
+' '+char(10)+'No Show: '+sum(case [Visit Type] when 'No Show' then [Count] else 0 end)
+' '+char(10)+'Cancelled: '+sum(case [Visit Type] when 'Cancelled' then [Count] else 0 end)
as comment
from yourtable
group by date
答案 1 :(得分:0)
正如已经说过的那样,并不是你应该接近这种方式,这种分组不会在SQL中完成。
但是,如果您的前端可以处理数据,那么您可以通过将数据分组为XML或JSON来帮助解决问题。看看以下内容:
DECLARE @myTable AS TABLE (Date DATE
,VisitType NVARCHAR(20)
,Count INT
)
INSERT INTO @myTable
VALUES ('2018-6-3', 'Completed', 9),
('2018-6-3', 'Late Cancelled', 1),
('2018-6-3', 'No Show', 2),
('2018-6-13', 'Completed', 5),
('2018-6-13', 'Bumped', 1),
('2018-6-13', 'No Show', 3)
SELECT Date
, (SELECT VisitType, [Count] FROM @myTable B WHERE B.Date = A.Date FOR XML AUTO ) Result
FROM @myTable A
GROUP BY DATE
您可以使用xml参数来获取所需的确切格式。这在网上有很好的记录。