Results 1 to 2 of 2

Thread: Table with count of records multifield

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Table with count of records multifield

    Dear all,
    building my application, I'm in trouble with a particular selection in one of MyExpress SQL DB.
    In particular I have a table made like below:

    id | Code | Description | .... | T1 | T2 | .... |
    1 AO234 XYZ a b
    2 AB567 ABC b c
    3 AO234 GFH a
    4 FR456 JKL b b

    Basically I need to perform a selection-count on the basis of the Code column to retrieve the following data table:

    (example assuming to made the selection of Code AO234)

    T | #
    a 2
    b 1

    So far I have been able to make the selection on column T1 or T2, but I cannot make the join of the two.

    Do you have any suggestion I could follow?

    Thanks,
    A.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Table with count of records multifield

    One way would be to use a Union:
    Code:
    SELECT T, Sum(Num) as N
    FROM (
       SELECT T1 as T, Count(T1) as Num
       FROM tableName
       WHERE Code = 'AO234'
       GROUP BY T1
    
       UNION ALL
    
       SELECT T2, Count(T2)
       FROM tableName
       WHERE Code = 'AO234'
       GROUP BY T2
       )
    GROUP BY T
    Note however that the need to do anything like this should make you question the table design, as it might be better to have a related table to contain the T1 and T2 values (with a link back to the id field of this table).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width