0

What is wrong with:

select count (a.*, b.*)
from tableA a, tableB b
where a.x = 'blah'
and b.x = 'blah'

I keep getting the error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '*'.

I have a bunch of table names to select from so wanted to use table name aliases? Any help appreciated: I'm not very good with sql.

7
  • count(*) will do fine here! Or perhaps count(a.columnname). Commented Apr 8, 2015 at 14:19
  • should've made my question clearer, i have a bunch of tables to select from. updating my question to show this... Commented Apr 8, 2015 at 14:21
  • 2
    Yeah--that's just plain not going to work. Please explain what result you are trying to get. A sample table of results will be helpful. Commented Apr 8, 2015 at 14:25
  • select count( * ) from tableA union all select count( * ) from tableB etc Commented Apr 8, 2015 at 14:27
  • 1
    Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 20 years ago) and its use is discouraged Commented Apr 8, 2015 at 14:43

3 Answers 3

5

You cannot use multiply arguments in COUNT. In your case you are asking to give you count of a.col1, a.col2, a.col3... COUNT expects just 1 argument. The only exception is * in that case it understands that it is supposed to calculate all the records from the table.

If you would like to calculate all the records from a, then do something like COUNT(a.id). If you want to calculate unique records of a table, then do COUNT(distinct a.id).

If you want to calculate all the records from a and b tables, then you can do COUNT(a.id) as quantityOfAtable, COUNT(b.id) as quantityOfBtable. Or you can sum them COUNT(a.id) + COUNT(b.id).

One more thing you need to know about COUNT that it is counting only the NOT NULL rows. So, if you are using LEFT JOIN or the column that can be NULL then it will just calculate the amount of the records where this column is NOT NULL. Of course, you can use DISTINCT to calculate unique records as well.

Sign up to request clarification or add additional context in comments.

Comments

0
  • COUNT() function takes only one parameter.

count (a.*): means count(a.field1,a.field2,a.field3,....) which is invalid.

you have to use count(*): means count all .

Comments

0

You should be using JOIN syntax here:

select count(*)
from tableA a
inner join tableB b
  on a.x = b.x
where a.x = 'blah'

Comments

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.