trying to get only one row from each group - sql

I have a problem with DISTINCT ON. I have five different groups of people which include names and surnames. My goal is to get only one name per group (the first one). When try to use DISTINCT ON, I got an error.
SELECT DISTINCT ON (group_A) surname FROM table
I want my table to look like
group_A...surname_a
broup_B...surname_b
.
.
.
Thank you for your help

The syntax in Postgres would be:
SELECT DISTINCT ON (group_A) group_A, surname
FROM table
ORDER BY group_A;

Related

How to get the number of time a particular number appeared

I want count the number of times a single data occured in a column, how can I achieve that using mysqli. For instance I want to know the number of times Victor appeared in the column of name.
If you're using SQL server:
SELECT name, count(1)
from Tablename
where name like 'Victor'
group by name
This query will give you results like - eg Victor appeared 22 times:
Victor 22
Is this what you're looking for?
Please provide more information so its easier to assist.
Try window functions
SELECT ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name DESC)
AS Total, Name from table
this one will give you count of records with name='Victor'
select count(name) as cnt from t where name='Victor'
this one will give you all names with counts
select name, count(1) as cnt
from t
group by name
order by name

Count specific column in DB2

I have a table contact with three columns e.g. name, surname and age.
I would like to count the number of entries from the specific column surname.
How looks the select statement in DB2 to achieve this?
You can change the column name using as :
select count(surname) as surname_count
from contact c;
I assume you want to perform a
select count(surname)
from contact
group by surname
but you need to put some effort into the question and prove you have already researched a bit beforehand

SQL Finding Duplicates with multiple criteria but different ID numbers

I work with claims and I have been trying to write a query that captures different claim numbers with multiple criteria, however, I can not get the desired return. The picture I attached is some what an idea of a table I am working with. I need to return different claim numbers with the following criteria being the same:
Sum(billed), Diagnosis_code, Rev_code, Cpt_Code, POS_Code, Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID.
Those criteria need to match exactly and the may not follow the same ascending or descending order as shown in the table. Here is the screen shot
I only want claim_no 101 and 102 to return because they have different claim numbers but match everything else. I do not want claim_no 103 because it does not match all of the above criteria.
I work with SQL Server 2012. Don't know if it maters but DOS data type is datetime. Any help will be greatly appreciated. Thanks.
If you want rows that match another row, you can do something like this:
select t.*
from t
where exists (select 1
from t t2
where t2.claim_no <> t.claim_no and
t2.Diagnosis_code = t.Diagnosis_code and
t2.Rev_code = t.Rev_code and
. . .
);
Fill in the . . . with the conditions that you want.
As per your sample data, below query will be work. If you want to add filter condition you can append with this query.
Select Clime_no, Sum(billed), Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID
from table_name
group by Clime_no, Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID

SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

I have an SQL table with duplicate records on FacilityID. FacilityKey is unique. I need to select rows that have duplicates on FacilityID but I only want to show one record for each and I want to choose the one with the most recent (highest) FacilityKey. Can anybody help me figure out how to write my query? I've tried everything I could think of and searched the internet for something similar to no avail. All I can find are examples of identifying the duplicate records.
Something like this should work:
select FacilityID, max(FacilityKey)
from Facilities
group by FacilityID
having count(FacilityID)>1
And then if you want to get all of the fields, something like this:
select *
from facilities
inner join (
select FacilityID, max(FacilityKey) as maxkey
from Facilities
group by FacilityID
having count(FacilityID)>1
) t on t.FacilityID = facilities.FacilityID and t.maxkey=facilities.FacilityKey
try this:
select
FacilityKey , MAX(FacilityID) AS FacilityID
From YourTable
GROUP BY FacilityKey
HAVING COUNT(*)>1

SELECT information from another query SQL

I am trying to select information from an already run query using SQL. I don't have the rights to create a view which I am aware would solve my problem. I have run the below (obfuscated) query which is throwing a number of errors:
SELECT
distinct(countValue),
count(countValue)
FROM
(
SELECT customer_identifier, count(distinct(2nd_customer_identifier)) AS countValue FROM table GROUP BY customer_identifier;
)
GROUP BY
distinct(countValue)
The subquery (below) is attempting to get a count of the unique payees for every customer:
SELECT
customer_identifier,
count(distinct(2nd_customer_identifier)) AS countValue
FROM table
GROUP BY aid
and the main query using that is attempting to get the counted values from the above table and count how many times each one occurs.
Any help would be much appreciated.
Can you logically replace it with the following? This will tell you how many multiple-payer situations you have.
SELECT
CountValue,
count(countValue) TotalRecords
FROM
(
SELECT
customer_identifier,
count(distinct([2nd_customer_identifier])) AS countValue
FROM table
GROUP BY customer_identifier
) a
GROUP BY countValue ;
Output will tell you something along the lines of:
There were 25 one-payer accounts
There were 17 two-payer accounts
There were 9 three-payer accounts
Etc.
If that's not what you want, please edit your question to describe the output you are after.