简体   繁体   中英

Columns from multiple table with same value returning wrong count of data when selecting multiple values

SELECT COUNT(*) FROM (
SELECT
    columnA,
    columnB
FROM
    "table" A JOIN "table" B ON a.ID = b.ID
    WHERE a.column in ('value1','value2')
       AND SUBSTR(b.column,7,6) in ('value1','value2')
)

Here, when I enter a single value then I get the correct count but when I enter multiple values, I am getting a wrong count. For example, entering only value 1 gives a count of 1241 rows and entering only value 2 gives me a count of 0. I am expecting to enter both the values and get the sum of results from both values. But somehow due to a weird combination of values, I am getting more results than required instead of just 1241 rows.

I tried something like where (a.column,SUBSTR(b.column,7,9)) in ('value1','value2') but it did not work!

Did yo try?

SELECT Sum(COUNT(x.columnA) + Count(x.columnb)) FROM (
SELECT
    columnA,
    columnB
FROM
    "table" A JOIN "table" B ON a.ID = b.ID
    WHERE a.column in ('value1','value2')
       AND SUBSTR(b.column,7,6) in ('value1','value2')
)x

It's difficult to understand what you want from your sample, but from what I gather you might have something like column being 'value1' in table A and 'value2' in table B. I think you need to change your code like this:

SELECT COUNT(*) FROM (
SELECT
    columnA,
    columnB
FROM
    "table" A JOIN "table" B 
    ON a.ID = b.ID
      AND SUBSTR(b.column,7,6)=a.column
    WHERE a.column in ('value1','value2')

)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM