473,386 Members | 1,668 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.

Wrong results returned by a table pivot

I'm working with MySQL 5.0.25 form phpMyAdmin 2.6.3 and I get strange results when trying to pivot a table...
My table "Test" is very simple and looks like :
Expand|Select|Wrap|Line Numbers
  1. MatchID    UserID    Presence
  2. 1    1    1
  3. 1    2    0
  4. 1    3    1
  5. 1    4    1
  6. 2    1    1
  7. 2    2    0
  8. 2    3    1
  9. 2    4    1
  10.  
I want to display it like:
MatchID User1 User2 User3 User4
1
2

My SQL query is very simple:
Expand|Select|Wrap|Line Numbers
  1. SELECT MatchID, 
  2.     CASE UserID WHEN 1 THEN Presence ELSE 6 END AS User1,
  3.     CASE UserID WHEN 2  THEN Presence  ELSE 6 END AS User2,
  4.     CASE UserID WHEN 3 THEN Presence ELSE 6 END AS User3,
  5.     CASE UserID WHEN 4 THEN Presence ELSE 6 END AS User4
  6. FROM Test2
  7. GROUP BY MatchID
  8.  
Very simple...
but I get a very strange result...
Expand|Select|Wrap|Line Numbers
  1. MatchID    User1    User2    User3    User4
  2. 1    6    6    1    6
  3. 2    6    6    1    6
  4.  
I'm lost... Any help would be appreciated...
Nov 24 '06 #1
4 7193
ronverdonk
4,258 Expert 4TB
It is not clear to me what you want to accomplish here. I tested your select, but now showed the UserID, replace the 6 by a blank for readability and removed the GROUP BY. The result is as follows, just what you would expect:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT MatchID, UserID,
  2.     ->     CASE UserID WHEN 1 THEN Presence ELSE '' END AS User1,
  3.     ->     CASE UserID WHEN 2 THEN Presence ELSE '' END AS User2,
  4.     ->     CASE UserID WHEN 3 THEN Presence ELSE '' END AS User3,
  5.     ->     CASE UserID WHEN 4 THEN Presence ELSE '' END AS User4
  6.     -> FROM Test;
  7. +---------+--------+-------+-------+-------+-------+
  8. | MatchID | UserID | User1 | User2 | User3 | User4 |
  9. +---------+--------+-------+-------+-------+-------+
  10. |       1 |      1 | 1     |       |       |       |
  11. |       1 |      2 |       | 0     |       |       |
  12. |       1 |      3 |       |       | 1     |       |
  13. |       1 |      4 |       |       |       | 1     |
  14. |       2 |      1 | 1     |       |       |       |
  15. |       2 |      2 |       | 0     |       |       |
  16. |       2 |      3 |       |       | 1     |       |
  17. |       2 |      4 |       |       |       | 1     |
  18. +---------+--------+-------+-------+-------+-------+
  19. 8 rows in set (0.00 sec)
  20.  
Ronald :cool:
Nov 26 '06 #2
Thanks for the reply Ronald,
I may have not been very clear on what I want to achieve:
Here is how I would like my table to be displayed:
Expand|Select|Wrap|Line Numbers
  1. +---------+--------+-------+-------+-------+-------+
  2. | MatchID | User1  | User2 | User3 | User4 |
  3. +---------+--------+-------+-------+-------+-------+
  4. |      1  | 1      |   0   | 1     | 1     |
  5. |      2  | 1      |   0   | 1     | 1     |
  6. +---------+--------+-------+-------+-------+-------+
  7.  
where UserI corresponds to the UserID I.
I read everywhere about cross tabs and pivoting table and this is exactly what I want to achieve, but it seems that my code is not working properly.
Any hints?
Nov 27 '06 #3
ronverdonk
4,258 Expert 4TB
All I could think of in this particular case, where you have only 1 and 0 values, is to SUM them. So the query would be like:
Expand|Select|Wrap|Line Numbers
  1. SELECT MatchID, SUM(user1) as user1, SUM(user2) as user2, 
  2.        SUM(user3) as user3, SUM(user4) as user4 
  3.        FROM (SELECT MatchID, 
  4. CASE UserID WHEN 1 THEN Presence ELSE '0' END AS User1,
  5. CASE UserID WHEN 2 THEN Presence ELSE '0' END AS User2,
  6. CASE UserID WHEN 3 THEN Presence ELSE '0' END AS User3,
  7. CASE UserID WHEN 4 THEN Presence ELSE '0' END AS User4
  8. FROM Test) as result GROUP BY MatchID;
That will, for this case, result in:
Expand|Select|Wrap|Line Numbers
  1. +---------+-------+-------+-------+-------+
  2. | MatchID | user1 | user2 | user3 | user4 |
  3. +---------+-------+-------+-------+-------+
  4. |       1 |     1 |     0 |     1 |     1 |
  5. |       2 |     1 |     0 |     1 |     1 |
  6. +---------+-------+-------+-------+-------+
Sorry I cannot help you any further. Maybe there are members who can.
ANYONE???

Ronald :cool:
Nov 27 '06 #4
Thanks a lot Ronald!!! It's working now!
Still, I can't figure out why a sum over the field is working while a simple call to the value of the field isn't, but anyway... It works now!

Cheers,
Maïté
Nov 30 '06 #5

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

Similar topics

6
by: pb648174 | last post by:
I have a pivot table implementation, part of which is posted below. It returns no errors in query analyzer, but when profiler is run, it shows that "Error 208" is happening. I looked that up in BOL...
2
by: Rob | last post by:
I'm just getting around to using pivot tables and charts. I find the Pivot table interface to be INCREDIBLY frustrating. When I view a table in Design view, then choose Pivot table view, I get...
1
by: Grey | last post by:
I have created a asp.net form for user to input data. After input the data, user need to click a button to export the input data to excel for data analysis with excel pivot table function. is it...
3
by: Peter | last post by:
Hello all, I have the following t-sql batch: create procedure stp_test ( @p_date1 as datetime = null, @p_date2 as datetime = null )
4
by: Spartaco | last post by:
This is a test I made that I believe has a wrong solution, they say the correct answer is C ?!? ---- You are an asp.net developer and currently you are working on te sql statement you will use...
9
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
5
by: Pourya99 | last post by:
Hello, I have an Access Data Access Page which has a pivot table. The data source of the pivot table is a SQL database table. The data in the pivot table itself is not a problem. I have a text...
0
by: ravindarjobs | last post by:
Hello friends, I am using ms access 2003, and Vb6. i am displaying a table in a form as pivot table (following "Auto Form: Pivot Table" Wizard) Usually i select the items i want to be...
1
by: is49460 | last post by:
Good afternoon! I use transfer spreadsheet function the export data from one of the table into the excel spreat sheet. I use the following code: DoCmd.TransferSpreadsheet acExport, 8, "qry...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.