Select multiple row where one of their columns is the same - sql

i wanna select a row where in another row with same userid is Active . like :
mytable
userid
userfield
fieldstatus
1
Name
Tom
1
account
Active
2
name
Jerry
2
account
Failed
I want to select tom in name user field where its account is active . I'm writing in python

You can use a subquery with exists:
select t.fieldstatus from mytable t where t.userfield = "name" and exists (select 1 from mytable t1 where t1.userid = t.userid and t1.userfield = "account" and t1.fieldstatus = "Active")
Output
fieldstatus
Tom

You can check the same userid value using exists
select *
from mytable t
where exists (
select * from mytable t2
where t2.userid=t.userid
and t2.userfield='account'
and t2.fieldstatus='Active'
)

Related

How to merge users in PostgreSQL

I need to make something to merge some users in PGSQL but I think that pgsql don't own the MERGE property. I just want to know how to make two users to be matched like this :
id | name | username | mail
1 | toto | tata | toto.tata#gmail.com
2 | titi | tutu | titi.tutu#gmail.com
Here I want to chose which data I would like I want to say that I want to merge only username from 2 to 1 so the result would be :
id | name | username | mail
1 | toto | tutu | toto.tata#gmail.com
You just need to select all the columns for first id and the column you need with second id will be a subquery in select list. Please check below answer for selecting merged result.
Schema and insert statements:
create table users (id int , name varchar(50), username varchar(50), mail varchar(50));
insert into users values (1 , 'toto' , 'tata' , 'toto.tata#gmail.com');
insert into users values (2 , 'titi' , 'tutu' , 'titi.tutu#gmail.com');
Query:
select id,name,(select username from users where id=2) username,mail from users where id=1
Output:
id
name
username
mail
1
toto
tutu
toto.tata#gmail.com
db<fiddle here
To merge the rows within the table you can first update first row with data from second row then delete the second row. Try this:
Schema and insert statements:
create table users (id int , name varchar(50), username varchar(50), mail varchar(50));
insert into users values (1 , 'toto' , 'tata' , 'toto.tata#gmail.com');
insert into users values (2 , 'titi' , 'tutu' , 'titi.tutu#gmail.com');
Update query:
update users set username=(select username from users where id=2) where id=1;
delete from users where id=2;
Select query:
select * from users
id
name
username
mail
1
toto
tutu
toto.tata#gmail.com
db<fiddle here
You could use aggregation:
select min(id) as id,
max(name) filter (where id = 1) as name,
max(username) filter (where id = 2) as username,
max(mail) filter (where id = 1) as mail
from t
where id in (1, 2);
This assumes that you want to pull particular column values from particular ids.
Or you could use join:
select t1.id, t1.name, t2.username, t1.mail
from t t1 join
t t2
on t1.id = 1 and t2.id = 2;
If you actually want to change the data, use update and delete:
update t t1
set username = t2.username
from t t2
where t1.id = 1 and t2.id = 2;
delete from t
where t.id = 2;
Here is a db<>fiddle.

SELECT DISTINCT Users in Table 1 which don't exist in Table 2

I have a table with 4 columns of user information. Each table has the following columns:
Username | Full_Name | Job_Name | Current_Job_Allowed
Table 1 includes all users and the Job_Name which they have permissions to view. This means that there are multiple lines of the same username in Table 1 with different Job_Name values.
Table 2 contains a list of all possible users.
Username |Full_Name
--------------+-----------------
amunoz |Andrew Munoz
csmith |Carl Smith
cwatkins |Cat Watkins
ggriffiths |Garmin Griffiths
jcarr |Jason Carr
jhothi |Jark Hothi
jphillips |Jim Phillips
lbradfield |Lisa Bradfield
ntaylor |Noria Taylor
rfelipe |Ralf Felipe
Query 1 contains all users specified by a query parameter which I specify, i.e. 'KML_20160531'.
I would like to now select a DISTINCT list of all users which have a different Job_Name from the parameter I specify for Job_Name. For example Table 1 contains:
Username|Full_Name |Job_Name |Current_Job_Allowed
--------+------------+------------+----------------------
amunoz |Andrew Munoz|KML_20160531|1
jcarr |Jason Carr |KML_20160531|1
rfelipe |Ralf Felipe |KML_20140531|1
amunoz |Andrew Munoz|KML_20160431|1
I would then like to return the below when I enter 20160531 for Job_Name. This will return all possible new users for the Job_Name value I entered.
Username |Full_Name
--------------+---------------
csmith |Carl Smith
cwatkins |Cat Watkins
ggriffiths |Garmin Griffiths
jhothi |Jark Hothi
jphillips |Jim Phillips
lbradfield |Lisa Bradfield
ntaylor |Noria Taylor
rfelipe |Ralf Felipe
This parameter query will show you which users have a Table 1 row with Job_Name matching the parameter value:
PARAMETERS which_job Text ( 255 );
SELECT t1.[Username], t1.Job_Name
FROM [Table 1] AS t1
WHERE t1.Job_Name=[which_job];
So you can use that as a subquery, left join Table 2 to the subquery, and select the rows where the "right side" is Null:
PARAMETERS which_job Text ( 255 );
SELECT t2.[Username]
FROM
[Table 2] AS t2
LEFT JOIN
(
SELECT t1.[Username]
FROM [Table 1] AS t1
WHERE t1.Job_Name=[which_job]
) AS sub
ON t2.[Username] = sub.[Username]
WHERE sub.[Username] Is Null;
Assuming that query returns the correct rows, add the other field you want to see to SELECT t2.[Username].
You should not need DISTINCT unless Table 2 allows duplicate Username values, or Table 1 allows more than one row with the same combination of Username and Job_Name.
You can use not in and distinct
select distinct a.username, a.fullname
from table1 as a
where a.username not in (select distinct username
from table2 where job_name ='my_value');
and for only the job
select distinct a.username, a.fullname
from table1 as a
where a.username not in (select username
from table2
group by username
having count(job_name) = 1
and job_name ='my_value' );
You don't actually need DISTINCT for this query at all:
select t.*
from table1 as t
where not exists (select 1
from table2 as t2
where t2.username = t.username and t2.job_name = "KML_20160531"
);
From your description of the problem, Current_Job_Allowed does not seem relevant.
This will be syntax for your problem
SELECT DISTINCT UserName
FROM Table1
WHERE UserName NOT IN (
SELECT UserName From Table2
WHERE job_name = ''
);

Sqlite query - How I can select a predecessor?

I have this table below:
ID name Last
0 Joe Doe
1 Hut Nob
2 Lis Hug
3 Edy mur
I use this query to select an ID:
SELECT name FROM myDatabase WHERE ID = 2
In this case the query returns me the string Lis, now, How I can select the predecessor value?
Simple, the predecessor from 2 is 1, so I need only to do WHERE ID < 2 or WHERE ID = 2 - 1.
But this method have a problem! Lets suppose that I delete that row (ID = 1), the query will return null, because that ID not exists.
So, in this example, how I can select the predecessor from ID 2 and return ID 0? (ID 1 is gone)
You can use subquery to find max ID that is lower than ID that you provided:
SELECT *
FROM mytable m
WHERE m.id = (SELECT MAX(m2.id)
FROM mytable m2
WHERE m2.ID < 2);
SqlFiddleDemo
Get all the smaller IDs, and from those, take only the largest one:
SELECT name
FROM MyTable
WHERE ID < 2
ORDER BY ID DESC
LIMIT 1;

How to GROUP multiple records from two different SQL statements

I have a table called tbl which contains all the data I need. I have many columns in this table, but for purposes of this example I will only list the columns necessary to accomplish this task.
Here's how data stored in the tbl (note uID is char(20) and cID is int)*:
uID cID
1 10
1 11
1 12
2 11
We usually query this table like
SELECT * FROM tbl WHERE uID = "1"
So it returns
uID cID
1 10
1 11
1 12
But I also need to return the row where uID is different but cID do match. Or grab the uID of the second row (which is 2) based on cID and do a select statement like this:
SELECT * FROM tbl WHERE uID in ('1','2')
That query will return what I'm looking for
uID cID
1 10
1 11
1 12
2 11
This table contains a lot of rows and I want to be able to do this programatically for every call where cID matches and uID is different.
Any suggestions?
I think this may be what you want:
SELECT *
FROM tbl
WHERE uID = '1'
UNION ALL
SELECT *
FROM tbl
WHERE uID <> '1' AND
EXISTS (select 1 from tbl tbl2 where tbl2.uId = '1' and tbl2.cID = tbl.cID);
or something like this:
SELECT uID, cID
FROM tbl
WHERE uID IN
(
SELECT uID
FROM tbl
INNER JOIN
(
SELECT cID
FROM tbl
GROUP BY cID
HAVING count(*) > 1
) c ON c.cID = tbl.cID
)

SQL how to find rows which have highest value of specific column

For example, the table has columns MYINDEX and NAME.
MYINDEX | NAME
=================
1 | BOB
2 | BOB
3 | CHARLES
Ho do I find row with highest MYINDEX for specific NAME? E.g. I want to find ROW-2 for name "BOB".
SELECT Max(MYINDEX) FROM table WHERE NAME = [insertNameHere]
EDIT: to get the whole row:
Select * //never do this really
From Table
Where MYINDEX = (Select Max(MYINDEX) From Table Where Name = [InsertNameHere]
There are several ways to tackle this one. I'm assuming that there may be other columns that you want from the row, otherwise as others have said, simply name, MAX(my_index) ... GROUP BY name will work. Here are a couple of examples:
SELECT
MT.name,
MT.my_index
FROM
(
SELECT
name,
MAX(my_index) AS max_my_index
FROM
My_Table
GROUP BY
name
) SQ
INNER JOIN My_Table MT ON
MT.name = SQ.name AND
MT.my_index = SQ.max_my_index
Another possible solution:
SELECT
MT1.name,
MT1.my_index
FROM
My_Table MT1
WHERE
NOT EXISTS
(
SELECT *
FROM
My_Table MT2
WHERE
MT2.name = MT1.name AND
MT2.my_index > MT1.my_index
)
SELECT MAX(MYINDEX) FROM table
WHERE NAME = 'BOB'
For the whole row, do:
SELECT * FROM table
WHERE NAME = 'BOB'
AND MyIndex = (SELECT Max(MYINDEX) from table WHERE NAME = 'BOB')
If you wanted to see the highest index for name = 'Bob', use:
SELECT MAX(MYINDEX) AS [MaxIndex]
FROM myTable
WHERE Name = 'Bob'
If you want to skip the inner join, you could do:
SELECT * FROM table WHERE NAME = 'BOB' ORDER BY MYINDEX DESC LIMIT 1;
Use
FROM TABLE SELECT MAX(MYINDEX), NAME GROUP BY NAME