473,467 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrong results returned by a table pivot

3 New Member
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 7198
ronverdonk
4,258 Recognized Expert Specialist
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
Biggoby
3 New Member
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 Recognized Expert Specialist
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
Biggoby
3 New Member
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...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.