Question

SQL Reporting Services and combining 2 datasets into a table

Asked by: jbrowell

Hello, I'm a total SQL beginner, learning fast but needing some help with a report I'm creating.

The report is for a job ledger and the SQL table includes JobNo, Date, CostCode, TotalCost amongst others. I created a dataset with the mentioned headers between two dates and a job number specified by the user. Then in the Layout section very cleverly entered the data into a table grouped by the cost code, added some totals and was feeling very pleased with myself and my first 'real' report.

Now the FD wants the same table to include the total costs for each cost code BEFORE the specified start date. ie the "before total" next to the "specified date total" for each cost code then added together to give a running total for the total cost for that code.

I have created another dataset (called RunningCost) from the same SQL table to include the costs from the beginning of time (20010101) to the start date given by the user. And I have created another table in Layout, again grouped by the cost code. The results I get out from this table are exactly what I want. The problem is I cannot get this info into the first table.

I can drop in:
=Sum(Fields!TotalCost.Value, "RunningCost")
to the first table within the group structure, but this does not give the total for each cost code, only puts in the complete total for everything previous at the end of each cost code section.

Is there any way of adding the data to the first table so it is also filtered by the groups or can you combine the 2 datasets into one in such a way that would allow me to create the desired results.

Many thanks for any help in advance,
John

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-07-29 at 09:27:31ID23604620
Tags

Microsoft

,

SQL Server 2005 Reporting Services

Topics

Databases Miscellaneous

,

SQL Query Syntax

,

MS SQL Server

Participating Experts
3
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Merge Datasets
    Hi, I have two datasets. Both have two columns with the same data structures. The first column in these two datasets are exactly identical. I want to merge them into one dataset. The result dataset will have three columns. The first column in the result dataset will come fro...
  2. DataSets
    When do the DataSets created as a project Datasource get read: When the specific form that utuilzies it requests the infroamtion or when the application is started? Are these good to use or should I use another form to access the SQL Server data? This will be a multiuser ap...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Rick_RickardsPosted on 2008-07-29 at 22:32:40ID: 22117693

Basically it sounds like you'd like data from one query to be included inside another.  Can that be done (usually).  The explanation could be a bit to follow and certainly a lot clearer if it included the solution.  Can you post
1) post both queries
2) Identify which is which and
3) Identify what columns you'd like included in the finished product?

 

by: jbrowellPosted on 2008-07-30 at 02:43:45ID: 22118725

Thanks for replying Rick. Here's the details you requested with a few more details.
I am using Visual Studio to create the report.

Here is the first query:
SELECT     [Job No_], [Global Dimension 2 Code], [Posting Date], [Document No_], Description, [Total Cost], [Total Price], LEFT([Global Dimension 2 Code], 1)
                      AS Expr1, [Source Name]
FROM         [Company$Job Ledger Entry]
WHERE     ([Job No_] = '0218') AND ([Posting Date] BETWEEN '20080601' AND '20080630')
ORDER BY [Global Dimension 2 Code]

(For testing and getting this up and running I removed the user inputs and used the examples shown in the WHERE statement, in this case Job 0218 for June 2008).

[Global Dimension 2 Code] is the cost code and is of the format 101, 102, 103, 201, 202, 301, 302 etc.

In the Layout section I added a table and inserted two Groups. The first was grouped by Expr1 to group 100, 200, 300 etc. The second was by [Global Dimension 2 Code]. This way I can total each cost code and each section. The columns are Posting Date, Source, Document No, Description and Cost (which is Total Cost + Total Price). All fine.

As mentioned before I also want to add next to each cost code total the sum of the costs BEFORE the entered dates. With our example, all costs before June. So you can see for any given code the costs before June, the June costs then a final column in which they are added together.

To get the data I created a second query called RunningCost:
SELECT     [Global Dimension 2 Code] AS RCGD2Code, [Total Cost] AS RCTotalCost, [Total Price] AS RCTotalPrice, LEFT([Global Dimension 2 Code], 1)
                      AS RCExpr1
FROM         [Company$Job Ledger Entry]
WHERE     ([Job No_] = '0218') AND ([Posting Date] BETWEEN '20010101' AND '20080531')
ORDER BY RCGD2Code

I now need to add the previous cost (RCTotalCost + RCTotalPrice) for each cost code into the table. My problem is I cannot get it to recognise the group structure in the table. With the output being the TOTAL total cost for everything not just the cost code total cost or section total.

Recap of the table structure:
First grouped into cost code sections, 100, 200, 300 etc
Then grouped in to each cost code 101, 102, 201, 202 etc
Columns for the cost code ectries are:
Posting Date, Source, Document No, Description, Cost (Total Cost + Total Price)
A cost code total at the end of each code and another at the end of each section
(All this works fine)
Now need to enter previous cost code totals next to the current totals
Further column to add these together.

Cheers for your time and help,
John

 

by: YurichPosted on 2008-08-07 at 13:21:27ID: 22184964

Oh, I have to say that you confused me completely with your detailed explanation. However, I can tell you the following on the subject:

1. You CANNOT use two datasets on the same dataobject (e.g. table, list, or matrix).

2. You can use one of the work arounds, such as:

a. Create a subreport and place it in the cell of your report - than you can use different datasets and you can pass a parameter from your main report to you subreport to synchronize them.

b. Create several tables and place 'em one right after another - it will create illusion of one table (e.g. table to list details from ds1, table to list totals from ds2, table to list totals from ds1)

c. Figure out how to do it with groups and one dataset.

It sounds like you can do it just using the latter way (c), but as I mentioned earlier, I'm a bit confused with what you exactly want.

You can try to abstract and simplify your query for the sake of experiment and then I can probably suggest something. Leave only necessary fields and give us example of your result set. Then show us a sample result set of what you're trying to achieve in your report.

Cheers,
Yurich

 

by: jbrowellPosted on 2008-08-08 at 02:23:15ID: 22188440

Many thanks for the reply Yurich. Sorry for the confusion. I was worried that no one would understand my rambling explainations. I'll try and show an abstract example.

Here's the data:
JobLedger
JobNo   CostCode   Date           Cost
218       101            20080514   200
218       101            20080605   100
218       103            20080524   300
218       102            20080413   200
218       101            20080621   300
218       103            20080616   200
218       103            20080401   100
218       102            20080625   400
218       101            20080322   200
218       102            20080607   100

I use the query:
SELECT     JobNo, CostCode, Date, Cost
FROM         [Company$JobLedger]
WHERE     (JobNo = '0218') AND (Date BETWEEN '20080601' AND '20080630')
ORDER BY CostCode

(With the final version JobNo and Date will be entered via parameters)

In the Layout section of Visual Studio I add a table and insert a group based on the CostCode. This shows the CostCode items grouped together and allows me to total each group.

Example report output:
Job               Code             Date              Cost
218               101               05/06/2008    100
218               101               21/06/2008    300
                                                    Total -  400                    

218               102               07/06/2008   100
218               102               25/06/2008   400
                                                   Total -  500

218               103               16/06/2008   200
                                                   Total -  200

This I can do. All looks fine.

What the FD also wants is the cost total of the data before the given dates next to each section total and then add to the current total to give a new total.

He wants it to look like this:
Job               Code             Date              Cost  Previous Total    New Total
218               101               05/06/2008    100
218               101               21/06/2008    300
                                                    Total -  400           400                   800              

218               102               07/06/2008   100
218               102               25/06/2008   400
                                                   Total -  500           200                   700

218               103               16/06/2008   200
                                                   Total -  200           400                  600

I can extract the data (sort of, not sure of how to do BEFORE a data yet, but not to worry, it can wait for now) using:
SELECT     CostCode AS RCCode, Cost AS RCCost,
FROM         [Company$JobLedger]
WHERE     (JobNo = '0218') AND (Date BETWEEN '20010101' AND '20080531')
ORDER BY RCCode

I add SUM of RCCost in to the group structure of the table, but it ignores the grouping for the CostCode and gives the complete total eg 1000 after each section.

I have also tried taking out all the data with one query for all dates and then using group FILTER to only show the required dates that way. But was running into a lot of problems, tho maybe that was to do with UK and US date formats and may be a better way to resolve this. Not sure. I am open to suggestions!

I hope that has made things a little clearer. Any help with this is greatly appreciated.

Thanks for your time looking at this.

Cheers,
John

 

by: YurichPosted on 2008-08-10 at 20:25:23ID: 22201658

Ooph...

I see what you want know but it took me a while to comprehend it all :\

It is doable, but you have to do it at the backend. You can't do it in MS RS. At least i couldn't see how to - the reason is very simple. You can't mix two datasets in one data regions (table in your case) and you can't use two table either as you want to have totals for each cost code.

Now how to do it. You'll need to get all previous costs first, then join on them for your actual selection. Then, you'll have in each row for each cost code totals for your previous costs. I'm attaching a script for this. It's not a mock script, I have created a table with the data you provided and added some more data with earlier dates. So, the script is working and returns this output:

JobNo  CostCode Date                             Cost      PrevCost
218      101      2008-06-05 00:00:00.000      100.00      1086.00
218      101      2008-06-21 00:00:00.000      300.00      1086.00
218      102      2008-06-25 00:00:00.000      400.00      737.00
218      102      2008-06-07 00:00:00.000      100.00      737.00
218      103      2008-06-16 00:00:00.000      200.00      923.00

The last column is the sum of all previous costs. In the report, don't show this column, but in the footer of your group use First() to display "Previous Total" and add it to the sum of your Cost to show "New Total"

Good luck ;)
Yurich

DECLARE @JobNo varchar( 8 )
DECLARE @DateFrom datetime
DECLARE @DateTo datetime
SELECT @JobNo = '0218'
SELECT @DateFrom = '2008-06-01'
SELECT @DateTo = '2008-06-30'
 
 
SELECT ledger.JobNo, ledger.CostCode, 
 ledger.Date, ledger.Cost,				
 prev_costs.PrevCost					-- previous cost sum
FROM dbo.JobLedger as ledger
LEFT OUTER JOIN
(
	SELECT CostCode, SUM( Cost ) as PrevCost	-- select the sum of all costs
	FROM dbo.JobLedger				-- before the date from = 
	WHERE JobNo = @JobNo				-- making it previous cost
	AND Date < @DateFrom
	GROUP BY CostCode
) as prev_costs
	ON ledger.CostCode = prev_costs.CostCode
 
WHERE  ledger.JobNo = @JobNo
AND ledger.Date BETWEEN @DateFrom AND @DateTo
ORDER BY ledger.CostCode

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: jbrowellPosted on 2008-08-11 at 04:21:36ID: 31481286

Brilliant! Thanks Yurich. I have adapted it for my original code and it does exactly what I wanted.

Thanks again for all your time and patience,
John

 

by: YurichPosted on 2008-08-11 at 04:47:07ID: 22203480

I'm glad it worked for you :)
You're welcome

 

by: kalbalPosted on 2010-09-15 at 19:42:09ID: 33688224

Hi
I am just another programmer looking for a solution to my exact same problem. I used Yurich's code and it worked like a charm.

Thanks Yurich!!!

 

by: YurichPosted on 2010-09-15 at 22:55:51ID: 33688869

my pleasure :)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...