0

I know this is simple but I can't figure it out. Can you show me how to merge these two, so Total is to right of Count.


MariaDB [meta]> select `Sector`, count(*) as `Count` from meta where `Close` > `VWAP` and `Sector` != '' group by `Sector`;                                                                  +-----------------------+-------+
| Sector                | Count |
+-----------------------+-------+
| Basic Industries      |    85 |
| Capital Goods         |    90 |
| Consumer Durables     |    42 |
| Consumer Non-Durables |    63 |
| Consumer Services     |   237 |
| Energy                |    80 |
| Finance               |   233 |
| Health Care           |   165 |
| Miscellaneous         |    31 |
| Public Utilities      |   100 |
| Technology            |   162 |
| Transportation        |    35 |
+-----------------------+-------+
12 rows in set (0.00 sec)

MariaDB [meta]> select `Sector`,count(`Sector`) as `Total` from `meta` where `Sector` != '' group by `Sector`;                                                                               +-----------------------+-------+
| Sector                | Total |
+-----------------------+-------+
| Basic Industries      |   368 |
| Capital Goods         |   403 |
| Consumer Durables     |   157 |
| Consumer Non-Durables |   237 |
| Consumer Services     |   832 |
| Energy                |   382 |
| Finance               |   880 |
| Health Care           |   681 |
| Miscellaneous         |   155 |
| Public Utilities      |   264 |
| Technology            |   709 |
| Transportation        |   114 |
+-----------------------+-------+
12 rows in set (0.00 sec)

I also need another column for Percentage where count is a percentage of total.

2
  • Have you tried researching how to do that?
    – Kermit
    Sep 4, 2014 at 23:59
  • Yes, I have tried subqueries and joins but I cannot figure out the proper context. My issue is because of the GROUP BY, I think.
    – ctrlbrk
    Sep 4, 2014 at 23:59

1 Answer 1

0

Add the following SELECT clause to the second query:

SUM(`Close` > `VWAP`) AS `Count`

What it does is summarizes number of rows that have the Close > VWAP condition true.

The ANSII SQL-friendly (apart of using backticks) one would be

SUM(CASE WHEN `Close` > `VWAP` THEN 1 ELSE 0 END) AS `Count`
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.