Can anyone help a newbie to SQL out please with this situation.
Given a membership database where there can be more than one member per membership account, is it possible to return all records from the membership table where no member (in the membername table) is over a specified age.
The tables look like this:
membership.id
membership.membershipnumber
membername.id
membername.membershipnumber
membername.name
membername.age
Many thanks
There are probably faster methods but this is a straightforward way of doing it.
Select membershipnumber
From membership
Where membershipnumber
Not In (
Select membershipnumber
From membername
Where age > #pAge
)
Select distinct m.membershipnumber
from membership s
inner join (select membershipnumber from membername where age > 18) aux on aux.membershipnumber = m.membershipnumber
You can replace 18 by a #variable.
Related
I was trying to build one query in SQL but was not being able to and need someone's help, the requirement is as below,
Extract only the accounts where there is no after_image = '0199d' present or if its presents the audit_Date should be < 20170701. Also unique accounts should be extracted. Please suggest
select acct, max(audit_date)from yourtable a
where not exists (select 1 from yourtable b where a.acct=b.acct and b.after_image = '0199d'
and b.audit_Date >= '20170701')
group by acct
I have got two tables T1 and T2 . T1 is present in database D2 and T2 is present in database D2. I am using DB2 database. I also have two queries for fetching data from T1 and T2 respectively.
Now, I have a requirement where I need to create a view and get the combined data of two queries in that view.
For example, create view testview as select employeeName, employeeGender from sampledb.employee where employeeID >6 UNION select studentName, studentGender from testdb2.student where studentId > 6.
I want to do something like this.
I have searched a lot on internet for creating a view on different schemas in DB2, but couldn't get a right solution.
Please help.
If you speak schema and not database, this doesn't work?
create view t1.viewname (name, gender) as
select employeeName, employeeGender
from S1.employee
where employeeID >6
UNION
select studentName, studentGender
from S2.student
where studentId > 6
I have a Users table with following schema,
UserId, Name, ManagerId
This ManagerId, is nothing but a Userid as a manager is also a user.
I can get list of managers using below query but how do i get managerId along with name ?
Select DISTINCT(ManagerId) from Users
group by ManagerId
I want following output
ManagerId, Name
Try this:
SELECT UserId, Name
FROM Permission
WHERE UserId IN (Select DISTINCT ManagerId FROM Permission)
Also possible with a JOIN:
SELECT UserId, Name
FROM Permission AS p1
JOIN (Select DISTINCT ManagerId FROM Permission) AS p2
ON p1.UserId = p2.MAnagerId
I believe you want to retrieve users who are managers. This could be done using EXISTS clause:
SELECT DISTINCT UserId, Name
FROM Permission p1
WHERE EXISTS (
SELECT 1
FROM Permission p2
WHERE p1.UserId = p2.ManagerId
)
DISTINCT clause is optional and may be extraneous depending on your data. If this is the case, remove it for performance boost (distinct requires the input to be sorted and then duplicates are removed).
I'm not sure if it's a good idea to use IN together with DISTINCT like Giorgos suggested. SQL Server has a good optimization engine, but I've had bad experience with such statements.
Better use EXISTS
SELECT P1.UserId, P1.Name
FROM Permission AS P1
WHERE EXISTS (
SELECT *
FROM Permission AS P2
WHERE P2.ManagerId = P1.UserId
);
Keep in mind that it would be a good idea to have indexes on Permission table where first indexed column will be UserId and in another index ManagerId. This way EXISTS will perform very efficiently.
I've been using this query statement ever since. I wonder why this does not work on SQL Server 2008 R2.
SELECT
UserName
FROM
Users
WHERE
UserName NOT IN (SELECT UserName FROM UserTableT2)
The codes does not return any data. Goal is select all UserName in Users table which do not belong to UserTableT2.
EDIT:
Here's the actual query
Update using #Tim Schelmter's query:
Update :
Update:
Thank you!
I would use NOT EXISTS:
SELECT u.UserName
FROM Users u
WHERE NOT EXISTS
(
SELECT 1 FROM UserTableT2 ut2
WHERE u.UserName = ut2.UserName
)
Why? Because it works also if there are NULL values in UserTableT2.UserName.
Worth reading:
Instead of NOT IN, use a correlated NOT EXISTS for this query pattern.
Always. Other methods may rival it in terms of performance, when all
other variables are the same, but all of the other methods introduce
either performance problems or other challenges.
With your updated columns and tables:
SELECT u.usr_id
FROM ousr u
WHERE NOT EXISTS
(
SELECT 1 FROM ApprovalStageApprovers asa
WHERE u.usr_id = asa.ApprovalUser
)
I've seen a few other questions on Stackoverflow that discuss sub-selects, but they usually relate to the use of multiple tables. In most cases, a proper join could serve the same purpose.
However my query below refers to a single table. How would I write this using DBIX::Class?
select ID, username, email, role
from Employees
where (ID in
(select max(ID)
from Employees
where username = 'jsmith'
))
order by ID DESC
Thanks!
--
Edit 1: SQL code fix
The Cookbook has almost the exact same query as example.
Your SQL query doesn't make sense to me because the subquery returns a single id, so WHERE id = () would make more sense.
What are you trying to accomplish with it?