I have two tables
select * from elig
select * from elig1
in the same database.
Table 1
Firstname, Lastname, MemberNo
Table 2
Firstname, Lastname, MemeberNo
Table 1:
ash dassant 1
alex peters 2
john lex 3
kyle passant 4
file leters 5
zed luthr 6
Table 2:
a dassant 1
a peters 2
j lex 3
When I try to select the records from table 1 I get complete firstname and lastname
But when I try to select records from table 2 I get only the first letter of first name and complete lastname
Any help here would be appreciated if I can get the full first name for second table
Related
I have the following use-case:
Table Employee has 3 columns-
EmpID, OfficeMobile,HomeMobile.
Some employees have given same number for both places. Others have given different.
Output should be 2 columns EmpID & ContactNo
If same numbers for both places 1 row else 2 rows.
EmpID
OfficeMobile
HomeMobile
1
123
123
2
456
789
Expected Output:
EmpID
Contact number
1
123
2
456
2
789
I could come up with this query which can compare the two contact numbers but I'm not finding a way to generate two rows when the home and mobile numbers are different.
select ed.empId,
case
when ed.home_number = ed.mobile_number then ed.home_number
else (ed.home_number )
end as contact
from employeeDetails as ed;
You can use UNION ALL
SELECT ed.empId, ed.home_number as contact
FROM employeeDetails ed
UNION ALL
SELECT ed.empId, ed.mobile_number as contact
FROM employeeDetails ed
WHERE ed.home_number <> ed.mobile_number
I am trying to find a query that will select from two tables but not join those two tables as I don't want the result on one line.
Lets say I have these tables:
Persons1 Persons2
Number Name Surname Number Name Surname
----------------------- -------------------------
1 Peter Miller 1 Frank Farian
2 Hans Geige 2 Thomas Müller
And when I use this query:
SELECT
NEW.NAME,
OLD.NAME
FROM
PERSONS1 NEW,
PERSONS2 OLD
WHERE
NEW.Number = 1 AND
OLD.Number = 1
I get the following result:
Peter Frank
How do I write the query to get:
Peter
Frank
Use UNION for this:
SELECT NEW.NAME
FROM PERSONS1 NEW
WHERE NEW.Number = 1
UNION
SELECT OLD.NAME
FROM PERSONS2 OLD
WHERE OLD.Number = 1
I have table where have over 100k information.
ID FirstName
1 Bob
2 Bob
3 Tom
4 John
5 John
6 John
.. ....
Want procedure which will be count how much names are same, For example it must be like :
FirstName Count
Bob 2
Tom 1
John 3
Please help me to write it
It's very basic SQL example, group by column + aggregating results
select
FirstName, count(*)
from Table1
group by FirstName
Try this
select FirstName,Count(FirstName) From TableA group by FirstName
Try this
SELECT FirstName, COUNT(*) As Count
FROM YourTable
GROUP BY FirstName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Create Procedure GetCount
as
BEGIN
Select FirstName,Count(*) from tablename group by FirstName
END
I have the following tables:
Students
StudentID UserID FirstName LastName
1 1 John Doe
2 3 Peter Pan
3 5 Mark Twain
Employees
EmployeeID UserID FirstName LastName
1 2 Juan Carlos
2 4 Joe Reyes
3 6 Ash Ketchum
Users
UserID Username
1 test1
2 test2
3 test3
4 test4
5 test5
6 test6
Messages
MessageID MessageFrom MessageTo Subject
1 1 2 test message
2 4 3 another test message
3 3 5 another test message
I want to display the output like this
MessageID MessageFrom MessageTo Subject
1 John Doe Juan Carlos test message
2 Joe Reyes Peter Pan another test message
3 Peter Pan Mark Twain another test message
Where MessageFrom and MessageTo returns a foreign key value from UserID located from the Users table. Students can send messages to the employees, and vice-versa. They can also send between students and employees.
I think this is The X Y Problem again.
Why did you split your Users in two parts ?
Why not making it like this (which would solve the whole problem) :
UserID | FirstName | LastName | UserName | Type
-------------------------------------------------------
1 John Doe test1 1
2 Juan Carlos test2 2
3 Mark Twain test3 1
And add a table Types
TypeId | TypeName
1 Students
2 Employees
This way you have 2 Tables for users instead of 3.
All users and names are in the same table as it should be.
This should do the trick, since you don't care if the message is related to a student or employee you can just union those 2 tables.
SELECT
MessageID,
(FromTable.FirstName + ' ' + FromTable.LastName) AS MessageFrom,
(ToTable.FristName + ' ' + ToTable.LastName) AS MessageTo,
Subject
FROM
Messages
INNER JOIN
(
(SELECT UserID, FirstName, LastName FROM Students)
UNION
(SELECT UserID, FirstName, LastName FROM Employees)
) AS FromTable ON FromTable.UserID = Messages.MessageFrom
INNER JOIN
(
(SELECT UserID, FirstName, LastName FROM Students)
UNION
(SELECT UserID, FirstName, LastName FROM Employees)
) AS ToTable ON ToTable.UserID = Messages.MessageTo
I have got a table named student. I have written this query:
select * From student where sname in ('rajesh','rohit','rajesh')
In the above query it's returning me two records; one matching 'rajesh' and another matching: 'rohit'.
But i want there to be 3 records: 2 for 'rajesh' and 1 for 'rohit'.
Please provide me some solution or tell me where i am missing.
NOTE: the count of result of sub query is not fix there can be many words there some distinct and some multiple occurrence .
Thanks
Your requirements are not clear, and I'll try to explain why.
Let's define table students
ID FirstName LastName
1 John Smith
2 Mike Smith
3 Ben Bray
4 John Bray
5 John Smith
6 Bill Lynch
7 Bill Smith
Query with WHERE clause:
FirstName in ('Mike', 'Ben', 'Mike')
will return 2 rows only, because it could be rewritten as:
FirstName='Mike' or FirstName='Ben' or FirstName='Mike'
WHERE is filtering clause that just says if existing row satisfy given conditions or not (for each of rows created by FROM clause.
Let's say we have subquery that returns any number of non distinct FirstNames
In case if SQ contains 'Mike', 'Ben', 'Mike' using inner join you can get those 3 rows without problem
Select ST.* from Students ST
Inner Join (Select name from …. <your subquery>) SQ
On ST.FirstName=SQ.name
Result will be:
ID FirstName LastName
2 Mike Smith
2 Mike Smith
3 Ben Bray
Note data are not ordered by order of names returning by SQ. If you want that, SQ should return some ordering number, eg.:
Ord Name
1. Mike
2. Ben
3. Mike
In that case query should be:
Select ST.* from Students ST
Inner Join (Select ord, name from …. <your subquery>) SQ
On ST.FirstName=SQ.name
Order By SQ.ord
And result:
ID FirstName LastName
2 Mike Smith (1)
3 Ben Bray (2)
2 Mike Smith (3)
Now, let's se what will happen if subquery returns
Ord Name
1. Mike
2. Bill
3. Mike
You will end up with
ID FirstName LastName
2 Mike Smith (1)
6 Bill Lynch (2)
7 Bill Smith (2)
2 Mike Smith (3)
Even worse, if you have something like:
Ord Name
1. John
2. Bill
3. John
Result is:
ID FirstName LastName
1 John Smith (1)
4 John Bray (1)
5 John Smith (1)
6 Bill Lynch (2)
7 Bill Smith (2)
1 John Smith (3)
4 John Bray (3)
5 John Smith (3)
This is an complex situation, and you have to clarify precisely what requirement is.
If you need only one student with the same name, for each of rows in SQ, you can use something like SQL 2005+):
;With st1 as
(
Select Row_Number() over (Partition by SQ.ord Order By ID) as rowNum,
ST.ID,
ST.FirstName,
ST.LastName,
SQ.ord
from Students ST
Inner Join (Select ord, name from …. <your subquery>) SQ
On ST.FirstName=SQ.name
)
Select ID, FirstName, LastName
From st1
Where rowNum=1 -- that was missing row, added later
Order By ord
It will return (for SQ values John, Bill, John)
ID FirstName LastName
1 John Smith (1)
6 Bill Lynch (2)
1 John Smith (3)
Note, numbers (1),(2),(3) are shown to display value of ord although they are not returned by query.
If you can split the where clause in your calling code, you could perform a UNION ALL on each clause.
SELECT * FROM Student WHERE sname = 'rajesh'
UNION ALL SELECT * FROM Student WHERE sname = 'rohit'
UNION ALL SELECT * FROM Student WHERE sname = 'rajesh'
Try using a JOIN:
SELECT ...
FROM Student s
INNER JOIN (
SELECT 'rajesh' AS sname
UNION ALL
SELECT 'rohit'
UNION ALL
SELECT 'rajesh') t ON s.sname = t.sname
just because you've got a criteria in there two times doesn't mean that it will return 1 result per criteria. SQL engines usually just use the unique criteria - thus, from your example, there will be 2 criteria in IN clause: 'rajesh','rohit'
WHY do you need to return 2 results? are there two rajesh in your table? they should BOTH return then. You don't need to ask for rajesh twice for that to happen. What does your data look like? What do you want to see returned?
Hi i am query just as you give above and it give me all data that matches in the condition of in clause. just like your post
select * from person
where personid in (
'Carson','Kim','Carson'
)
order by FirstName
and its give me all records which fulfill this Criteria