Find out particular id - sql

I have a table in sql like this:
id billpay
-------------------------
1024 0
1024 0
1024 1
1025 1
1025 1
I want to retrieve only those id having billpay 1
Please help me with this

Try this:
select distinct id from yourtable where billpay = 1

It should be like this:
SELECT id FROM tabel WHERE billpay = 1;

This will retrieve those ids in ascending order which have at least one record in the table with billpay = 1.
The DISTINCT keyword will ensure you don't receive back multiple records with the same id.
SELECT DISTINCT id
FROM [TableName]
WHERE billpay = 1
ORDER BY id ASC
If you want to exclude those ids which also have records with billpay = 0, then use this:
SELECT DISTINCT id
FROM [TableName]
WHERE billpay = 1
AND id NOT IN (SELECT id FROM [TableName] WHERE billpay = 0)
ORDER BY id ASC
Regards,

select ID
from MyData
Where billpay = 1
Group By ID
The group by will list unique IDs
select ID
from MyData A
Where not exists (select 'X' from MyData B where B.billpay <> 1 and B.ID = A.ID)
Group By ID
This will only list IDs where billpay is only 1

Try this:
SELECT id
FROM mytable
GROUP BY id
HAVING COUNT(CASE WHEN COALESCE(billpay, 0) <> 1 THEN 1 END) = 0
The above will select only those ids associated to billpay=1 and nothing but billpay=1.
SQL Fiddle Demo

The following query selects the ids from group of ids where the number of records with billpay = 1 is the same as the number of records in the group
select id
from bills
group by id
having sum(billpay) = count(id)

Use NOT EXISTS to find rows with no other than billplay 1, use DISTINCT to return only one of each id found.
select distinct id
from tablename t1
where not exists (select 1 from tablename t2
where t1.id = t2.id
and t2.billpay <> 1)

Try to use GROUP BY +MIN statement to exclude Id's with existing billpay=0
SELECT id
FROM yourtable
GROUP BY id
HAVING MIN(billpay)=1

Related

Postgresql combine IN with NOT IN

I have a table of entities where each can have different statuses. For the sake of keeping history, each status change is reflected by a new row.
Example:
Entity Id Status
123456 1
123456 2
789000 1
Assuming i want to find all rows that have only status 1 (so if they have other statuses they should not be returned), How do I do that?
This query:
select entityid
from tablename
group by entityid
having min(status) = 1 and max(status) = 1
returns all the entityids that you want, so you can use it with the operator IN:
select * from tablename
where entityid in (
select entityid
from tablename
group by entityid
having min(status) = 1 and max(status) = 1
)
Just use not exists:
select t.*
from t
where not exists (select 1
from t t2
where t2.entity_id = t.entity_id and t2.status <> 1
);

SQL query to return rows where only one record is present in a given status

I have a table with data similar to below. I am trying to get a list of results that will display all rows where only one unique SourceID exists in status 10. If I were querying this table, I would expect ID's 3 and 4 to be returned.
Table Example
Select *
From table
Where Status = 10 and Source ID in
(
Select SourceID
From Table
Group by SourceID
Having Count(*) = 1
)
You can use NOT EXISTS :
SELECT t.*
FROM table t
WHERE NOT EXISTS (SELECT 1 FROM table t1 WHERE t1.SourceID = t.SourceID AND t1.Status <> t.Status);
Maybe that would work?
SELECT ID FROM Mytable
WHERE [Status] = 10
GROUP BY ID
HAVING COUNT(SourceID) = 1
First, find out all the unique SourceIDs
SELECT
SourceID
FROM
Data
GROUP BY
SourceID
HAVING
COUNT(SourceID) = 1
And then use this query as a sub query to get all the rows that has unique SourceID;
SELECT
*
FROM
Data
WHERE
SourceID IN (
SELECT
SourceID
FROM
Data
GROUP BY
SourceID
HAVING
COUNT(SourceID) = 1
)
Use a sub-query to check if t there is an exact count of 1 of those source id's
SELECT t.* FROM YourTable t WHERE t.status = 10
AND
(SELECT COUNT(0) x From YourTable t2
where t2.sourceid = t.sourceid) = 1

Oracle SQL: How to write below SQL in Oracle

There is a table t1:
id type
1 a
1 b
2 c
2 a
3 a
3 a
4 a
Now I need to check if the id only has type a and the count is 1 (single), i.e., only id 4 satisfies this condition in the data above
SELECT type, COUNT (1)
FROM t1
where id = :id
GROUP BY type
HAVING COUNT (1) = 1;
I use the above SQL query to get the data and then use it in code. It's not a good solution, can anyone help me to get the correct result with one SQL query?
I'd group by the ID and filter on two counts:
Total count is 1
Count of rows that aren't type a (using a case statement) is 0
SELECT id
FROM t1
GROUP BY id
HAVING COUNT(*) = 1 AND COUNT(CASE WHEN type <> 'a' THEN 1 END) = 0
You want a simple aggredated query with a HAVING BY clause that ensures that only one row exists and that its type is equal to 'a'.
SELECT id
FROM t1
GROUP BY id
HAVING COUNT(*) = 1 and SUM(DECODE(type, 'a', 0, 1)) = 0
I would simply do:
SELECT id
FROM t1
GROUP BY id
HAVING COUNT(*) = 1 AND MIN(type) = 'a';
You need id in group by clause & just filter out the types with having clause :
SELECT id
FROM t1
GROUP BY id
HAVING MIN(type) = MAX(type) AND MIN(type) = 'a';

how to get duplicates when I group rows?

I have this table:
MyTable(ID, FK, ...)
I am using this query:
select ID fromMytable were FK <> 1
group by ID, FK
order by ID
This gives me the result that I want:
255
255
267
268
790
...
The 255 is duplicate because has two differnt KFs. The rest of the IDs has the same FK. I would like to get the IDs which has more than one FK and has differents values.
If an ID has two rows with FK = 2 and FK = 3 then get this ID, but if the ID has FK = 2, FK = 2, FK = 2 I don't want this ID because it has the same FK.
How could I get this IDs?
Thank you so much.
You should count distinct FKs
select ID from Mytable where FK <> 1
group by ID
having count(distinct FK) > 1
order by ID
Try this:
SELECT
ID, COUNT(*)
FROM
fromMytable
WHERE
FK <> 1
GROUP BY
ID
HAVING
COUNT(*) > 1
ORDER BY ID
Use HAVING to find only ID that exists more one once:
select DISTINCT ID
from Mytable
where FK <> 1
group by ID, FK
having count(*) >= 2
order by ID
You can use ROW_NUMBER window function.
SELECT ID FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RN, ID
from Mytable WHERE FK <> 1
) TMP
WHERE RN = 1

How to write the query that returns records where some column value appears more than once?

I have a table that has column with statuses. I need to write the query that returns records where some column value appears more than once?
Something like this:
select * from Table1
where COUNT(StatusID = 6) > 1
You can write your query like following:
SELECT *
FROM Table1
WHERE StatusID IN
(
SELECT StatusID
FROM (SELECT StatusID,Count(*) AS cnt FROM Table1 WHERE StatusID=6
GROUP BY StatusID
HAVING COUNT(*) > 1) AS tbl
)