How to count and select records with count = 1 across multiple columns - sql

Hi and thanks in advance.
Trying to simplify two SQL statements without much success.
Count unique records in a table (by unique, I mean the whole record is found only once in the table)
Select unique records in a table (by unique, I mean the whole record is found only once in the table)
Since the table has 30 columns, is there some way to simply specify ALL columns in the table rather than having to include all individually in the SQL?
I got this working where you spell out every column name (where 'col n name' refers to the last column) but it is not ideal since there are just so many columns …
SELECT col 1 name, col 2 name, col 3 name, …, col n name FROM table name
GROUP BY col 1 name, col 2 name, col 3 name, …, col n name
Having Count(*)=1
Thanks
deutz

create view tab_view1 as select DISTINCT * from tab;
select COUNT(*) from tab_view1; -- first desired result
select * from tab_view1; -- second desired result
First occurence is unique, second is not.
If you want to exclude any record that has duplicate:
create view tab_view2 as select tab.*, COUNT(*) AS occurs
from tab group by tab.*
having COUNT(*) = 1;
select COUNT(*) from tab_view2; -- first desired result
select * from tab_view2; -- second desired result

Related

Count items in column SQL query

Let's say I have a table that looks like,
id
2
2
3
4
5
5
5
How do I get something like,
id count
2 2
3 1
4 1
5 3
where the count column is just the count of each id in the id column?
You want to use the GROUP BY operation
SELECT id, COUNT(id)
FROM table
GROUP BY id
select id, count(id) from table_name group by id
or
select id, count(*) from table_name group by id
This is your query:
SELECT id, COUNT(id)
FROM table
GROUP BY id
What GROUP BY clause does is this:
It will split your table based on ids i.e all your 1's are separated, then the 2's , 3's and so on. You can assume it like new tables are created where in one table all the 1's are stored, 2's in another , 3's in yet another and so on.
Then after that the SELECT query is applied on each of these separate tables and the result is returned for each of these "groups".
Good luck!
Kudos! :)

SQL Separating Distinct Values using single column

Does anyone happen to know a way of basically taking the 'Distinct' command but only using it on a single column. For lack of example, something similar to this:
Select (Distinct ID), Name, Term from Table
So it would get rid of row with duplicate ID's but still use the other column information. I would use distinct on the full query but the rows are all different due to certain columns data set. And I would need to output only the top most term between the two duplicates:
ID Name Term
1 Suzy A
1 Suzy B
2 John A
2 John B
3 Pete A
4 Carl A
5 Sally B
Any suggestions would be helpful.
select t.Id, t.Name, t.Term
from (select distinct ID from Table order by id, term) t
You can use row number for this
Select ID, Name, Term from(
Select ID, Name, Term, ROW_NUMBER ( )
OVER ( PARTITION BY ID order by Name) as rn from Table
Where rn = 1)
as tbl
Order by determines the order from which the first row will be picked.

Number of times one row column equals another row's other column in SQL

The confusing question is best asked through an example. Say we have the following result set:
What I want to do is count how many times one number appears from both columns.
So the returning data set might look like:
ID Counted
0 4
1 2
9 1
13 1
My original thought was to do some sort of addition between the counts on both IDs, but I'm not exactly sure how to GROUP them in SQL in a way that is working.
You can do this with a subquery, GROUP BY, and a UNION ALL, like this:
SELECT ID, COUNT(*)
FROM(
SELECT ID1 AS ID FROM MyTable
UNION ALL
SELECT ID2 AS ID FROM MyTable
) source
GROUP BY ID
ORDER BY ID ASC

Get row count including column values in sql server

I need to get the row count of a query, and also get the query's columns in one single query. The count should be a part of the result's columns (It should be the same for all rows, since it's the total).
for example, if I do this:
select count(1) from table
I can have the total number of rows.
If I do this:
select a,b,c from table
I'll get the column's values for the query.
What I need is to get the count and the columns values in one query, with a very effective way.
For example:
select Count(1), a,b,c from table
with no group by, since I want the total.
The only way I've found is to do a temp table (using variables), insert the query's result, then count, then returning the join of both. But if the result gets thousands of records, that wouldn't be very efficient.
Any ideas?
#Jim H is almost right, but chooses the wrong ranking function:
create table #T (ID int)
insert into #T (ID)
select 1 union all
select 2 union all
select 3
select ID,COUNT(*) OVER (PARTITION BY 1) as RowCnt from #T
drop table #T
Results:
ID RowCnt
1 3
2 3
3 3
Partitioning by a constant makes it count over the whole resultset.
Using CROSS JOIN:
SELECT a.*, b.numRows
FROM YOUR_TABLE a
CROSS JOIN (SELECT COUNT(*) AS numRows
FROM YOUR_TABLE) b
Look at the Ranking functions of SQL Server.
SELECT ROW_NUMBER() OVER (ORDER BY a) AS 'RowNumber', a, b, c
FROM table;
You could do it like this:
SELECT x.total, a, b, c
FROM
table
JOIN (SELECT total = COUNT(*) FROM table) AS x ON 1=1
which will return the total number of records in the first column, followed by fields a,b & c

select data and if duplicate id add (sum) the data

I am using TSQL to select data from the database. I want to select all data in a table but if the id is a duplicate add (sum) all column 'a' with same ID and make it just 1 row.
There should be no duplicate IDs in the output.
SELECT DISTINCT id,a,b FROM dbo.test WHERE
id not in (select id from dbo.test) CASE a WHEN a + a??
example:
dbo.test
========
id a
1 4
1 5
2 3
3 2
output:
1 9 <-- two ids of 1 so column 'a' is added together.
2 3
3 2
SELECT ID, SUM(a) as a, SUM(b) as B
FROM MyTable
GROUP BY ID
This is what grouping and aggregation is designed for!
FYI, this is how aggregation works:
When you group and aggregate fields, you are combining records into a single row.
In your OP, you wanted to see a SUM of A for every value of ID. That's what the original query I posted does.
You also want to include B, which is a varchar field, according to your comments.
In that case, you need to decide what to do with B. Since you are grouping multiple rows together, there are (potentially) multiple values of B per value of ID. You need to either:
Group by B as well, which will add extra rows
Apply an aggregation such as MAX(), MIN(), etc. to B
Exclude B from the results list.