2

I have a table, with columns like this:

name1,name2,name_thesame,adress1,adress2,adress_thesame,city1,city2,city_thesame

In all columns ending with _thesame, there is a true or false depending if name1 and name2 are the same, same with adress etc etc.

I now need a query that returns a count how many true and false i got for each of the _thesame columns.

Cant wrap my head around how to do this query - any body got some ideas or pointers? Thanks

5
  • 3
    which dbms are you using? Commented Oct 6, 2015 at 11:30
  • 2
    Edit your question and provide sample data and desired results. The question is not entirely clear. Commented Oct 6, 2015 at 11:31
  • stackoverflow.com/questions/17194145/… is practically the same question Commented Oct 6, 2015 at 11:32
  • @Terminus - right, practically same. Commented Oct 6, 2015 at 11:33
  • @user5051310 The linked question counts a single column, while this counts multiple columns. Commented Apr 20, 2022 at 18:07

2 Answers 2

4

This is another option:

SELECT SUM(CASE WHEN name_thesame = true THEN 1 ELSE 0 END) as nametrue,
       SUM(CASE WHEN name_thesame = false THEN 1 ELSE 0 END) as namefalse,
       SUM(CASE WHEN adress_thesame = true THEN 1 ELSE 0 END) as adresstrue,
       SUM(CASE WHEN adress_thesame = false THEN 1 ELSE 0 END) as adressfalse,
       SUM(CASE WHEN city_thesame = true THEN 1 ELSE 0 END) as citytrue,
       SUM(CASE WHEN city_thesame = false THEN 1 ELSE 0 END) as cityfalse
FROM yourTable

You can tweak it to deal with NULLs as well if relevant:

...
CASE WHEN name_thesame = false OR name_thesame IS NULL THEN 1 ELSE 0 END
...

or either NVL(), ISNULL(), IFNULL() or COALESCE(), depending on the DBMS you're using (syntax is always the same):

...
CASE WHEN COALESCE(name_thesame, false) = false THEN 1 ELSE 0 END
...

Result:

nametrue | namefalse | adresstrue | adressfalse | citytrue | cityfalse
       5 |         7 |          2 |           1 |       10 |         8
Sign up to request clarification or add additional context in comments.

3 Comments

If he doesn't have NULL can he just subtract # of true from the total # of rows to get # of false?
Of course it can be done, but it wouldn't make the query shorter or more performant (something like COUNT(*) - SUM(CASE WHEN name_thesame = true THEN 1 ELSE 0 END)). Unless I miss something obvious ?
Could put it in the application but I thinking about it, no idea if that's in any way better.
3

For a single property you can do:

select name_thesame, count(*) 
from table
group by name_thesame

This will give you results like:

 true     10
 false    15

If you want to have it as a list for multiple columns, you can just union the queries:

select 'Name', name_thesame, count(*) 
  from table
  group by name_thesame
union 
select 'Address', adress_thesame, count(*) 
  from table
  group by adress_thesame

getting:

 Name    true   10
 Name    false  15
 Address true   20
 Address false  5

3 Comments

Thanks, yeah that would give me the correct results. However I was hoping for a solution to do it programmaticly, so I dont have to hard code every column name, as there are 100+ where I want the counts to be done. But I will accept the answer and do the hard coding, thanks
To be honest, 100+ (meaning 300+ field in the table) is a bit of a red flag, design wise :)
@user2199192, specify a database platform, I'll try to draft a script that will write the query for you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.