I have data in an MYSQL database that looks like this:
Project Date Time
A 2009-01-01 15
A 2009-01-02 10
B 2009-01-02 30
A 2009-01-09 15
C 2009-01-07 5
I would like to produce output from this data like this:
Date Project A Time Project B Time Project C Time
2009-01-01 15 0 0
2009-01-02 10 30 0
2009-01-07 15 0 5
Can this be done with an SQL query, or do I need to write an external script to itterate through the DB and organize the output?
(Also, if someone has a better suggestion for a subject line let me know and I'll edit the question; I'm not sure of the proper terms to describe the current and desired formats, which makes searching for this information difficult)
You're looking for pivot / crosstab support. Here is a good link.
http://en.wikibooks.org/wiki/MySQL/Pivot_table
I believe this is called Pivot table. Just google for it.
I've been looking for something like this and found a MySQL stored procedure that works perfectly:
http://forums.mysql.com/read.php?98,7000,250306#msg-250306
The result you're looking for could be obtained from the following simple call:
call pivotwizard('date','project','time','from_table','where_clause')
Related
sorry I know you expect code examples but I have absolutly no idear how to start with that issue.
I have a database with about 100000 entries of that structure:
ID | LONGARG
0 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
1 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
2 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS3|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
3 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
Longarg is of type CLOB.
My question is, is there a possibility to select all the text that is between EC and the first | to get a result like that without using a StoredProcedure and for all datarows?
Result:
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
LONG_TEXT_INSIDE_THIS3
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
Thanks in advance for your help
Stefan
I'm using an MRP system for stocking inventory where I work. The interface it self isn't the best, so I have decided to open up the database file and do everything manually. I'm having some issues though. I'm trying to sort my database by using ORDER BY. I'm not getting the results I thought I would. It is showing them in this format:
1
10
100
101
101
11
110
111
etc
Instead of
1
2
3
4
5
etc
This is my query
SELECT *
FROM tblStockItems
Order By (`MasterPNo`)
I'm currently working in access, and then database is in the JET format. If you're wondering why I am using access instead of the MRP Interface, it is because later down the line I will be needing to re-organise the whole stock system, so a lot of fields will have their product numbers changed.
Thanks for reading
if possible, change the column type to number
if not, a cast should do it:
ORDER BY Val(MasterPNo)
I am using SQL Server 2005.
I have a site that people can vote on awesome motorcycles. Each time a user votes, there is one for the first bike and one vote against the second bike. Two votes are stored in the database. The vote table looks like this:
VoteID VoteDate BikeID Vote
1 2012-01-12 123 1
2 2012-01-12 125 0
3 2012-01-12 126 0
4 2012-01-12 129 1
I want to tally the votes for each bike quite frequently, say each hour. My idea is to store the tally as a percentage of contest won versus lost on the bike table as an attribute of the bike. So, if a bike won 10 contests and lost 20 contest, they would have a score (tally) of 33. I would tally up daily, weekly, and monthly scores.
BikeID BikeName DailyTally WeeklyTally MonthlyTally
1 Big Dog 5 10 50
2 Big Cat 3 15 40
3 Small Dog 9 8 0
4 Fish Face 19 21 0
Right now, there are about 500 votes per day being cast. We anticipate 2500 - 5000 per day in the next month or so.
What is the best way to tally the data and what is the best way to store it? Should the tallies be on their own table? Should a trigger be used to run a new tally each time a bike is voted on? Should a stored procedure be run hourly to get all tallies?
Any ideas would be very helpful!
Store your VoteDate as a datetime value instead of just date.
For your tallies, you can just make that a view and calculate it on the fly. This should be very simple to do using GROUP BY and DATEPART functions. If you need exact code for how to do this, please open a new question.
For that low volume of rows it doesn't make any sense to store aggregations in a table when you can just calculate them whenever you want to see them and get accurate and immediate results that are up-to-date.
I agree with #JNK try a view or just a normal stored proc to calculate the outputs on the fly. If you find it becomes too slow as your data grows I would investigate other routes then (like caching the data in another table etc). Probably worth keeping it simple to start with; you can always resuse the logic from the SP/VIEW later if you do want to setup a scheduled task.
Edit :
Removed the index view as per #Damien_The_Unbeliever comments its not deterministic and i'm stupid :)
I have a Jasper report with the following output format:
Item | Price | Quantity
----------------------------
1 100 5
2 150 8
3 200 11
How do I make that table to this format:
Item 1 2 3
Price 100 150 200
Quantity 5 8 11
The column headers have now become row headers.
I'm actually using DynamicJasper, but of course, it's still relies on Jasper.
What special setting or property should I set to achieve the format I'm looking for.
Also, what do you call this format? Inverted Headers? Inverted Columns? It's hard to Google this issue since the keywords I'm using doesn't seem to be correct. Google always gives me a different answer.
Please check if Crosstabs serve your purpose
As it was suggested before, either check out crosstab, or you can check their CrosstabBuilder/LayoutManager classes and probably override/extend some to adopt to your needs
I'm working now for a while on a reporting applications where I use hibernate to define my queries. However, more and more I get the feeling that for reporting use cases this is not the best approach.
The queries only result partial columns, and thus not typed objects
(unless you cast all fields in java).
It is hard to express queries without going straight into sql or
hql.
My current problem is that I want to get the top N per group, for example the last 5 days per element in a group, where on each day I display the amount of visitors.
The result should look like:
| RowName | 1-1-2009 | 2-1-2009 | 3-1-2009 | 4-1-2009 | 5-1-2009
| SomeName| 1 | 42 | 34 | 32 | 35
What is the best approach to transform the data which is stored per day per row to an output like this? Is it time to fall back on regular sql and work with untyped data?
I really want to use typed objects for my results but java makes my life pretty hard for that. Any suggestions are welcome!
Using the Criteria API, you can do this:
Session session = ...;
Criteria criteria = session.createCriteria(MyClass.class);
criteria.setFirstResult(1);
criteria.setMaxResults(5);
... any other criteria ...
List topFive = criteria.list();
To do this in vanilla SQL (and to confirm that Hibernate is doing what you expect) check out this SO post: