Getting distinct result without using the DISTINCT clause - sql

I have the following code, I am able to get the result I desire by using DISTINCT but is there a way to get the same result without using DISTINCT?
SELECT S.name, COUNT(DISTINCT R.peopleID) AS numathletes
FROM Sports S
JOIN Results R ON S.ID = R.sportID
WHERE R.result >= S.record
GROUP BY S.name;
as for the tables i'm using:
Table S has ID, name, record
Table R has peopleID, competitionID, sportID, result

Yes, it is possible.
Here is one variant
SELECT name, COUNT(*) AS numathletes
FROM
(
SELECT S.name, R.peopleID
FROM
Sports S
JOIN Results R ON S.ID = R.sportID
WHERE R.result >= S.record
GROUP BY S.name, R.peopleID
) AS T
GROUP BY name;
It is a more verbose variant which shows clearly how the calculations are done.
You should try and check with your data and hardware which variant is faster.
Quite likely that they will perform the same.

Related

Group By and Inner Join Together To Get Unique Values By Maximum Date

I have a table here in which I want to write a SELECT query in SQL Server that allows me to get the following:
For each unique combination of SalesPerson x Country, get only the rows with the latest Upload_DateTime
However, I am trying to do a group-by and inner join, but to no avail. My code is something like this:
SELECT t1.[SalesPerson], t1.[Country], MAX(t1.[Upload_DateTime]) as [Upload_DateTime]
FROM [dbo].[CommentTable] AS t1
GROUP BY t1.[SalesPerson], t1.[Country]
INNER JOIN SELECT * FROM [dbo].[CommentTable] as t2 ON t1.[SalesPerson] = t2.[SalesPerson], t1.[Country] = t2.[Country]
It seems like the GROUP BY needs to be done outside of the INNER JOIN? How does that work? I get an error when I run the query and it seems my SQL is not right.
Basically, this subquery will fetch the person, the country and the latest date:
SELECT
SalesPerson, Country, MAX(uplodaed_datetime)
FROM CommentTable
GROUP BY SalesPerson, Country;
This can be used on a lot of ways (for example with JOIN or with an IN clause).
The main query will add the remaing columns to the result.
Since you tried a JOIN, here the JOIN option:
SELECT
c.id, c.SalesPerson, c.Country,
c.Comment, c.uplodaed_datetime
FROM
CommentTable AS c
INNER JOIN
(SELECT
SalesPerson, Country,
MAX(uplodaed_datetime) AS uplodaed_datetime
FROM CommentTable
GROUP BY SalesPerson, Country) AS sub
ON c.SalesPerson = sub.SalesPerson
AND c.Country = sub.Country
AND c.uplodaed_datetime = sub.uplodaed_datetime
ORDER BY c.id;
Try out: db<>fiddle

SQL DISTINCT only for one column

I am trying to get the results from this query to only give me DISTINCT results by the concatenated column fullname. The results instead give me DISTINCT by both fullname and facilityname (which is 'name'). I've looked over other solutions to similar questions but don't understand them well enough to apply them to this situation.
I've tried to play with the code, but nothing worth noting.
USE country_club;
SELECT DISTINCT
CONCAT(mem.firstname, mem.surname) AS fullname,
fac.name
FROM
Bookings AS boo
JOIN
Members AS mem ON boo.memid = mem.memid
JOIN
Facilities AS fac ON boo.facid = fac.facid
WHERE
boo.facid = 0 OR boo.facid = 1
ORDER BY
fullname ASC;
No errors on this code to note. Just need to modify the conditions to arrive at the desired outcome.
If you want one row per group of columns, use group by, not distinct:
SELECT concat(m.firstname, m.surname) as fullname,
MIN(f.name)
FROM Bookings b JOIN
Members m
ON b.memid = m.memid JOIN
Facilities f
ON b.facid = f.facid
WHERE b.facid IN (0, 1)
GROUP BY concat(m.firstname, m.surname)
ORDER BY fullname ASC;
If you're using the mysql, before running your query, run this:
SET SESSION sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY', ''));

SUM a column count from two tables

I have this simple unioned query in SQL Server 2014 where I am getting counts of rows from each table, and then trying to add a TOTAL row at the bottom that will SUM the counts from both tables. I believe the problem is the LEFT OUTER JOIN on the last union seems to be only summing the totals from the first table
SELECT A.TEST_CODE, B.DIVISION, COUNT(*)
FROM ALL_USERS B, SIGMA_TEST A
WHERE B.DOMID = A.DOMID
GROUP BY A.TEST_CODE, B.DIVISION
UNION
SELECT E.TEST_CODE, F.DIVISION, COUNT(*)
FROM BETA_TEST E, ALL_USERS F
WHERE E.DOMID = F.DOMID
GROUP BY E.TEST_CODE, F.DIVISION
UNION
SELECT 'TOTAL', '', COUNT(*)
FROM (SIGMA_TEST A LEFT OUTER JOIN BETA_TEST E ON A.DOMID
= E.DOMID )
Here is a sample of the results I am getting:
I would expect the TOTAL row to display a result of 6 (2+1+3=6)
I would like to avoid using a Common Table Expression (CTE) if possible. Thanks in advance!
Since you are counting users with matching DOMIDs in the first two statements, the final statement also needs to include the ALL_USERS table. The final statement should be:
SELECT 'TOTAL', '', COUNT(*)
FROM ALL_USERS G LEFT OUTER JOIN
SIGMA_TEST H ON G.DOMID = H.DOMID
LEFT OUTER JOIN BETA_TEST I ON I.DOMID = G.DOMID
WHERE (H.TEST_CODE IS NOT NULL OR I.TEST_CODE IS NOT NULL)
I would consider doing a UNION ALL first then COUNT:
SELECT COALESCE(TEST_CODE, 'TOTAL'),
DIVISION,
COUNT(*)
FROM (
SELECT A.TEST_CODE, B.DIVISION
FROM ALL_USERS B
INNER JOIN SIGMA_TEST A ON B.DOMID = A.DOMID
UNION ALL
SELECT E.TEST_CODE, F.DIVISION
FROM BETA_TEST E
INNER JOIN ALL_USERS F ON E.DOMID = F.DOMID ) AS T
GROUP BY GROUPING SETS ((TEST_CODE, DIVISION ), ())
Using GROUPING SETS you can easily get the total, so there is no need to add a third subquery.
Note: I assume you want just one count per (TEST_CODE, DIVISION). Otherwise you have to also group on the source table as well, as in #Gareth's answer.
I think you can achieve this with a single query. It seems your test tables have similar structures, so you can union them together and join to ALL_USERS, finally, you can use GROUPING SETS to get the total
SELECT ISNULL(T.TEST_CODE, 'TOTAL') AS TEST_CODE,
ISNULL(U.DIVISION, '') AS DIVISION,
COUNT(*)
FROM ALL_USERS AS U
INNER JOIN
( SELECT DOMID, TEST_CODE, 'SIGNMA' AS SOURCETABLE
FROM SIGMA_TEST
UNION ALL
SELECT DOMID, TEST_CODE, 'BETA' AS SOURCETABLE
FROM BETA_TEST
) AS T
ON T.DOMID = U.DOMID
GROUP BY GROUPING SETS ((T.TEST_CODE, U.DIVISION, T.SOURCETABLE), ());
As an aside, the implicit join syntax you are using was replaced over a quarter of a century ago in ANSI 92. It is not wrong, but there seems to be little reason to continue to use it, especially when you are mixing and matching with explicit outer joins and implicit inner joins. Anyone else that might read your SQL will certainly appreciate consistency.

How to make Multi Table Sub Query less wordy Access

I am trying to make a query to find all details of students interviewed more than once
I have gotten the results successfully using:
SELECT S.StudNo, S.StudLName, S.StudFName, S.StudMobile, S.City, S.DateEnrolled, S.ProgNo, S.AmountDue, S.Gender
FROM STUDENT AS S INNER JOIN STUDENT_INTERVIEW AS SI ON S.StudNo = SI.StudID
WHERE
(SELECT COUNT(SI.StudID)
FROM STUDENT_INTERVIEW)
GROUP BY S.StudNo, S.StudLName, S.StudFName, S.StudMobile, S.City, S.DateEnrolled, S.ProgNo, S.AmountDue, S.Gender
HAVING COUNT(SI.StudID) > 1;
But it seems excessively long.. I am trying to rewrite it to make it less wordy although am unable to get the correct results. I have been trying to use more sub queries to not have to use GROUP BY. When I do so I get the results for all students, not the specific 2 I'm after
SELECT *
FROM STUDENT
WHERE StudNo IN
(SELECT StudID
FROM STUDENT_INTERVIEW
WHERE
(SELECT COUNT(StudID)
FROM STUDENT_INTERVIEW
HAVING COUNT(StudID) > 1;))
Your first query is a good start. Here is an improvement:
SELECT S.*
FROM STUDENT AS S
WHERE S.StudNo IN (SELECT SI.StudId
FROM STUDENT_INTERVIEW as SI
GROUP BY SI.StudId
HAVING COUNT(*) > 1
);
The outer query needs neither the JOIN nor the GROUP BY. You can basically do all the work in the subquery.

SQL aggregate query error

I have 3 tables like this
player(id,name,age,teamid)
team(id,name,sponsor,totalplayer,totalchampion,boss,joindate)
playerdetail(id,playerid,position,number,allstar,joindate)
I want to select teaminfo include name,sponsor,totalplayer,totalchampion,boss,
the average age of the players, the number of the allstar players
I write the t-sql as below
SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T,Player P,PlayerDetail D
WHERE T.ID=P.TID AND P.ID=D.PID
but it doesn't work, the error message is
'Column 'Team.Name' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.'
Who can help me?
Thx in advance!
Add
GROUP BY
T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE
In most RDBMS (except MySQL which will guess for you), a column must be either aggregated (COUNT, AVG) or in the GROUP BY
Also, you should use explicit JOINs.
This is clearer, less ambiguous and more difficult to bollix your code
SELECT
T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE,
AVG(P.AGE) AS AverageAge,
COUNT(D.ALLSTAR) As AllStarPlayer
FROM
Team T
JOIN
Player P ON T.ID=P.TID
JOIN
PlayerDetail D ON P.ID=D.PID
GROUP BY
T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE;
Given that you want this data per team, and team.ID uniquely identifies team, I suggest the following:
SELECT max(T.NAME) As TeamName,
max(T.SPONSOR) As Sponsor,
max(T.TOTALPLAYER) As TotalPlayers,
max(T.TOTALCHAMPION) As TotalChampions,
max(T.BOSS) As Boss,
max(T.JOINDATE) As JoinDate,
AVG(P.AGE) AS AverageAge,
COUNT(D.PID) As AllStarPlayer
FROM Team T
join Player P on T.ID=P.TID
left join PlayerDetail D on P.ID=D.PID and D.ALLSTAR = 'Y'
group by T.ID
Use:
SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T
JOIN Player P ON T.ID = P.TEAMID
JOIN PlayerDetail D ON P.ID = D.PLAYERID
GROUP BY T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE