Find the unique value in column MS SQL database - sql

I have a set of data as below
number quantity
1 4
2 6
3 7
4 9
2 1
1 2
5 4
I need to find the unique value in the column "number"
The output should look like this:
number quantity
3 7
4 9
5 4
Any help would be appreciated. I am using MS SQL

In the inner query get all the distinct numbers, then join with again with the main table to get your expected results.
select o.*
from mytable o , (select number
from mytable
group by number) dist
where o.number = dist.number

One way to go could be to have an aggregate query that counts the number of occurrences for each number use it in a subquery:
SELECT number, quantity
FROM my_table
WHERE number IN (SELECT number
FROM my_table
GROUP BY number
HAVING COUNT(*) = 1)

If your column name is my_column in table my_table, the query is:
SELECT my_column, COUNT(*) as count
FROM my_table
GROUP BY my_column
HAVING COUNT(*) > 1
This will return all records that have duplicate my_column content, as well as how many times this content occurs in the database.
you can use below code for desire output:
SELECT DISTINCT(my_column), COUNT(*) as count
FROM my_table
GROUP BY my_column

Try this :
SELECT *
FROM yourtable t1
WHERE (SELECT Count(*)
FROM yourtable t2
WHERE t1.number = t2.number) = 1
Query in where clause will return number of occurrences of each number and checking it with 1 will return only those rows will have only one occurrence in table.

You can probably use ROW_NUMBER() analytic function like
select * from
(
select number,
quantity,
ROW_NUMBER() OVER(PARTITION BY number ORDER BY number) AS rn
from table1
) tab where rn = 1;

Try this:
create table #TableName(number int, quantity int)
insert into #TableName values(1, 2)
insert into #TableName values(1, 4)
insert into #TableName values(2, 4)
SELECT number, quantity
FROM #TableName
WHERE number
IN(SELECT number
FROM #TableName
GROUP BY number
HAVING COUNT(NUMBER) = 1)

Related

BigQuery Count Unique and Count Distinct

I am looking for SQL to count unique values in the column.
I am aware of DISTINCT - that gives me how many unique values there are. However, I am looking for - how many ONLY unique values there are.
So if my data is Letters: {A,A,A,B,B,B,C,D}. I am looking to get:
Count Distinct = 4 {A,B,C,D) and
Count Unique = 2 {C,D} <== this is what I am looking for
I am working with BigQuery.
Thank You,
Do
Below query will return only unique values in the column.
SELECT col
FROM UNNEST(SPLIT('A,A,A,B,B,B,C,D')) col
GROUP BY 1 HAVING COUNT(1) = 1;
Then, you can simply count rows.
WITH uniques AS (
SELECT col
FROM UNNEST(SPLIT('A,A,A,B,B,B,C,D')) col
GROUP BY 1 HAVING COUNT(1) = 1
)
SELECT COUNT(*) cnt FROM uniques;
Another option
select count(*) from (
select * from your_table
qualify 1 = count(*) over(partition by col)
)

Finding Occurrence of the duplicate values

I have table with 3 columns (id, Name, Occurrence), I want to update the Occurrence column ,based on the id column, attached snap for the reference.
for example if my id column has "606" value 3 times then my occurrent column should have 3 against all the "606" value.
Below is the method which I tried.
I tried to find the duplicate values using group by and Having clause and saved it in a temp table and from there I tried to join the table value from the temp table.
you can use window functions in an updatable CTE for this.
You haven't supplied any actual sample data so this is untested, however the following should work:
with x as (
select Id, Occurence, count(*) over(partition by Id) qty
from Table
)
update x
set Occurence = Qty;
You can go for GROUP BY based approach also.
declare #TABLE TABLE(ID INT, NAME CHAR(3), occurance int null)
insert into #TABLE VALUES
(1,'AAA',NULL),(1,'AAA',NULL),(2,'CCC',NULL),(3,'DDD',NULL), (3,'DDD',NULL),(4,'EEE',NULL),(5,'FFF',NULL);
;WITH CTE_Table as
(
SELECT ID, COUNT(*) AS Occurance
FROM #table
group by id
)
UPDATE t
SET occurance = c.occurance
FROM #table t
INNER JOIN CTE_Table as c
on C.ID = T.ID
SELECT * FROM #TABLE
ID
NAME
occurance
1
AAA
2
1
AAA
2
2
CCC
1
3
DDD
2
3
DDD
2
4
EEE
1
5
FFF
1
You can use a CTE and calculate row number and update your table base on CTE
;WITH q
AS
(
SELECT Id,COUNT(1) 'RowNum'
FROM YourTable
GROUP BY Id
)
UPDATE YourTable
SET Occurrence=q.RowNum
FROM YourTable t
INNER JOIN q
ON t.Id=q.Id

sql - getting sum of same column from multiple tables

I have a few tables in my DB. Let's call them table1, table2, table3.
All of them have a column named value.
I need to create a query that will return a single number, where this number is the sum of all the value columns from all the tables together...
I've tried the following way:
SELECT (SELECT SUM(value) FROM table1) + (SELECT SUM(value) FROM table2) + (SELECT SUM(value) FROM table3) as total_sum
But when at least one of the inner SUM is NULL, the entire total value (total_sum here) is NULL, so that's not very trustworthy.
When there is no value in a certain inner SUM query, I need it to return 0, so it doesn't affect the rest of the SUM.
To make it more clear, let's say I have the following 2 tables:
TABLE1:
ID | NAME | VALUE
1 Name1 1000
2 Name2 2000
3 Name3 3000
TABLE2:
ID | NAME | VALUE
1 Name1 1500
2 Name2 2500
3 Name3 3500
Eventually, the query I need will return a single value - 13500, which is the total sum of all the values under the VALUE column of all the tables here.
All the other columns have no meaning for the needed query, and I even don't care much for performance in this case.
You can achieve it using Coalesce as follows
SELECT
(SELECT coalesce(SUM(value),0) FROM table1) +
(SELECT coalesce(SUM(value),0) FROM table2) +
(SELECT coalesce(SUM(value),0) FROM table3) as total_sum
Another approach is to use union all to merge all values into single table
select distinct coalesce(sum(a.value), 0) as total_sum from
(select value from table1
union all
select value from table 2
union all
select value from table 3) a;
You can use the ISNULL function to take care of the NULLs.
SELECT ISNULL((
SELECT SUM(value) FROM table1
)
, 0
) + ISNULL((
SELECT SUM(value) FROM table2
)
, 0
) + ISNULL((
SELECT SUM(value) FROM table3
)
, 0
) AS total_sum;
You could simply sum all of them:
select sum(total) as Total
from (
select sum(value) as total from Table1
union all
select sum(value) as total from Table2
union all
select sum(value) as total from Table3
) t;

SQL - Insert if the number of rows is greater than

I have created a SQL query that will return rows from an Oracle linked server. The query works fine and will return 40 rows, for example. I would like the results to only be inserted into a table if the number of rows returned is greater than 40.
My thinking would then be that I could create a trigger to fire out an email to say the number has been breached.
DECLARE #cnt INT
SELECT #cnt = COUNT(*) FROM LinkedServer.database.schemaname.tablename
IF #cnt > 40
INSERT INTO table1 VALUES(col1, col2, col3 .....)
Let's say that the query is:
select a.*
from remote_table a
Now you can modify the query:
select a.*, count(*) over () as cnt
from remote_table a
and will contain the number of rows.
Next,
select *
from (
select a.*, count(*) over () as cnt
from remote_table a
)
where cnt > 40;
will return only if the number of rows is greater than 40.
All you have to do is
insert into your_table
select columns
from (
select columns, count(*) over () as cnt
from remote_table a
)
where cnt > 40;
and will insert only if you have more than 40 rows in the source.
Create Procedure in sqlserver and use count() function for conditional checking for row count or use ##ROWCOUNT.
if ((select count(*) from Oraclelinkservertable) > 40)
begin
-- code for inserting in your table
Insert into tablename
select * from Oraclelinkservertable
end
Try using OFFSET.
SELECT * FROM tableName ORDER BY colName OFFSET 40 ROWS

select a set of values as a column without CREATE

I'm trying to write a query that will return all QUERY_ID values alongside all matching TABLE_ID values, where QUERY_ID is not specified in any table, and I can't create tables, so have to specify it in the query itself:
QUERY_ID TABLE_ID
1 1
2 NULL
3 3
4 4
5 NULL
I feel like there ought to be a simple way to do this, but I can't think of it for the life of me. Any help would be wonderful. Thanks!
select q.QUERY_ID, t.TABLE_ID
from (
select 1 as QUERY_ID
union all
select 2
union all
select 3
union all
select 4
union all
select 5
) q
left outer join MyTable t on q.QUERY_ID = t.TABLE_ID
one way by using the built in master..spt_values table
SELECT number AS QUERY_ID,TABLE_ID
FROM master..spt_values v
LEFT JOIN YourTable y ON y.QUERY_ID = y.TABLE_ID
WHERE TYPE = 'p'
AND number > 0
AND number <= (SELECT COUNT(*) FROM YourTable)
order by QUERY_ID
are you able to create #temp tables...can you do this?
create table #temp(QUERY_ID int identity,TABLE_ID varchar(200))
insert #temp(TABLE_ID)
select TABLE_ID
from YourTable
select * from #temp
order by QUERY_ID
drop table #temp
or like this
select identity(int,1,1) as QUERY_ID,TABLE_ID
into #temp
from YourTable
select * from #temp
order by QUERY_ID
On sql server 2005 and up there is the row_number function so maybe a reason to upgrade :-)