473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Report By Year

I have a SQL view that shows data by Category and Year - so the data may
look something like this.
Category - Year - Amount
Fruit - 2006 - $12,000
Fruit - 2007 - $16,000
Vegetables - 2006 - $15,000
Vegatables - 2007 - $25,000

Say I want to show that data in Tabular view with Category being the Y-Axis
and Year being the X-Axis, how would I go about doing this? I'm having a
hard time trying to figure out where to start. An Access-style report
doesn't seem to work because I have no way (that I know of) to have a
dynamic X-axis (years may vary based on what report the user requests). Is
the SQL query good enough or do I need to do something with the query in
order to get the years into separate columns (instead of a Year column as
above I would have a 2006 and a 2007 column containing the amounts for those
years)? A full solution may not be necessary if anyone could just point me
in the right direction to start.

Thanks for any assistance!
Ryan
Mar 20 '07 #1
8 1661
Ryan wrote:
I have a SQL view that shows data by Category and Year - so the data may
look something like this.
Category - Year - Amount
Fruit - 2006 - $12,000
Fruit - 2007 - $16,000
Vegetables - 2006 - $15,000
Vegatables - 2007 - $25,000

Say I want to show that data in Tabular view with Category being the Y-Axis
and Year being the X-Axis, how would I go about doing this?
<snip>

Yeah, this is the lacking feature in SQL Server that I miss the most
from my Access days (I'm assuming you're using SQL Server).

I don't think SQL Server will create dynamic columns for you as the
Access cross-tab did. Nevertheless, if you know the years in advance,
maybe you can use something like this (adapted straight from the
docs):

Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category

And if you got this far, having a Totals columns is just a query away:

Select A.*, Total = (A.[2004] + A.[2005] + A.[2006] + A.[2007])
From (
Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category
) A

HTH.

Regards,

Branco.

Mar 20 '07 #2
Thanks for the advice. Knowing the years in advance could be an issue,
because I want to allow the user to select the years they want to view (a
set of years in sequential order, ex. 2006, 2007, 2008). Though I suppose
this could still be done in SQL using a Stored Procedure with a query
similar to what you have here. I'm using a reporting tool (Component One
Reports) which is very similar to Access so I'll keep poking around on that
to see if theres an easier solution on the front-end (report) side.

Ryan

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...
Ryan wrote:
>I have a SQL view that shows data by Category and Year - so the data may
look something like this.
Category - Year - Amount
Fruit - 2006 - $12,000
Fruit - 2007 - $16,000
Vegetables - 2006 - $15,000
Vegatables - 2007 - $25,000

Say I want to show that data in Tabular view with Category being the
Y-Axis
and Year being the X-Axis, how would I go about doing this?
<snip>

Yeah, this is the lacking feature in SQL Server that I miss the most
from my Access days (I'm assuming you're using SQL Server).

I don't think SQL Server will create dynamic columns for you as the
Access cross-tab did. Nevertheless, if you know the years in advance,
maybe you can use something like this (adapted straight from the
docs):

Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category

And if you got this far, having a Totals columns is just a query away:

Select A.*, Total = (A.[2004] + A.[2005] + A.[2006] + A.[2007])
From (
Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category
) A

HTH.

Regards,

Branco.

Mar 20 '07 #3
Post your question to microosft.public.sqlserver.programming. I believe you
*can* do this with a SQLServer query, and they would be the ones to tell
you how.

Then come back and post the answer here so we know, too. ;-)

Robin S.
------------------------------------------------------
"Ryan" <Ty****@newsgroups.nospamwrote in message
news:ug**************@TK2MSFTNGP05.phx.gbl...
Thanks for the advice. Knowing the years in advance could be an issue,
because I want to allow the user to select the years they want to view (a
set of years in sequential order, ex. 2006, 2007, 2008). Though I
suppose this could still be done in SQL using a Stored Procedure with a
query similar to what you have here. I'm using a reporting tool
(Component One Reports) which is very similar to Access so I'll keep
poking around on that to see if theres an easier solution on the
front-end (report) side.

Ryan

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...
>Ryan wrote:
>>I have a SQL view that shows data by Category and Year - so the data
may
look something like this.
Category - Year - Amount
Fruit - 2006 - $12,000
Fruit - 2007 - $16,000
Vegetables - 2006 - $15,000
Vegatables - 2007 - $25,000

Say I want to show that data in Tabular view with Category being the
Y-Axis
and Year being the X-Axis, how would I go about doing this?
<snip>

Yeah, this is the lacking feature in SQL Server that I miss the most
from my Access days (I'm assuming you're using SQL Server).

I don't think SQL Server will create dynamic columns for you as the
Access cross-tab did. Nevertheless, if you know the years in advance,
maybe you can use something like this (adapted straight from the
docs):

Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category

And if you got this far, having a Totals columns is just a query away:

Select A.*, Total = (A.[2004] + A.[2005] + A.[2006] + A.[2007])
From (
Select Category,
[2004] = Sum(Case [year] when 2004 then amount else 0 end),
[2005] = Sum(Case [year] when 2005 then amount else 0 end),
[2006] = Sum(Case [year] when 2006 then amount else 0 end),
[2007] = Sum(Case [year] when 2007 then amount else 0 end)
From YourView
Group by Category
) A

HTH.

Regards,

Branco.


Mar 20 '07 #4
Hi Ryan,

For the report you want to present, I think the SQL Server reporting
service is quite good for present such summary data. And the Matrix data
region in Reporting Service is the exact one that can present the data view
you want. You can have a look at the following reference:

#SQL Server 2005 Books Online Working with Matrix Data Regions
http://msdn2.microsoft.com/en-us/library/ms157334.aspx

#Reporting Services - Getting the Matrix to Display Two Subtotals for the
Same Group
http://www.sqlskills.com/blogs/liz/2...sGettingTheMat
rixToDisplayTwoSubtotalsForTheSameGroup.aspx

Also, if you do not want to install reporting service, and only want to
present such a report in your client application, Visual Studio 2005 has
provided a reportviewer control(for both ASP.NET and winform application)
that can display client report(use the same template syntax as Sql server
reporting service report). Here are some useful articles discussing on
using ReportViewer control of VisualStudio 2005:

#Adding Data Regions to a ReportViewer Report
http://msdn2.microsoft.com/en-us/lib...86(VS.80).aspx

#Adding Matrix Data Regions
http://msdn2.microsoft.com/en-us/lib...09(VS.80).aspx

#Build Client-Side Reports Easily
http://www.ftponline.com/vsm/2005_11...res/rjennings/

#ReportViewer Control in Visual Studio 2005
http://gotreportviewer.com/

Hope this helps. If you have any more specific questions on this, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Mar 21 '07 #5
Steven,

Thanks this is just what I needed. I'm able to create the matrix to get the
data I want. The downsides so far is that I am having trouble getting the
column headers for my static fields to show up, the matrix fields that have
no data need to show 0 but they're showing up as blanks, and I'm not real
fond of the report designer interface. But I guess those are all things
I'll get used to and/or find a fix for eventually.

Ryan

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:Nk**************@TK2MSFTNGHUB02.phx.gbl...
Hi Ryan,

For the report you want to present, I think the SQL Server reporting
service is quite good for present such summary data. And the Matrix data
region in Reporting Service is the exact one that can present the data
view
you want. You can have a look at the following reference:

#SQL Server 2005 Books Online Working with Matrix Data Regions
http://msdn2.microsoft.com/en-us/library/ms157334.aspx

#Reporting Services - Getting the Matrix to Display Two Subtotals for the
Same Group
http://www.sqlskills.com/blogs/liz/2...sGettingTheMat
rixToDisplayTwoSubtotalsForTheSameGroup.aspx

Also, if you do not want to install reporting service, and only want to
present such a report in your client application, Visual Studio 2005 has
provided a reportviewer control(for both ASP.NET and winform application)
that can display client report(use the same template syntax as Sql server
reporting service report). Here are some useful articles discussing on
using ReportViewer control of VisualStudio 2005:

#Adding Data Regions to a ReportViewer Report
http://msdn2.microsoft.com/en-us/lib...86(VS.80).aspx

#Adding Matrix Data Regions
http://msdn2.microsoft.com/en-us/lib...09(VS.80).aspx

#Build Client-Side Reports Easily
http://www.ftponline.com/vsm/2005_11...res/rjennings/

#ReportViewer Control in Visual Studio 2005
http://gotreportviewer.com/

Hope this helps. If you have any more specific questions on this, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.


Mar 21 '07 #6
Thanks for your reply Ryan,

For column header of static columns, I've ever found some existing
discussion on this and static columns seems hasn't header text setting as
dynamic column. However, someone has been able to use the top/left cell to
add some header text for all those static columns.

In addition, here are some other useful resource on reportviewer based
client report designing:

#Walkthrough: Creating a ReportViewer Report
http://msdn2.microsoft.com/en-us/lib...73(VS.80).aspx

#Defining a Report Layout
http://msdn2.microsoft.com/en-us/lib...86(VS.80).aspx

#ReportViewer User Interface Reference
http://msdn2.microsoft.com/en-us/lib...80(VS.80).aspx

http://blogs.msdn.com/ChrisHays/

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.




Mar 22 '07 #7
Hi Steven,

Thats exactly the workaround I ended up using, I put a table in the upper
left cell of the matrix that had static header names. Thanks for the links!

Ryan
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:7C*************@TK2MSFTNGHUB02.phx.gbl...
Thanks for your reply Ryan,

For column header of static columns, I've ever found some existing
discussion on this and static columns seems hasn't header text setting as
dynamic column. However, someone has been able to use the top/left cell to
add some header text for all those static columns.

In addition, here are some other useful resource on reportviewer based
client report designing:

#Walkthrough: Creating a ReportViewer Report
http://msdn2.microsoft.com/en-us/lib...73(VS.80).aspx

#Defining a Report Layout
http://msdn2.microsoft.com/en-us/lib...86(VS.80).aspx

#ReportViewer User Interface Reference
http://msdn2.microsoft.com/en-us/lib...80(VS.80).aspx

http://blogs.msdn.com/ChrisHays/

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.




Mar 22 '07 #8
You're welcome :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Mar 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AstrA | last post by:
Hi All Wondered if you could help. Basically I have 2 tables that contain all the data I want for my report, but I need to put it in a particular way and I need to display it in an ASP page...
1
by: Rajani | last post by:
Hello, I have a table(msaccess) with the structure... job_code text 6 style text 10 qty number fabrication text 65 ship_date date/time
5
by: Tony Williams | last post by:
I have a table "tblmaintabs" that stores data that is collected from various companies on a quarterly basis in March, June, September and December each year (these dates are stored in a Date/time...
0
by: Rahul Chatterjee | last post by:
Hello All I have designed a dotnet application using VB which basically takes a selection and passes value to a crystal report which in turn passes the value to a Stored procedure. After the...
2
by: john | last post by:
My report is based on a query with user input . In my report I have a text box with the following code: =. This works when the query outcome is 1 or more records. But when the query output is zero...
12
by: kabradley | last post by:
Hello, Thanks for looking at my post and hopefully having an answer or at least a suggestion to my problem. I currently work at a financial planning office that deals with many clients and accounts....
6
by: DavidOwens | last post by:
Sub PreviewReport_Click() On Error GoTo Err_PreviewReport_Click Dim stRepName As String: Rem Holds the Report name Dim stDispId As String: Rem Holds the Dispenser ID Dim...
69
by: kabradley | last post by:
Alrighty Guys and Gals, I have another question that I hope you all can help me with. I have a report that uses a cross-tab query as its record source. This cross-tab query is getting all of its...
5
by: LadyIlsebet | last post by:
I'm not a fantastic Access developer, but I'm trying to help get Inventory and whatnot organized at work. They are used to 5 year budget plans that list out exactly what has to be purchased what...
9
by: magickarle | last post by:
Hi, I have a database in access with the following columns: Day AgentID ManagerID Grade They got information about agents and some grades. I would like to have ONE form with several...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.