Select unique rows based on only some columns - sql

I have an SQL view, and I want to get unique rows based on only some columns.
If all other columns are the same (excluding A), duplicates must be deleted.
and
If all other columns are the same (excluding B), duplicates must be deleted.
I used the code below, but it still gives me the max of A then the Max of B, and what I want is to get distinct rows excluding these two columns, and I want to keep these columns in the select function.
SELECT Name, Account, MAX(A), MAX(B)
FROM MyTable
GROUP BY Name, Account
Thanks in advance!

Have not tested, but is this what you are searching for?
select sub.Name,sub.Account,MAX(sub.prod)
from
(
select Name,Account,MAX(A) as prod from MyTable group by Name,Account
union all
select Name,Account,MAX(B) as prod from MyTable group by Name,Account
) as sub
group by sub.Name, sub.Account

Related

Is it possible to apply "Select Distinct" to any column of the query that isn’t in the first place?

For example, like the query below:
WITH T1 AS
(
SELECT DISTINCT
song_name,
year_rank AS rank,
group_name
FROM
billboard_top_100_year_end
WHERE
year = 2010
ORDER BY
rank
LIMIT 10
)
SELECT
rank,
group_name,
song_name
FROM
T1
LIMIT 10
I need to put the column song_name on the top because I didn’t know how to use DISTINCT if the column song_name was in third place.
So, after I needed to query again just to obtain the exactly same result but by another order of visualization.
DISTINCT does not apply to a certain column of the result set, but to all. It just eliminates duplicate result rows.
SELECT DISTINCT a, b, c FROM tab;
is the same as
SELECT a, b, c, FROM tab GROUP BY a, b, c;
Perhaps you are looking for the (non-standard!) PostgreSQL extension DISTINCT ON:
SELECT DISTINCT ON (song_name)
song_name, col2, col2, ...
FROM tab
ORDER BY song_name, col2;
With the ORDER BY, this will give you for each song_name the result with the smallest col2. If you omit the ORDER BY, you will get a random result row for each song_name.

Removing doubling lines

I have written a union query but I need to eliminate the lines that are duplicated (line 2 and 3 in the column 'kods') and leave only distinct values of column 'kods'. How can that be done?
You need to decide which of the id values to discard using either min or max and group by the remaining columns. you don't need distinct and can union all since group by will perform the dedupe.
select kods, min(id) id, vards, uzvards from (
select kods, id, vards, uzvards
from dataset
union all
select kods, id, vards, uzvards
from dataset_2
)x
group by kods, vards, uzvards

SQL : Find Numbers of Rows in a Table according to Criteria

I want to get numbers of rows in a table according to certain criteria.
Please see the below table:-
Herein I want to get numbers of rows according to Column StationTo.
I want to get numbers of rows of each StationTo entries.
You could group by the StationTo and use the aggregate count(*) function:
SELECT StationTo, COUNT(*)
FROM mytable
GROUP BY StationTo
EDIT:
If you just want the number of rows for a single StationTo, you could use a where clause:
SELECT COUNT(*)
FROM mytable
WHERE StationTo = 'P11004400000'
Hi have you master table for stationTo records?
select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count.
or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'

How to apply Count on multiple distinct columns and use Having clause

I would like to do something like this , but getting an error please suggest some good methods?
select A,B,C, count(Distinct A,B,C)
from table_name
group by A,B,C
having count(Distinct A,B,C) > 1
Basically i have an index on the columns(A,B,C), and some rows doesnt have this unique combination set, So I'm trying a query similar to identify the rows which disobeys the unique constraint. PLease let me know if there is a best way
If you group by these columns then you already only get those unique records and then you can use count(*) to get how many duplicates you have
select A,B,C, count(*)
from table_name
group by A,B,C
HAVING count(*) > 1
What #jurgend said is right, and you can further find the exact rows (I'm assuming there are more fields to look at, including maybe a PK) by doing
SELECT *
FROM table_name
WHERE (A,B,C) IN (
SELECT A, B, C
FROM table_name
GROUP BY A, B, C
HAVING COUNT(*) > 1
)
A Tuple IN list query works in Oracle, although not all other DBMS.

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