Forum Stats

  • 3,826,571 Users
  • 2,260,666 Discussions
  • 7,897,004 Comments

Discussions

How to add static data in count() without union

Jian-cdo
Jian-cdo Member Posts: 485 Blue Ribbon

Hello,

I have a query like:

select case when gender is null then 'Unknown' else Gender end gender, count(1) genderCount

from table_1

group by gender

=============

table_1 has dynamic data. Then I have another table_2, which has static data. Say, the gender count is always like these after running the query:

Gender GenderCount

male 2700

female 2100

unknown 300

Now I wonder how I can directly add three numbers into the count without using union (run the query on table2 again). My current query is this way, but I don't like the second part will be run again every time user refreshes the page. I'm not going to provide the information to create a table since I just want an idea on how to tune the query. Thanks.

select gender, count(1) genderCount

from (

select case when gender is null then 'Unknown' else Gender end gender

from table_1

union all

select gender from table_2 )

group by gender

Tagged:

Answers

  • fac586
    fac586 Senior Technical Architect Member Posts: 20,956 Red Diamond

    I'm not going to provide the information to create a table

    You are going to have to since I have no idea what this means:

    table_1 has dynamic data. Then I have another table_2, which has static data.


    Now I wonder how I can directly add three numbers into the count without using union (run the query on table2 again). My current query is this way, but I don't like the second part will be run again every time user refreshes the page.

    I haven't a clue what problem you are trying to solve.