简体   繁体   中英

SQL Copied query from MS Access

I am trying to recreate a MS Access query in SQL, the following is the Access query converted to run in SQL

    SELECT Column1
          ,Column2
          ,Column3
          ,Count('x') AS CountX
    FROM [SchmaName].[SQLView]
    WHERE ("SomeWhereStatement")
    GROUP BY Column1, Column2, Column3
    HAVING Count('x')>1;

What I am confused about is the

 ,Count('x') AS CountX

and

 HAVING Count('x')>1;

X does not come from the SQLView that has been created and when I run the above it will return a single record with Columns 1 - 3 being null and column CountX being 12896. I am expecting this return about 4285 records in total and I suspect the reason that I am not is something to do with this Count. Any suggestions as to what this count is actually doing?

When you pass any constant argument in COUNT() (ie 'x' is constant argument), it will return total number of records :

So, with your query it will return the records based on Columns which are defined with GROUP BY clause.

SELECT Column1, Column2, Column3, Count('x') AS CountX
FROM [SchmaName].[SQLView]
WHERE ("SomeWhereStatement")
GROUP BY Column1, Column2, Column3
HAVING Count('x') > 1; 

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