简体   繁体   English

在SQL中将结果分组

[英]Grouping results within SQL

I was thinking about doing this in crystal, but am thinking about doing it another way now. 我本来想以水晶的方式进行此操作,但现在正在考虑以另一种方式进行此操作。 This is the way I was thinking about doing it before. 这就是我以前考虑做的方式。 Count by group in crystal reports 在水晶报表中按组计数

I am pulling by transactions and need a count of unique people, but in the report they want to show the count of individual people and then show their services below as shown in the image. 我忙于交易,需要一定数量的独特人员,但是在报告中,他们想要显示单个人员的数量,然后在下面显示他们的服务,如图所示。 What I'd like to do is somehow get a count of unique users that I could just throw in at the bottom. 我想做的就是以某种方式获得一些唯一的用户,我可以将它们排在底部。 Crystal won't allow me to do a count by group and users will have duplicates in the format they want it displayed. Crystal不允许我按组进行计数,并且用户将以他们希望显示的格式进行重复。 I am hopping that I can group it in the code then just add it to the bottom of the report. 我希望可以将其分组在代码中,然后将其添加到报告的底部。 I hope I'm getting across what I'm trying to accomplish. 我希望我能理解我要完成的工作。 If I can somehow just add the total of unique users at the bottom of the report it will finish it for me. 如果我能以某种方式在报表底部添加唯一身份用户总数,它将为我完成此操作。 Thanks in advance. 提前致谢。

在此处输入图片说明

select 

distinct p.patient_id,
pa.fname as 'Patient',
p.clinic_id,
p.service_id,
p.program_id,
p.protocol_id,
p.discharge_reason,
p.date_discharged

from patient_assignment p
join patient pa
on p.patient_id = pa.patient_id
where p.program_id not in ('TEST', 'SA', 'INTAKE' ) and (p.date_discharged between '2013-01-01 00:00:00.000' and '2013-06-01 00:00:00.000')
and p.patient_id not in ('00000004', '00001667', '00020354')

This is too long for a comment. 这个评论太长了。

SQL Queries have fixed columns. SQL查询具有固定的列。 You can't "just throw" a different type of row at the bottom. 您不能在底部“仅抛出”另一种类型的行。 Although there are SQL solutions (such as concatenating all the fields to make a single row), these are not very palatable. 尽管有SQL解决方案(例如将所有字段串联在一起形成一行),但这些解决方案并不是很好用。

Instead, here are some other ways. 相反,这里有一些其他方法。

You can run another query to get the count. 您可以运行另一个查询以获取计数。

You can query the "result" set to get the count (perhaps after fetching all the rows). 您可以查询“结果”集以获取计数(也许在获取所有行之后)。

You can do some minor coding work in the application. 您可以在应用程序中进行一些次要的编码工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM