How can I output information from a different table? [duplicate] - sql-server

This question already has answers here:
How to get max number in a column?
(3 answers)
Closed 8 years ago.
I have a query that i get the MAX number of "stars"
<cfquery datasource="Intranet" name="getMaxstars">
SELECT TOP (1) WITH TIES employee, SUM(execoffice_status) AS 'total_max'
FROM CSEReduxResponses
GROUP BY employee
ORDER BY 'total_max' DESC
</cfquery >
I also have a different table EMPLOYEE. Table EMPLOYEE also comes from a different datasource="phonelist". Where in this table I have the employees first_name and last_name columns , they share the same column emp_id.
How can I output the employee first_name and last_name using the other table.
What I eventually I want to do it output:
max:
john doe - stars = 4

Use a subquery like below:
select employee_id, sum(stars) as num_stars
from table_a
group by employee_id
having sum(stars) = (select max(num_stars)
from (select employee_id, sum(stars) as num_stars
from table_a
group by employee_id) x)

SELECT TOP (1) WITH TIES employee_id, SUM(stars) AS 'total'
FROM Table_A
GROUP BY employee_id
ORDER BY 'total' DESC
This is an alternative method.

Related

retrieve Top 2 & bottom 2 employees salary in each department [duplicate]

This question already has answers here:
Select top and bottom rows
(9 answers)
Closed 7 months ago.
Employees salary table with department number
salary
deptno
15000
sales
16422
sales
18654
tech
25789
sales
12548
tech
13598
tech
WITH CTE(SALARY,DEPNO)AS
(
SELECT 15000,'SALES'UNION ALL
SELECT 16422,'SALES'UNION ALL
SELECT 18654,'TECH'UNION ALL
SELECT 25789,'SALES'UNION ALL
SELECT 12548,'TECH'UNION ALL
SELECT 13598,'TECH'UNION ALL
SELECT 1,'KUMAR'UNION ALL
SELECT 2,'KUMAR'UNION ALL
SELECT 70,'KUMAR'UNION ALL
SELECT 500,'KUMAR'UNION ALL
SELECT 1000,'KUMAR'
)
SELECT X.SALARY,X.DEPNO,
CASE
WHEN X.TOP_COL IN(1,2)THEN 'TWO MAX_SAL'
WHEN X.BOTTOM_COL IN(1,2)THEN 'TWO MIN SAL'
END AS FLAG
FROM
(
SELECT C.SALARY,C.DEPNO,
RANK()OVER(PARTITION BY C.DEPNO ORDER BY C.SALARY DESC)TOP_COL,
RANK()OVER(PARTITION BY C.DEPNO ORDER BY C.SALARY ASC)BOTTOM_COL
FROM CTE AS C
)X WHERE X.TOP_COL IN(1,2)OR X.BOTTOM_COL IN(1,2)
Your sample data is represented by CTE. I have enhanced them with additional department "KUMAR" to show two min and two max salaries

Trying to get tie results on column not included in order by

Given the following result sets, sorted in descending order on column Date.
I want to use Top clause like:
select top 4 *
from donation d
order by d.Date desc;
Which give me the following result:
Even though I limit my result with top 4, I want to be able to include the last row, because it is tie with the last record (id : 5) based on the Name.
This query first selects the names of the people in the top 4 dates (in a Common Table Expression), then uses these names to shown all their data. I guess that is what you want.
;WITH Top4Names AS
(
SELECT TOP 4 [Name]
FROM donation
ORDER BY [Date] DESC
)
SELECT d.id, d.[Name], d.Amount, d.[Date]
FROM donation d
INNER JOIN Top4Names t
ON t.[Name] = d.[Name]
ORDER BY d.[Date] DESC;

Is there a way to count rows in SQL Server that only count rows with identical information once?

For example let's say this is my data:
PK FNAME LNAME
------------------------
2 Fred SEG
3 Fred SEG
4 Dave Smith
I want to get a result of one Fred and one Dave.
Do you want count(distinct)?
select count(distinct fname)
from t;
If you actually want the rows, with names not repeated, then use:
select t.*
from t
where t.pk = (select min(t2.pk) from t t2 where t2.fname = t.fname);
you just need to add distinct keyword.
select distinct fname, lname from table;
The SQL COUNT function is an aggregate function that returns the number of rows returned by a query. You can use the COUNT function in the SELECT statement to get the number of employees, the number of employees in each department, the number of employees who hold a specific job, etc.
COUNT(*)
The COUNT(*) function returns the number of rows in a table including the rows that contain the NULL values.
Table employees:
Employee_Id
First_Name
Last_Name
Email
Phone_Number
Salary
Department_id
SELECT COUNT(*) FROM employees;

Select all columns, group by one column with other requirements

This is not the original table, I create the similar situation so that I can explain the problem better.
Let's say I have a table called [student], with 4 columns: [name], [gender], [age], [country].
How to do a 'SELECT *' query that returns the rows that meet this requirements:
student must be male
only one student from each country
if there are more than one students from a country, choose the oldest one
I tried using GROUP BY on [country] but keep getting error "Column '...' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
One possible approach:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Country ORDER BY Age DESC) RN
FROM students
WHERE gender = 'male') T
WHERE RN = 1;
The subquery selects only male students, and assigns them a row number based on their age, partitioned by country.

SELECT from multiple queries

I have this tables:
tblDiving(
diving_number int primary key
diving_club int
date_of_diving date)
tblDivingClub(
number int primary key not null check (number>0),
name char(30),
country char(30))
tblWorks_for(
diver_number int
club_number int
end_working_date date)
tblCountry(
name char(30) not null primary key)
I need to write a query to return a name of a country and the number of "Super club" in it.
a Super club is a club which have more than 25 working divers (tblWorks_for.end_working_date is null) or had more than 100 diving's in it(tblDiving) in the last year.
after I get the country and number of super club, I need to show only the country's that contains more than 2 super club.
I wrote this 2 queries:
select tblDivingClub.name,count(distinct tblWorks_for.diver_number) as number_of_guids
from tblWorks_for
inner join tblDivingClub on tblDivingClub.number = tblWorks_for.club_number,tblDiving
where tblWorks_for.end_working_date is null
group by tblDivingClub.name
select tblDivingClub.name, count(distinct tblDiving.diving_number) as number_of_divings
from tblDivingClub
inner join tblDiving on tblDivingClub.number = tblDiving.diving_club
WHERE tblDiving.date_of_diving <= DATEADD(year,-1, GETDATE())
group by tblDivingClub.name
But I don't know how do I continue.
Every query works separately, but how do I combine them and select from them?
It's university assignment and I'm not allowed to use views or temporary tables.
It's my first program so I'm not really sure what I'm doing:)
WITH CTE AS (
select tblDivingClub.name,count(distinct tblWorks_for.diver_number) as diving_number
from tblWorks_for
inner join tblDivingClub on tblDivingClub.number = tblWorks_for.club_number,tblDiving
where tblWorks_for.end_working_date is null
group by tblDivingClub.name
UNION ALL
select tblDivingClub.name, count(distinct tblDiving.diving_number) as diving_number
from tblDivingClub
inner join tblDiving on tblDivingClub.number = tblDiving.diving_club
WHERE tblDiving.date_of_diving <= DATEADD(year,-1, GETDATE())
group by tblDivingClub.name
)
SELECT * FROM CTE
You can combine the queries using a UNION ALL as long as there are the same number of columns in each query. You can then roll them into a Common Table Expression (CTE) and do a select from that.

Resources