Select from another select without temporary table? - sql

There is table of numbers containing values [1,1,2,3,7,3,4,2,7]
I want to select only duplicates so output set would contain [1,2,3,7] so 4 is filtered out.
I have code that I did like this:
DROP TABLE #tempTable;
SELECT [numbers] as nums, COUNT(*) as cny
INTO #tempTable
FROM [testBase].[dbo].[numbers] group by numbers;
SELECT nums from #tempTable where nums > 1;
Now I would like to know if I can get it without first selecting to #tempTable?
Something more like select from select or is it only way I can do it?

The same in one query.
Conditions that are made after GROUP BY should be added to HAVING clause.
SELECT [numbers] as nums, COUNT(*) as cny
FROM [testBase].[dbo].[numbers]
GROUP BY numbers
HAVING COUNT(*) > 1
More on this
HAVING (Transact-SQL)
Having clause tutorial

Try this:-
SELECT [numbers] AS NUM
FROM TAB_NAME
HAVING COUNT([numbers]) > 1;

Try this..
select count(numbers) from TABLE group by numbers having count(*)>1

SELECT numbers FROM
numbers
GROUP BY numbers
having Count(numbers) >= 2

Use Having clause. Having specifies a search condition for a group or an aggregate function used in SELECT statement.. So in the having clause you can Filter the distinct rows.
SELECT [numbers]
FROM [testBase].[dbo].[numbers]
GROUP BY [numbers]
HAVING Count([numbers]) > 1

Related

I can't figure out how to do this DISTINCT

Good morning
I tried and tried to understand why this Query gives the usual error on Group By. I would like to find the duplicate lines and delete them. I found this query on Microsoft's MSDN but despite this it keeps giving me this error on Group By.
The main table has 3 fields "Id, Item, Description", the table name is "tlbDescription", this query should in theory create a table named "duplicate_table" insert the duplicate values inside the "duplicate_table", then delete the values from table "tlbDescription" and finally delete the table "duplicate_table".
If someone can kindly give me a hand
Thank you
Fabrizio
This is the query:
SELECT DISTINCT *
INTO duplicate_table
FROM [tlbDescrizione]
GROUP BY [Articolo]
HAVING COUNT([Articolo]) > 1
DELETE [tlbDescrizione]
WHERE [Articolo] IN (SELECT [Articolo] FROM duplicate_table)
INSERT [tlbDescrizione]
SELECT * FROM duplicate_table
DROP TABLE duplicate_table
This query doesn't make sense:
SELECT DISTINCT *
INTO duplicate_table
FROM [tlbDescrizione]
GROUP BY [Articolo]
HAVING COUNT([Articolo]) > 1;
It is selecting all columns but is an aggregation query because of the GROUP BY. Hence, the SELECT columns are inconsistent with the GROUP BY columns and you get an error.
If you want all the columns then you can use window functions:
SELECT DISTINCT *
INTO duplicate_table
FROM (SELECT d.*, COUNT(*) OVER (PARTITION BY d.Articolo) as cnt
FROM tlbDescrizione d
) d
WHERE cnt > 1;
Or, if you want only the ids:
SELECT Articolo
INTO duplicate_table
FROM tlbDescrizione
GROUP BY [Articolo]
HAVING COUNT(*) > 1;

How to use to functions - MAX(smthng) and after COUNT(MAX(smthng)

I don't understand why I can't use this in my code :
SELECT MAX(SMTHNG), COUNT(MAX(SMTHNG))
FROM SomeTable;
Searched for an answer but didn't find it in documentation about these aggregate functions.
Also I get an SQL-compiler error "Invalid column name "SMTHNG"".
You want to know what the maximum SMTHNG in the table is with:
SELECT MAX(SMTHNG) FROM SomeTable;
This is an aggregation without GROUP BY and hence results in one single row containing the maximum SMTHNG.
Now you also want to know how often this SMTHNG occurs and you add COUNT(MAX(SMTHNG)). This, however, does not work, because you can not aggregate an aggregate directly.
This doesn't work either:
SELECT ANY_VALUE(max_smthng), COUNT(*)
FROM (SELECT MAX(smthng) AS max_smthng FROM sometable) t;
because the sub query only contains one row, so it's too late to count.
So, either use a sub query and select from the table again:
SELECT ANY_VALUE(smthng), COUNT(*)
FROM sometable
WHERE smthng = (SELECT MAX(smthng) FROM sometable);
Or count per SMTHNG before looking for the maximum. Here is how to get the counts:
SELECT smthng, COUNT(*)
FROM sometable
GROUP BY smthng;
And the easiest way to get the maximum from this result is:
SELECT TOP(1) smthng, COUNT(*)
FROM sometable
GROUP BY smthng
ORDER BY COUNT(*) DESC;
First of all, please read my comment.
Depending on what you're trying to achieve, the statement have to be changed.
If you want to count the highest values in SMTHNG field, you may try this:
SELECT T1.SMTHNG, COUNT(T1.SMTHNG)
FROM SomeTable T1 INNER JOIN
(
SELECT MAX(SMTHNG) AS A
FROM SomeTable
) T2 ON T1.SMTHNG = T2.A
GROUP BY T1.SMTHNG;
use cte like below or subquery
with cte as
(
select count(*) as cnt ,col from table_name
group by col
) select max(cnt) from cte
you can not use double aggregate function at a time on same column

how do I select value with the result of count(*) group by?

I have a table like this:
Each keyword should have 4 pieces of data.
And I want to select the keywords that are less than 4 pieces of data.
I used select count(*) from mytable group by keyword to achieve something like this:
how can I select the keywords based on the count(*) result?
Here I want the keyword b from the result grid.
I am new to sql, thanks in advance.
to filter result by aggregation functions (like count) you need to use having, for example:
select keyword, count(*)
from mytable
group by keyword
having count(*) < 4
Just include having clause something like that :
select keyword
from mytable t
group by keyword
having count(*) < 4;

count all the distinct records in a table

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

SELECT *, COUNT(*) in SQLite

If i perform a standard query in SQLite:
SELECT * FROM my_table
I get all records in my table as expected. If i perform following query:
SELECT *, 1 FROM my_table
I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:
SELECT *, COUNT(*) FROM my_table
I get only ONE row (with rightmost column is a correct count).
Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.
SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.
You'd want something like
SELECT somecolumn,someothercolumn, COUNT(*)
FROM my_table
GROUP BY somecolumn,someothercolumn
If you want to count the number of records in your table, simply run:
SELECT COUNT(*) FROM your_table;
count(*) is an aggregate function. Aggregate functions need to be grouped for a meaningful results. You can read: count columns group by
If what you want is the total number of records in the table appended to each row you can do something like
SELECT *
FROM my_table
CROSS JOIN (SELECT COUNT(*) AS COUNT_OF_RECS_IN_MY_TABLE
FROM MY_TABLE)