In SQL Server 2016 I miss this error in this query:
select count(*)
from (select count(*), clave
from products
where state = 1
group by key
having count(*) > 1 );
I have tried to copy and paste the query in a note pad in case some invalid character or space has been inserted.
You need alias :
select count(*)
from (select count(*)
from products
where [state] = 1
group by [key]
having count(*) > 1
) t; -- t alias
Considering to use only words or identifiers which have not reserved by SQL Server, such as key (especially in your case) & many more.
Second thing when you include group by clause with your query you should be care about select statement with expressions/columns (which are available in group by clause or the expression/column which are not in group by clause you should include aggregate function to that column/expression.)
'Key' is a reserve word so you can not use it so you should rename it and your result is a table so you should give it an alias.
Try something like
`
select count(*)
from (select count(*), clave
from products
where state = 1
group by key1
having count(*) > 1 ) AS product_alias;
'
Related
I have this table called item:
| PERSON_id | ITEM_id |
|------------------|----------------|
|------CP2---------|-----A03--------|
|------CP2---------|-----A02--------|
|------HB3---------|-----A02--------|
|------BW4---------|-----A01--------|
I need an SQL statement that would output the person with the most Items. Not really sure where to start either.
I advice you to use inner query for this purpose. the inner query is going to include group by and order by statement. and outer query will select the first statement which has the most items.
SELECT * FROM
(
SELECT PERSON_ID, COUNT(*) FROM TABLE1
GROUP BY PERSON_ID
ORDER BY 2 DESC
)
WHERE ROWNUM = 1
here is the fiddler link : http://sqlfiddle.com/#!4/4c4228/5
Locating the maximum of an aggregated column requires more than a single calculation, so here you can use a "common table expression" (cte) to hold the result and then re-use that result in a where clause:
with cte as (
select
person_id
, count(item_id) count_items
from mytable
group by
person_id
)
select
*
from cte
where count_items = (select max(count_items) from cte)
Note, if more than one person shares the same maximum count; more than one row will be returned bu this query.
I need to count all the distinct records in a table name with a single query and also without using any sub-query.
My code is
select count ( distinct *) from table_name
It gives an error:
Incorrect syntax near '*'.
I am using Microsoft SQL Server
Try this -
SELECT COUNT(*)
FROM
(SELECT DISTINCT * FROM [table_name]) A
I'm afraid that if you don't want to use a subquery, the only way to achieve that is replacing * with a concatenation of the columns in your table
select count(distinct concat(column1, column2, ..., columnN))
from table_name
To avoid undesired behaviours (like the concatenation of 1 and 31 becoming equal to the concatenation of 13 and 1) you could add a reasonable separator
select count(distinct concat(column1, '$%&£', column2, '$%&£', ..., '$%&£', columnN)
from table_name
You can use CTE.
;WITH CTE AS
(
SELECT DISTINCT * FROM TableName
)
SELECT COUNT(*)
FROM CTE
Hope this query gives you what you required.
As others mentioned, you cannot use DISTINCT with *. Also it is good practice to use a column name instead of the *, like a unique key / primary key of the table.
SELECT COUNT( DISTINCT id )
FROM table
select distinct Name , count(Name) from TableName
group by Name
having count(Name)=1
select ##rowcount
I had the same issue involving a query that had multiple joins to tables and I could not simply do count(distinct ) or count(distinct alias.).
My solution was to create a string made up of the key columns I cared about and count them.
SELECT Count(DISTINCT person.first || '~' || person.last)
from person;
If you want to use DISTINCT keyword, you need to specify column name on which bases you want to get distinct records.
Example:
SELECT count(DISTINCT Column-Name) FROM table_name
I have the following sqlfiddle:
CREATE TABLE tester(
name TEXT,
address TEXT
)
Each person in the table can have multiple addresses. I'd like to select all names and the number of addresses they have that have > 1 address. I have tried:
SELECT d.name, count(address) c FROM (SELECT DISTINCT ON(name) FROM tester) d
LEFT JOIN tester ON d.name = links.name
WHERE count(address) > 1
I get:
ERROR: syntax error at or near "FROM" Position: 64
I've also tried a DISTINCT ON query:
SELECT DISTINCT ON(name) name, count(address) FROM tester HAVING count(address) > 1
I get:
ERROR: column "tester.name" must appear in the GROUP BY clause or be used in an aggregate function Position: 26
I feel like I'm making this too difficult.
Simply use GROUP BY:
SELECT name, count(address)
FROM tester
GROUP BY name
HAVING count(address) > 1
GROUP BY in SQL (as well as in other languages) will always produce distinct groups, so there is no need for DISTINCT in this case.
You just need to use group by correctly. Like this:
SELECT name, count(*)
FROM tester
group by name
I'm trying to select the 5 rows with the highest count value
This is my query:
string sql = "SELECT top 5 count FROM Likes ORDER BY COUNT(*) DESC";
It's just throwing an error code that
Column 'Likes.count' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
It's for a project I've got to present tomorrow...
On SQL Server, simply do this:
SELECT TOP 5 * FROM Likes ORDER BY [Count] DESC
This assumes that your Likes-table already contains a column named [Count] meaning that you don't need to count the records yourself (which is what COUNT(*) does).
You should not use COUNT(*) here for order by.
SELECT top 5 [count] FROM Likes ORDER BY [Count] DESC
count is a reserved word which is why you should stay clear of using them for column names. If you don't want to rename the column you can escape it, different dbms may effect who you do this. In ssms you would use square brackets.
string sql = "SELECT top 5 [count] FROM Likes ORDER BY COUNT(*) DESC";
I have a Personal table with a LastName column and a MaybeUniqueID.
I want to put in output a table with a LastName column where the counter set on to the column MaybeUniqueID gives more than 1 row.
I would like to do everything in one unique run, avoiding mid-step outputs.
I prefere not using temporary table or table variables, otherwise I would like to use at most one table variable (not temporary tables), but I think this should not be necessary.
I am using Microsoft SQL Server 2005.
I tried different scenarios with different SQL statements like HAVING or GROUP BY, but I failed to get the outcome I am looking for.
Please have a look at the following not-working summary test:
SELECT LastName
FROM Personal
JOIN
(SELECT MaybeUniqueID AS ID2,
COUNT(*) AS CNT
FROM Personal
--WHERE CNT > 1
GROUP BY MaybeUniqueID
HAVING cnt > 1
) AS MultiMaybeUniqueID
ON Personal.MaybeUniqueID = MultiMaybeUniqueID.ID2
HAVING cnt > 1 should be HAVING COUNT(*) > 1.
Column aliases can only be referenced in the ORDER BY clause not the HAVING clause.
Though you could also use
;WITH T AS
(
SELECT LastName,
COUNT(*) OVER (PARTITION BY MaybeUniqueID) AS Cnt
FROM Personal
)
SELECT LastName
FROM T
WHERE Cnt > 1