Please help me to understand, how to convert columns to rows in MS Access. I have the below table, I would like to convert as the below result table.
NoOf_A NoOf_B NoOf_C NoOf_D
200 300 400 500
Result Table:
Type_OfCValue Total
NoOf_A 200
NoOf_B 300
NoOf_C 400
NoOf_D 500
you don't really convert columns to rows in the tables as they represent different concepts (column=structure, row=data).
What you need is a crosstab query. Check the link from excellent Allen Browne's site:
Crosstab queries
You can use a SELECT query first. Use that as source to your Crosstab query.
Related
I am trying to execute a calculated field in MS Access.
Background
I have a single table including fields such as:
Name Week Hours_Charged
X 21-06 10
Y 21-06 20
X 21-06 30
Z 21-06 40
I am trying to create a new field in the query wherein it says Gap and contains the substraction from 40.
Additionally, it will should group according to the Same names. So the expected output would be:
Name Week Hours_Charged Gap
X 21-06 40 0
Y 21-06 20 -20
Z 21-06 40 0
Conditions: With the same name, there can be various dates and hours. However, the idea remains same.
Any leads on this would be appreciated.
Thanks in advance.
I have tried implementing through the design mode but was not successful.
I replied your table like this:
And got this query:
SQL code of this query (please note I've used different field names):
SELECT Table1.MyName, Table1.MyWeek, Sum(Table1.Hours_Charged) AS SumaDeHours_Charged, 40-[SumaDeHours_Charged] AS GAP
FROM Table1
GROUP BY Table1.MyName, Table1.MyWeek;
And design view of this query in MS Access (Please, note my Access is in spanish, so name of rows in design view are in spanish, not in english. If you want to know the meaning of something, you can ask me or use the SQL code:
Transport table
id name
1 T1
2 T2
Pallets table
id name
1 P1
2 P2
Transport Pallet Capacity table
id transport_id pallet_id capacity
1 1 1 10
2 1 2 null
3 2 1 20
4 2 2 24
How to generate table like this:
id transport_id pallet_id_1_capacity pallet_id_2_capacity
1 1 10 null
2 2 20 24
Problem: pallets and transports can be added, so, neither quantity is known in advance.
For example, manager adds another pallet type and 'pallet_id_3_capacity' column should be generated (and can show null if no capacity data is yet available).
Another manager can fill 'transport pallet capacity' table later when notified.
Is there a way to build sql in mysql that will care about the above: specifically - dynamic number of pallets?
The SQL select-list must be fixed at the time you write the query. You can't make SQL that auto-expands its columns based on the data it finds.
But your request is common, it's called a pivot-table or a crosstab table.
The only solution is to do this in multiple steps:
Query to discover the distinct pallet ids.
Use application code to build a dynamic SQL query with as many columns as distinct pallet id values found in the first query.
Run the resulting dynamic SQL query.
This is true for all SQL databases, not just MySQL.
See MySQL pivot row into dynamic number of columns for a highly-voted solution for producing a pivot-table query in MySQL.
I am not voting your question as a duplicate of that question, because your query also involves transport_id, which will make the query solution a bit different. But reading about other pivot-table solutions should get you started.
Here is a table with barcodes which belongs to different warehouses.
Barcode | Warehouse
_____________________________
1111111 | A
2222222 | B
1111111 | C
3333333 | A
And here is a table with boxes containing barcodes.
Barcode | Box
_____________________________
1111111 | 0001
2222222 | 0002
Each warehouse's available stock is its amount in the first table, plus all the amount in boxes.
Example for warehouse A:
Barcode
_________
1111111 (from its warehouse)
3333333 (from its warehouse)
1111111 (from a box)
2222222 (from a box)
This is a simplified example. After retrieving the total amount of barcodes, I cross it with a lot of other queries and tables to transform it into a human-readable report.
Ok,
The idea would be a server-side query.
Every client (VBA msaccess) would retrieve the query and filter it using its warehouse code.
Warehouse A would call it like this:
select * from finalQuery where warehouse like 'A' <--- BUT it won't work, because boxes' barcodes haven't the warehouse field, thus, they would be excluded.
The "where" clause should be performed before the UNION ALL.
Would it be possible to use parameters in order to exclusively retrieve a warehouse's barcodes + all boxes' barcodes in a server-side query? Even though the user calls the last query with its code, it should push the parameter down to the first nested query.
Or any other trick? Maybe my scheme is wrong?
The problem manipulating queries in the client side, is that it becomes painstakingly SLOW, because as I said, after joining barcodes, I use the resulting query for building other queries.
Hope I explaied it clearly. It is somewhat complex to explain. I would appreciate any suggestion, trick, idea, etc
Thank you.
I think what you're looking for is a JOIN statement. You can join the Barcode-Warehouse table with the Barcode-Box table using the common Barcode column. This article is a great explanation: http://www.tutorialspoint.com/mysql/mysql-using-joins.htm.
Your server side query will end up being something like this:
SELECT Barcode, Box, Warehouse FROM Barcode-Warehouse LEFT JOIN Barcode-Box USING (Barcode);
This should result in a result set that has Barcode, Box, and Warehouse on each line. Your users would then be able to filter that result by Warehouse and retrieve only the records that they are interested in.
Found a way to solve it:
Can I create view with parameter in MySQL?
I filter the first query by adding:
"where warehouse=function()"
When I call the final query, I add the parameter for the function as explained in the post. Easy, simple.
thank you
I'm looking for a query that will return me several rows into columns but without knowing the number of rows beforehand. I have searched and the only solutions I found involve knowing how many rows there are.
Here's an example table:
parentID colA colB
2 aaaaaa 1000.00
2 bbbbbb 1500.00
3 cccccc 500.00
3 dddddd 700.00
3 eeeeee 2000.00
and i need it to look like:
parentID colA(n) colB(n) colA(n+1) colB(n+1) colA(n+2) colB(n+2)
2 aaaaaaa 1000.00 bbbbbb 1500.00 NULL NULL
3 cccccc 500.00 dddddd 700.00 eeeeee 2000.00
I realize this should be done in PHP but I need it to be in mysql for a third party excel exporter plugin I'm using.
Edit: Is there a way to do this if I know the maximum number of columns I'll need?
You cannot do a query in SQL without knowing the number of columns.
The columns of a SELECT-list must be fixed at the time of parsing the query. What you're asking for is that the state of data, which is not known until the query executes, determines the number of columns. That is not the way SQL works.
To accomplish a pivot-type operation, or any query where the data determines the columns, you have two choices:
Do a preparatory query to discover how many distinct groups you want to fetch, and use this to build the query with a matching number of columns.
Query all the data in rows, fetch it back into your application, and then transform the result set wholly within data structures (i.e. arrays) within your code.
Either way, you need to write application code, either before or after fetching the data.
Re your comment: You're right, this isn't a traditional pivot, but it's similar in that data is driving the number of columns. That is, you need as many columns as 2x the number of rows in the largest group. But they won't all be filled, because the number of rows per group varies. You don't have a fixed number of row per group, therefore you can't fill a fixed number of columns per group.
I'd really recommend you use the latter strategy: fetch the data as rows, as they are stored in the database. Then post-process it in your application code. Loop over the result set and build your data structure incrementally as you fetch rows from the database.
For example, in PHP:
while ($row = $stmt->fetch()) {
$data[$row['parentID']][] = $row['colA'];
$data[$row['parentID']][] = $row['colB'];
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Combine rows in Access 2007
If I have two tables: let's say OrderStatus and Comments, how can I sum up all comments which are made related to one order, so that I can put them into one field?
Tabel OrderStatus:
OrderID OrderStatus OrderDate
64 OK 13.08.2011
77 Deleted 21.06.2011
99 OK 18.04.2011
Table Comments:
CommID CommOrder CommText
1 64 "EAN 304"
2 64 "resent"
3 64 "no. 499"
4 99 "in stock"
5 99 "EAN 111"
What I want:
OrderID OrderStatus Comments
64 OK "EAN 304, resent, no. 499"
99 OK "in stock, EAN 111"
The total number of comments related to an order status is unknown.
I am trying to achieve this with Access SQL-subqueries. I already managed with own VBA routines called from the query builder but the OrderStatus table has more than 30,000 records and VBA is too slow (takes more than 10 minutes to generate a report). In Microsoft Access, SQL is much faster than VBA but can be complicated.
From Access 2007 on, Microsoft has included multivalued fields, however I'm using Access 2003 which isn't capable of automatically listing multiple values comma-separated.
Thanks for your help!
VBA is the way to go. But, I wouldn't call it from the query builder. I would add a "Comments" field to your original table, and make a stand-alone VBA function using the Recordset object to loop through the records in the second table, and paste the data in the Comments field. A function like this for just 30,000 records should run very quickly.