[英]Adding total row and Increasing quality for an sql query based on multiple parameters
I have a table in Microsoft Sql Server 2008 like: 我在Microsoft Sql Server 2008中有一张表,如下所示:
id name timelong
1 Eray 2
1 Jack 1
1 Ali 7
1 john 3
1 Roby 5
1 Mike 4
1 Josh 11
What I want to do is to select data based on user multi-selectable parameters. 我要做的是根据用户的多个可选参数选择数据。 Think that there are 4 checkboxes:
0-3,4-6,7-9,10-12
and users can select more than one checkbox. 认为有4个复选框:
0-3,4-6,7-9,10-12
,用户可以选择多个复选框。 Only those data user selected should be seen and a TOTAL row needs to be added at the bottom. 仅应看到用户选择的那些数据,并且需要在底部添加一个TOTAL行。
What I tried is on the bottom but it is not working well - TOTAL row is not there. 我尝试过的是在底部,但效果不佳-TOTAL行不存在。 My question is how I can add the Total row there, and is there any more professional way to provide this query.
我的问题是如何在此处添加“总计”行,还有没有其他更专业的方法来提供此查询。 Thanks.
谢谢。
declare @interval03 bit -- 0 to 3
declare @interval06 bit -- 4 to 6
declare @interval09 bit -- 7 to 9
declare @interval12 bit -- 10 to 12
Select *, sum(timelong)
From myTable
Where (@interval03=1 and timelong<4)
or
(@interval06=1 and timelong>3 and timelong<7)
or
(@interval09=1 and timelong>6 and timelong<10)
or
(@interval12=1 and timelong>9 and timelong<13)
group by id, name
Try grouping sets
: 尝试
grouping sets
:
Select ID, isnull(Name, 'TOTAL'), sum(timelong)
From myTable
Where (@interval03=1 and timelong <= 3)
or
(@interval06=1 and timelong between 4 and 6)
or
(@interval09=1 and timelong between 7 and 9)
or
(@interval12=1 and timelong >= 10)
group by grouping sets ((ID, name), ())
Assuming from your query that timelong
is an int, I've simplified your where
a little as well. 假设您的查询
timelong
是整数,那么我也简化了您的where
。
More information on grouping sets
, rollup
, and cube
: https://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx 有关
grouping sets
, rollup
和cube
grouping sets
更多信息: https : //technet.microsoft.com/zh-cn/library/bb522495(v=sql.105).aspx
TRY This..One more way to add totals... 尝试这个..增加总数的另一种方法...
declare @table Table (id INT,name VARCHAR(10), timelong INT)
insert into @table (id ,name, timelong) VALUES (1, 'Eray', 2)
insert into @table (id ,name, timelong) VALUES (1 ,'Jack' ,1)
insert into @table (id ,name, timelong) VALUES (1 ,'Ali' , 7)
insert into @table (id ,name, timelong) VALUES (1 ,'john' ,3)
insert into @table (id ,name, timelong) VALUES (1 ,'Roby' , 5)
insert into @table (id ,name, timelong) VALUES (1 ,'Mike' ,4)
insert into @table (id ,name, timelong) VALUES (1 ,'Josh' ,11)
declare @interval03 bit=1 -- 0 to 3
declare @interval06 bit -- 4 to 6
declare @interval09 bit -- 7 to 9
declare @interval12 bit -- 10 to 12
DECLARE @result TABLE (ID INT,Name VARCHAR (30),TimeLong INT)
INSERT INTO @result
Select id, name, sum(timelong) timelong
From @table
Where (@interval03=1 and timelong<4)
or (@interval06=1 and timelong>3 and timelong<7)
or (@interval09=1 and timelong>6 and timelong<10)
or (@interval12=1 and timelong>9 and timelong<13)
group by id, name
INSERT INTO @result
SELECT MAX(ID) +100 ID,'Total' Name,SUM(TimeLong) TimeLong
FROM @result
HAVING COUNT(*)<>0
SELECT * FROM @result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.