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
 Query Help

Author  Topic 

Skiman1369
Starting Member

5 Posts

Posted - 2011-02-23 : 16:47:56
I have created a table with 4 columns
CREATE TABLE [dbo].[Accounts](
[Account] [int] NOT NULL,
[Description] [varchar](60) NOT NULL,
[ShortDescription] [varchar](60) NOT NULL,
[Balance] [money] NOT NULL,
PRIMARY KEY CLUSTERED

I have also added the data which was over 400 records. The account numbers range from 100000 to 400000 I need to write 2 queries one that separates the accounts and balances into 4 account ranges (10000's 200000's ect) and totals each one account range with all the other Account numbers and balances included in the results. Then I was looking to write a query that in the result would include all the fields but at the end added a new field of total balance.

I'm stuck I can display all the data in the ranges I want and I can display the total but I can not get them together any help would be awesome.

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-02-23 : 17:50:37
Can you post a simplified sample of data and results you want to get from there?

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

Skiman1369
Starting Member

5 Posts

Posted - 2011-02-23 : 18:35:46

CREATE TABLE Accounts
(
Account int NOT NULL PRIMARY KEY,
Description varchar(60) NOT NULL,
ShortDescription varchar(60) NOT NULL,
Balance money NOT NULL,
)

INSERT INTO Accounts
(Account, Description, ShortDescription, Balance)
VALUES
(100001, 'Petty Cash ', 'Operating Cash - La Jolla', 15008),
(100002, 'Petty Cash ', 'Operating Cash - Del Mar', 13781),
(100003, 'Petty Cash ', 'Operating Cash - Encinitas', 17034),
(200001, 'Boardman Fine Foods', 'La Jolla Bakery Department Vendor', 11612),
(200002, 'Boardman Fine Foods', 'Del Mar Bakery Department Vendor', 24532),
(200003, 'Boardman Fine Foods', 'Encinitas Bakery Department Vendor', 534),
(200011, 'Brooks Surf and Turf', 'La Jolla Meat and Seafood Vendor', 19801),
(300001, 'Owners Equity', 'La Jolla', 8516),
(300002, 'Owners Equity', 'Del Mar', 19370),
(300003, 'Owners Equity', 'Encinitas', 7238);

The results I would be looking for would be

Account Description (s) Balance

100001 Petty Cash Operating Cash La Jolla 15008
100002 Petty Cash Operating Cash Del Mar 13781
100003 Petty Cash Operating Cash Encinitas 17034
100000's account Total 45823 (this can be on the bottom or the the side)


The second query would need to be similar

Account Description (s) Balance

100001 Petty Cash Operating Cash La Jolla 15008
100002 Petty Cash Operating Cash Del Mar 13781
100003 Petty Cash Operating Cash Encinitas 17034
All accounts and descriptions......
Total account total 45823 (this can be on the bottom or the the side)
Go to Top of Page

Skiman1369
Starting Member

5 Posts

Posted - 2011-02-23 : 21:13:14
Is that something thats even possible? or am I just going deal with just having labeled totals?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-02-23 : 22:05:11
add this to your query

sum(Balance) over (partition by Account / 10)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Skiman1369
Starting Member

5 Posts

Posted - 2011-02-24 : 10:06:35
That worked Thank you is there anyway to put a line break in between my totals per account?
Go to Top of Page

Skiman1369
Starting Member

5 Posts

Posted - 2011-02-24 : 10:16:49
Select Account, Balance, sum(Balance) over (partition by Account / 10000)as Total_per_account
From Accounts

This is the code that worked for me Now I'm wondering if I can put a line break between each 10000. Any help would be great.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-02-24 : 10:19:51
quote:
Originally posted by Skiman1369

That worked Thank you is there anyway to put a line break in between my totals per account?


that is a job for your reporting tool not SQL Server


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -