简体   繁体   中英

Adding total row and Increasing quality for an sql query based on multiple parameters

I have a table in Microsoft Sql Server 2008 like:

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. Only those data user selected should be seen and a TOTAL row needs to be added at the bottom.

What I tried is on the bottom but it is not working well - TOTAL row is not there. 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 :

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.

More information on grouping sets , rollup , and cube : https://technet.microsoft.com/en-us/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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