0

I need the following table to be queried to get the result given below the table.

Table:

----------------------------------
| Name  |  Age   |  slot         |
|-------|--------|---------------|
|A      |20      | 1             |
|B      |30      | 2             |  
|C      |30      | 1             |                
|D      |20      | 1             |                
|E      |40      | 2             |                
|F      |40      | 3             |
|G      |50      | 3             |
----------------------------------

Result:

-------------------------------------------
|Age   |Age_Count     |Slot    |Slot_Count|
-------------------------------------------
|20    | 2            |1       |3         |
-------------------------------------------
|30    | 2            |2       |2         |
-------------------------------------------
|40    | 2            |3       |2         |
-------------------------------------------
|50    | 1            |
-----------------------

While searching stackoverflow i found this question for single column question and there is [this link for multiple columns] (get the count of each distinct value in "Multiple" columns) question. The answers from the second link (for the multiple coulmn's distinct count) is displayed under a single column and my requirement is i guess quite different from the answers posted there.

Thanks in advance

Community
  • 1
  • 1
Abhishek D
  • 33
  • 1
  • 9
  • What exactly is the `slot` column in your result table? How should that be calculated? – Alex Tartan May 24 '15 at 12:27
  • It is any slot alloted to the ids. The calculation for it is same as for the age. It will calculate distinct slots and give me the number of it. e.g. i have 3 slots named slot 1, 2 slots named slot 2. so the result table is showing distinct slots under slot field and their count under slot_count. – Abhishek D May 25 '15 at 06:24

1 Answers1

0

Your request is kind of odd. Are you sure you want that?

If so, this may help:

SET @x:=0,@y:=0,@m:=0,@n:=0;
SELECT 
    DISTINCT age,age_count, slot,slot_count 
FROM (
    SELECT 
        age, age_count, slot, slot_count
    FROM (
        SELECT 
            @x:=@x + 1 AS aid, age, COUNT(*) age_count
        FROM
            slots
        GROUP BY age
    ) a
    LEFT JOIN (
        SELECT 
            @y:=@y + 1 AS sid, slot, COUNT(*) slot_count
        FROM
           slots
        GROUP BY slot
    ) s ON a.aid = s.sid

    UNION

    SELECT 
        age, age_count, slot, slot_count
    FROM (
        SELECT 
            @m:=@m + 1 AS aid, slot, COUNT(*) slot_count
        FROM
           slots
        GROUP BY slot
    ) a 
    LEFT JOIN (
        SELECT 
            @n:=@n + 1 AS sid, age, COUNT(*) age_count
        FROM
            slots
        GROUP BY age
    ) s ON a.aid = s.sid
) a

If you know for sure that you have more unique ages than unique slots , or opposite, you can get ride of messy union.

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
Tim3880
  • 2,563
  • 1
  • 11
  • 14