473,386 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,386 software developers and data experts.

How to create table using result of previous query as column name ?

I have table as Table1
p_cd dt qty price amt
001 12/13/2009 10 20 200
002 12/14/2009 5 10 50
001 12/15/2009 10 20 200
001 12/19/2009 8 20 160
i want result as
p_cd 12/13/2009 12/14/2009 12/15/2009 12/19/2009 totqty price amt
001 10 - 10 8 28 20 560
002 - 5 - - 5 10 50
Dec 30 '09 #1

✓ answered by ADezii

I do believe that a Crosstab Query will produce the results you seek:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(Table1.qty) AS SumOfqty
  2. SELECT Table1.p_cd, Sum(Table1.qty) AS [Total Quantity],
  3. Sum(Table1.price) AS [Total Price], 
  4. Sum(Table1.amt) AS [Total Amount]
  5. FROM Table1
  6. GROUP BY Table1.p_cd
  7. PIVOT Format([dt],"Short Date");
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. p_cd    Total Quantity    Total Price    Total Amount    12/13/2009    12/14/2009    12/15/2009    12/19/2009
  2. 001            28          $60.00           560              10                           10          8
  3. 002             5          $10.00            50                            5    

4 1827
How to create table using result of previous query as column name ?
Dec 30 '09 #2
ChipR
1,287 Expert 1GB
Have you created tables or stepped through recordsets with VBA before? Also, why would you want to do this? You seem to be taking correctly normalized data and storing it in a way that is not dynamic.
Dec 30 '09 #3
ADezii
8,834 Expert 8TB
I do believe that a Crosstab Query will produce the results you seek:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(Table1.qty) AS SumOfqty
  2. SELECT Table1.p_cd, Sum(Table1.qty) AS [Total Quantity],
  3. Sum(Table1.price) AS [Total Price], 
  4. Sum(Table1.amt) AS [Total Amount]
  5. FROM Table1
  6. GROUP BY Table1.p_cd
  7. PIVOT Format([dt],"Short Date");
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. p_cd    Total Quantity    Total Price    Total Amount    12/13/2009    12/14/2009    12/15/2009    12/19/2009
  2. 001            28          $60.00           560              10                           10          8
  3. 002             5          $10.00            50                            5    
Dec 31 '09 #4
NeoPa
32,556 Expert Mod 16PB
ADezii has provided code to show how the data can be formatted as requested. Chip makes a good point though. De-normalising your data is not generally recommended. If you need to though, it is a simple step to convert SQL similar to what ADezii's posted into a Make Table query. Remember we warned against such a step though.
Jan 6 '10 #5

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

Similar topics

4
by: Thomas Jerkins | last post by:
When I write a create table SQL statement I want to add some information about the column heading of each column. How does the exact syntax for the create table look like (which includes this column...
6
by: Bruce | last post by:
I want to create a new table based on an existing table, but I don't want the tables to have any enforced relationship. Is this possible without having to do a CREATE TABLE and an INSERT? ...
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
3
by: jaYPee | last post by:
I'm so sorry for posting this message again 'cause there's a lot of error in my previous posting... I'm having problem w/ my query on how to filter a record based on the criteria i give. ...
10
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung...
18
by: PC Datasheet | last post by:
An Access user saw my name in a newsgroup and sent me a request for help on a project. As part of the project, a list of the dates in a month was needed. For anyone needing a list of dates in a...
1
by: JackM | last post by:
I'm not sure if this qualifies as a mysql or a php question so I'm asking in both groups. I am pulling the results of a mysql query from my database and want to print the results into a two...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
7
by: Lucas_london via AccessMonster.com | last post by:
Hi I have set up a database/tables in Access based on daily timeseries data. However I would like to create two additional columns in the table as a basis to pull the data on a weekly and...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.