473,396 Members | 1,766 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,396 software developers and data experts.

Access Query of a Table

I have an access table in the following format:
Project_ID January February March .......
1 $1000 $2000 $1500
2 $0 $150 $345
3 $500 $600 $250
.
.
.

I want to have the data in the following format:
Project_ID Month Revenue
1 January $1000
1 February $2000
1 March $1500
2 January $0
2 February $150
2 March $345
3 January $500
3 Febuary $600
3 March $250
.
.
.
Is there a way to do this? I can replace the Month Names with Number such as 01 for january, 02 for February, 03 for March.....if the names are an issue.

Thanks,

Bilal
Mar 21 '12 #1

✓ answered by Luuk

UNION ALL is to 'connect' two SQL statements together, one before the union, and one after the union....

Expand|Select|Wrap|Line Numbers
  1. select id, 'jan', january from ExpectedRevenue
gives something like:
1, 'jan', $1000
2, 'jan', $2000

and:
Expand|Select|Wrap|Line Numbers
  1. select id, 'feb', februari from ExpectedRevenue
gives something like:
5, 'feb', $1500
6, 'feb', $1750

then:
Expand|Select|Wrap|Line Numbers
  1. select id, 'jan', january from ExpectedRevenue
  2. union all
  3. select id, 'feb', februari from ExpectedRevenue
gives:
1, 'jan', $1000
2, 'jan', $2000
5, 'feb', $1500
6, 'feb', $1750

6 1886
Rabbit
12,516 Expert Mod 8TB
Use a union query.
Expand|Select|Wrap|Line Numbers
  1. SELECT 'Jan' AS RevMonth, field2
  2. FROM someTable
  3.  
  4. UNION ALL
  5.  
  6. SELECT 'Feb' AS RevMonth, field3
  7. FROM someTable
Mar 21 '12 #2
Can you give the exact query for one record and i will duplicate for the rest. I am still learning and am getting error messages.

Thanks
Mar 22 '12 #3
NeoPa
32,556 Expert Mod 16PB
Bilal:
Can you give the exact query for one record
Rabbit's done that already pretty much (He posted examples for Jan and Feb in fact). Including the table name is impossible at this stage as you haven't told us what that is. A slightly closer match to your question might be :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Project_ID], 'January' AS [Month], [January]
  2. FROM   [YourTable]
  3.  
  4. UNION ALL
  5.  
  6. SELECT [Project_ID], 'February' AS [Month], [February]
  7. FROM   [YourTable]
  8.  
  9. UNION ALL
  10.  
  11. ...
PS. If you still have problems then say just that is very little help. We would need the SQL you are actually using and a full indication of the problem you're getting.
Mar 22 '12 #4
Actual Table Name is ExpectedRevenue

I have the following Query and when I run, I get the following error.

Expand|Select|Wrap|Line Numbers
  1. SELECT [Project_ID], 'January' AS [Month], [January] 
  2. FROM   [ExpectedRevenue] 
  3.  
  4. UNION ALL;
Invalid SQL statement, expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'

I am using Access 2007. I am not sure what I am missing.

Thanks,
Mar 22 '12 #5
Luuk
1,047 Expert 1GB
UNION ALL is to 'connect' two SQL statements together, one before the union, and one after the union....

Expand|Select|Wrap|Line Numbers
  1. select id, 'jan', january from ExpectedRevenue
gives something like:
1, 'jan', $1000
2, 'jan', $2000

and:
Expand|Select|Wrap|Line Numbers
  1. select id, 'feb', februari from ExpectedRevenue
gives something like:
5, 'feb', $1500
6, 'feb', $1750

then:
Expand|Select|Wrap|Line Numbers
  1. select id, 'jan', january from ExpectedRevenue
  2. union all
  3. select id, 'feb', februari from ExpectedRevenue
gives:
1, 'jan', $1000
2, 'jan', $2000
5, 'feb', $1500
6, 'feb', $1750
Mar 22 '12 #6
NeoPa
32,556 Expert Mod 16PB
I think Luuk has explained it pretty well, but I was curious how you ended up with what you posted. It was certainly not anything I'd posted as I showed the queries for both Jan and Feb as well as ending with an ellipsis (...) indicating that more of the same was to follow. Perhaps I should have made clearer the point Luuk made, which was that the last UNION ALL must come before the last SELECT query.
Mar 23 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: s_wadhwa | last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber, UCASE(Buildings.BuildingName) AS BuildingName, Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
4
by: ShastriX | last post by:
Getting a weird error while trying out a query from Access 2003 on a SQL Server 2005 table. Want to compute the amount of leave taken by an emp during the year. Since an emp might be off for...
5
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of...
12
by: zwasdl | last post by:
Hi, I'm using MS Access to query against Oracle DB via ODBC. Is it possible to use HINT in Access? Thanks, Wei
3
by: Idlemind23 | last post by:
Ahoy! I'm hoping for some help on what (I believe) should be a simple task. I have a form with a button. I would like that button to have a caption with a date. That date should be pulled out...
5
by: sansann | last post by:
Hello there! i need help on loading data from my Access database query table name combine into my MSFlexgrid select by batchno category. when i runn below coding the error said "Selected collating...
1
by: EwanD | last post by:
I am trying to read through and process an Access Query using VBA. I have used the OpenRecordset method with parameters as below OpenRecordset(sSourceRecordset, dbOpenDynaset) Where...
1
by: cut123 | last post by:
I have a query that I would like to export to different Excel documents based on the product. For each type of product I want all pertinent data to export to a different Excel document. There will...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
2
by: mburns | last post by:
Hello all- I was wondering if it is possible to link an Access query or table to a Word document and pull specific information into Word by manually entering a unique identifier, which would then...
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:
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.