Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 displaying sum at the bottom of the table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-11-09 : 08:20:16
neo writes "hi
i am a beginner at sql
this query confuses me...

q:A LIST OF SERVICE TRANSACTIONS(WITH A FULL DESCRIPTION OF THE SERVICE) ON A PARTICULAR DAY ALONG WITH THE NAMES OF THE CUSTOMER AND THEIR ROOM NUMBERS.THE TOTAL AMOUNT TRANSACTED NEEDS TO BE DISPLAYED AT THE BOTTOM OF THE LIST

REQD TABLES ARE
SERVICES,TRANSACTIONS AND RESERVATIONS...

all i can do was this answer...

SELECT T.TRANID,S.*,R.FNAME,R.LNAME,R.ROOMID FROM SERVICES AS S ,RESERVATIONS AS R,TRANSACTIONS AS T
WHERE S.SERNO=T.SERNO AND T.CUSTID=R.CUSTID ORDER BY T.TRANID
COMPUTE SUM(S.SERCHAR)

is there a better way to display in the same table the sum too?"

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-11-09 : 09:59:52
look up WITH ROLLUP

Jay White
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-11-09 : 10:22:46
WITH ROLLUP requires that you be grouping which in this case will get tricky.

You can just use a UNION to add the extra row, put NULL or a literal value in all the other columns. Something like

SELECT T.TRANID,S.*,R.FNAME,R.LNAME,R.ROOMID FROM SERVICES AS S ,RESERVATIONS AS R,TRANSACTIONS AS T
WHERE S.SERNO=T.SERNO AND T.CUSTID=R.CUSTID ORDER BY T.TRANID
UNION ALL
SELECT NULL, NULL, NULL, SUM(S.SERCHAR), NULL, NULL, NULL
FROM SERVICES AS S ,RESERVATIONS AS R,TRANSACTIONS AS T
WHERE S.SERNO=T.SERNO AND T.CUSTID=R.CUSTID ORDER BY T.TRANID

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-11-09 : 12:02:26
You could group by the entire rowset

Jay White
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-11-09 : 12:59:54
quote:
Originally posted by Page47

You could group by the entire rowset

Jay White



Yeah, that's what I defined as will get tricky
Go to Top of Page
   

- Advertisement -