ok, this is kind of complicated to explain, i will try my best, you are welcome to ask questions if you dont understand me.
i have a table desiged like this:
TITLE | BRANCH | BRANCH1 | BRANCH2 | BRANCH3
a BRANCH
(any of them) might contain "bank","credit" and so on
what i want to do, is return rows with distinct branches and count, where the title equals "xxx"
something like:
BANK | xxx Results
CREDIT | xxx Results
CREDIT | xxx Results
and so on.
If you can't alter your tables to conform to 1FN , following should get you started
UNPIVOT
the columns using a UNION ALL
giving you a resultset that conforms to 1FN . TITLE
from this subselect GROUP BY
and COUNT
the results SELECT BRANCH, COUNT(*)
FROM (
SELECT TITLE, BRANCH FROM MyTable
UNION ALL SELECT TITLE, BRANCH1 FROM MyTable
UNION ALL SELECT TITLE, BRANCH2 FROM MyTable
UNION ALL SELECT TITLE, BRANCH3 FROM MyTable
) q
WHERE TITLE = 'ATitle'
GROUP BY
BRANCH
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.