cancel
Showing results for 
Search instead for 
Did you mean: 

COUNT(*) giving wrong results

Former Member
0 Kudos

Hi all,

     I am facing a very peculiar issue here. Will take it up with the help of queries -

SELECT COUNT(*) FROM table_name WHERE "column_name" = 'elec'

-- this is giving me 2 rows.

SELECT COUNT(*)

  FROM (

       SELECT * FROM table_name WHERE "column_name" = 'elec'

  );   

---this gives 731 rows which is the CORRECT value.

SELECT COUNT(*) FROM (

            SELECT * FROM table_name

              )

WHERE "column_name" = 'elec';

---this also gives 731 rows which is the CORRECT value.

I have no clue what is I am doing wrong here. It is happening for queries with a 'WHERE' clause. I looked at the PlanViz fir these two queries, but could not make anything out of it. Any help would be appreciated

Accepted Solutions (0)

Answers (4)

Answers (4)

lbreddemann
Active Contributor
0 Kudos

To understand this a bit better:

Are you actually querying a table or something that looks like a table, e.g. a view, a calc view or a virtual table?

What is the SAP HANA revision you're using?

Can you provide the explain plan for both the correct and the incorrect result examples?

Astrid_Gambill
Contributor
0 Kudos

I've got the same problem.  Studio 2.1.19 against SPS 9 REV 96, against a calculated view

basic count statement returns value of 1, when it should be 2.

selecting all columns from the same table for the same account returns the expected 2 rows.

wrapping a count around the basic select, does bring back the expected count of 2.

It looks like there's an implicit DISTINCT being used when a specific column is being selected.

This statement brings back the expected 10 rows.

select count(*), vkont

from

(select *

from "_SYS_BIC"."WIP.AVAYA_PM/ZQCV_M2C_CONTRACT_ACCOUNT" accts

)

group by vkont

having count(*) > 1

whereas, this brings back nothing

select count(*), vkont

from

(select accts.vkont

from "_SYS_BIC"."WIP.AVAYA_PM/ZQCV_M2C_CONTRACT_ACCOUNT" accts

)

group by vkont

having count(*) > 1

Message was edited by: Astrid Gambill

lbreddemann
Active Contributor
0 Kudos

Ok, first of all: please don't hijack threads.

I get it, you have a problem that looks similar and everything, but 'me too' posts don't really add value and are against

Now to the question itself. I'd say: that's all in the documentation. It has been discussed here on SCN. It's in SAP notes. It's definitively possible to find what's going on there.

Maybe and the list of SAP notes mentioned in this discussion shed some more light onto the topic:


A5: Calc Engine is pretty special and I'd recommend to give the sap notes including their attachments a read:

Former Member
0 Kudos

Just an update -

I got the auto generated query from the log (placed the column in the label as well as the value axis to get the column name + count). The query generated is the same as the second and the third in my question. Any specific reasons for this?

SELECT "column_name", COUNT(*) AS "column_name_COUNT"

FROM (SELECT  * FROM table_name")  --had all the columns, so removed them for readibility

where "column_name" = 'elec' -- --added post the generated query to match the result

GROUP BY "column_name"

ORDER BY "column_name" ASC

-- Result - 731 records

henrique_pinto
Active Contributor
0 Kudos

This is different, because you have a group by column which you didn't have before.

But I'm not sure why it's generating it like this for you, I use select col1, count(col2) from table all the time and it works.

henrique_pinto
Active Contributor
0 Kudos

Do you have a non-null column in this table?
Try SELECT COUNT(<column>) instead of COUNT(*).

Former Member
0 Kudos

hey Henrique, i have Null values in my columns. So COUNT(<column>) will not work in my scenario.

nithinu
Participant
0 Kudos

Hello Sanjog,

Does the following query give you two rows ? Or one row with count as 2 ?

SELECT COUNT(*) FROM table_name WHERE "column_name" = 'elec';

-- this is giving me 2 rows.

Regards,

Nithin

Former Member
0 Kudos

It is giving me also 2 rows.