Count(*) the total unique entries but for 2 different fields - sql-server

I'm working on an old table using SQL Server 2005. (The table isn't designed very well,
but it can't be changed now.)
I'm trying to count the unique entries in 2 columns.
This gives the list I need:
SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable -- automatically removes dups
But how would I count that? (Hopefully with 1 statement.) Something like this, but the
syntax isn't right:
SELECT COUNT(SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable)

Use a subquery:
SELECT COUNT(*) FROM (SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable) AS u

Related

order by on set 1 union set 2 sql

I have this situation:
select name,
subject
from Table_1
where date > getdate()-1
group by name, subject
order by id desc
union
select name,
subject
from table_2
where name not like 'abc%'
Table_1 and table_2 has similar structure.
I need to order by in SET1 UNION SET 2
This is not allowed in sql server. says "ORDER BY items must appear in the select list". I dont understand why the problem is. I am selecting equal number of columns on both queries. only that I want the result set together.
(on SQL Server 2017)
Anybody help!!
Thanks in advance.
Elaborating on my comment
select name,
subject
from Table_1
where date > getdate()-1
--group by name, subject --this isn't needed
union
select name,
subject
from table_2
where name not like 'abc%'
order by <yourCol> desc --notice change here
And for a conditional order by, there are a few posts on that.
Also, you don't need the group by since union removes duplicates.
But, the error is clear that the column you want to order by must be contained in the select list...
If you want to keep the first set ordered before the second set, just use a static column....
select name,
subject,
1 as Sort
from Table_1
where date > getdate()-1
--group by name, subject --this isn't needed
union
select name,
subject,
2 as Sort
from table_2
where name not like 'abc%'
order by Sort asc--notice change here

Remove duplicate lines in sql server

I have a table with the following example format:
ID Name
1 NULL
1 NULL
2 HELLO
3 NULL
3 BYE
My goal is to remove repeated lines with same IDS, but with restrictions.
According to the example, I need to remove a row with ID-1, and the row with ID-3 and with no value (NULL).
I would stick with the table:
ID Name
1 NULL
2 HELLO
3 BYE
How can I do this in sql server? thank you
To just select the data, you can use a simple CTE (common table expression);
WITH cte AS (
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name DESC) rn
FROM myTable
)
SELECT id,name FROM cte WHERE rn=1;
An SQLfiddle to test with.
If you mean to delete the duplicates from the table and not just select the data without updating anything, you could use the same CTE;
WITH cte AS (
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name DESC) rn
FROM myTable
)
DELETE FROM cte WHERE rn<>1;
Another SQLfiddle to test with, and remember to always back up your data before running destructive SQL statements from random people on the Internet.

How to union two tables with Orderby clause?

I have two tables - table1 and table2. Both contains two columns - rollnum,name. Now I wants to select all rows from table1 and randomly 5rows from table2. I have written like this
select rollnum,name from table1 union (select top 5 rollnum,name from table2 order by NEWID())
but it shows an error ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator. please help . I think the mistake is at NEWID(). here rollnum is primary key
The problem is with the brackets. Try this instead
select rollnum,name from table1
union
select * from (select top 5 rollnum,name from table2 order by NEWID()) t
If you could have duplicate entries you may want to consider a union all instead of union
Try This..
SELECT rollnum AS 'NewID' ,
name
FROM table1
UNION
SELECT TOP 5
rollnum ,
name
FROM table2
ORDER BY NewID
NEWID() is a function which assign a value to a variable declared as the uniqueidentifier data type

Any Quicker Option Than Oracle Database Union

I have a table that has multiple columns which store a text value. For example:
ID FATHER_NAME MOTHER_NAME
--------------------------------
1 Henry Sarah
2 Martin Rebecca
3 Martin Nancy
I want to get all of the names in the table. I know I can do a union to do this:
(SELECT FATHER_NAME FROM MY_TABLE)
UNION
(SELECT MOTHER_NAME FROM MY_TABLE)
However, in my real table there are 15 columns I need to union and the query is obviously taking awhile (approximately 12 seconds). And I still need to do joins on these names, etc. Is there any other alternative to doing unions?
FYI: I am using Oracle.
If you are using Oracle 11g, you can use the UNPIVOT function:
select id, value, col
from yourtable
unpivot
(
value for col in (FATHER_NAME, MOTHER_NAME) -- other columns will go here
) u;
See SQL Fiddle With Demo
Or you can use UNION ALL instead of UNION the difference is you will not get DISTINCT values:
select id, FATHER_NAME value, 'FATHER_NAME' col
from yourtable
union all
select id, MOTHER_NAME value, 'MOTHER_NAME' col
from yourtable
See SQL Fiddle With Demo
The UNION might be slower due to it attempting to get the DISTINCT values.

How to display no duplicate records in SQL Server

Does anyone know how can I display records that are not duplicated record inside results in SQL Server?
Use distinct
SELECT DISTINCT * FROM table
Or use group by
select field1,field2,field3 FROM table GROUP BY field1, field2, field3
If you really meant "records that with no duplicate record ", i.e., every row that exists once, and only once, Try this:
Select * From Table
Group By [Here list all columns in Table]
Having Count(*) = 1
Another interpretation of the question.
SELECT *
FROM yourtable t1
WHERE NOT EXISTS
(SELECT *
FROM yourtable t2
WHERE t1.col_to_match_for_duplicates=t2.col_to_match_for_duplicates
AND t1.primarykey <> t2.primarykey
)

Resources