select count(d.Games_played),count(d.No_ofgames) from
(
SELECT COUNT(UserGamePlayed.intID) AS 'Games_played',games.vchCompetency,b.No_Games as 'No_ofgames'
FROM UserGamePlayed
inner join games on games.intGameID=UserGamePlayed.intGameID
inner join
(
select COUNT(Games.intGameID) AS 'No_Games',vchCompetency,intGradeID from Games
WHERE intGradeID=3
GROUP BY vchCompetency,intGradeID
) as b on b.vchCompetency=games.vchCompetency
WHERE intUserID=403 and UserGamePlayed.intGradeID=3
GROUP BY games.vchCompetency,b.No_Games
)as d
the table which i get from d is:
As per the table d i want to get a count of played,when exicute a full i am getting
You should replace COUNT with SUM (in your outer select only).
COUNT only counts (as the name indicates ;)) the rows while SUM will add up the values that are passed to it.
Related
I am working on two different tables. I want to join some of the information there and select the highest value in a particular column.
here is the code I am trying to use though not working
SELECT
result.matric_number, max(result.331), student.last_name, student.first_name, student.other_name
from result
INNER join student on result.result_id=student.StudentID
AND
student.Session = '2020/2021'
You have to use group by,If you are using aggregate function like max in the select statement.
Below query returns max(result) for the given group,result.matric_number, student.last_name, student.first_name, student.other_name
SELECT result.matric_number, max(result.331), student.last_name, student.first_name, student.other_name
from result INNER join student
on result.result_id=student.StudentID AND student.Session = '2020/2021'
group by result.matric_number, student.last_name, student.first_name, student.other_name
If you want max of all the records then remove all columns in select execpt max
SELECT max(result.331)
from result INNER join student
on result.result_id=student.StudentID AND student.Session = '2020/2021'
I'm working with two tables. I have a full list of groups in table A, and a list of each group member that has been reviewed in table B. So table B is a log of all review records for those members for each group.
select a.Group_Name, Max(b.Request_Review_Date)
From GroupTable a
Left Outer Join GroupReviews b ON a.Group_Name = b.Group_Name
Group By a.Group_Name
What I am trying to return is the full list of groups from table A, and find the latest review date from table B for each of those groups.
I have researched and tried all or most of the inner & outer joins, apply methods....but its just not giving me the results. Can anyone point me in the right direction? Or am I having to bring back two result sets and compare in my ASP code-behind?
Try a CTE then join back to it
WITH Recent AS
(
select group_name, max(Request_Review_Date) AS 'MaxReviewDate'
from GroupReviews
group by group_name
)
select a.group_name, MaxReviewDate
from GroupTable a left join Recent
on group_name = a.group_name
if you need the value for max for all the a.group name rows the ypu should join the subquery for max date
select a.Group_Name, t.max_date
left join (
select b.Group_Name, Max(b.Request_Review_Date) max_date
from GroupReviews b
Group By b.Group_Name
) t on t.Group_Name = a.Group_Name
SQL Fiddle
I'm trying to force through zero values for grades that don't exist in my Results table, but do exist in my list of possible Grades table.
I've managed to join tables successfully in order to almost achieve this using this previous post as guidance up to a point.
As you can see from my fiddle the resultset is displaying NULL values for my Year and Subject columns and I would like these to display the relevant subject.
Don't use * in SELECT instead use specify the colums you need and use ISNULL and make it as zero like:
SELECT ISNULL(t.amount,0)
FROM [yourtable] y
left join [someOtherTable] t
ON y.id=t.id
What you need is a table of Subjects and Years, then cross join for grades...
create table Subjects(SubjectID INT, Subject Varchar(50));
create table YearSub (SubjectID INT, Year INT, CrateDate Date); -- Map your available subjects for each year in here
Once you have thiese, make a CTE to hold the full list of available subjects, years and grades
with AllGrades as
(select SubjectID, Year, Grade
from YearSub YS
cross join Grades
)
select ...
from AllGrades
left join ...
And so on
i think this is what you wanted.
first you get all combinition of student & grades by using CROSS JOIN
then you LEFT JOIN to the resutl table to the get the count
select s.year, subject grade, grade, count(wag) as Total
from student s
cross join grades g
left join results r on s.upn = r.upn and s.upn = r.upn
group by s.year, g.grade
I've been banging my head against the wall for something that I think should be simple but just cant get to work.
I'm trying to retrieve the row with the highest multi_flag value when I join table A and table B but I can't seem to get the SQL right because it returns all the rows rather than the one with the highest multi_flag value.
Here are my tables...
Table A
Table B
This is almost my desired result but only if I leave out the value_id row
SELECT CATALOG, VENDOR_CODE, INVLINK, NAME_ID, MAX(multi_flag) AS multiflag
FROM TBLINVENT_ATTRIBUTE AS A
INNER JOIN TBLATTRIBUTE_VALUE AS B
ON A.VALUE_ID = B.VALUE_ID
GROUP BY CATALOG, VENDOR_CODE, INVLINK, NAME_ID
ORDER BY CATALOG DESC
This is close to what I want to retreive but not quite notice how it returns unique name_id and the highest multi_flag but I also need the value_id that belongs to such multi_flag / name_id grouping...
If I include the value_id in my SQL statement then it returns all rows and is no longer grouped
Notic ein the results below how it no longer returns the row for the highest multi_flag and how all the different values for name_id (Ex. name_id 1) are also returned
You can choose to use a sub-query, derived table or CTE to solve this problem. Performance will be depending on the amount of data you are querying. To achieve your goal of getting the max multiflag you must first get the max value based on the grouping you want to achieve this you can use a CTE or sub query. The below CTE will give the max multi_flag by value that you can use to get the max multi_flag and then you can use that to join back to your other tables. I have three joins in this example but this can be reduce and as far a performance it may be better to use a subquery but you want know until you get the se the actual execution plans side by side.
;with highest_multi_flag as
(
select value_id, max(multi_flag) AS multiflag
FROM TBLINVENT_ATTRIBUTE
group by value_id
)
select A.CATALOG, a.VENDOR_CODE, a.INVLINK, b.NAME_ID,m.multiflag
from highest_multi_flag m
inner join TBLINVENT_ATTRIBUTE AS A on a.VALUE_ID =b. m.VALUE_ID
INNER JOIN TBLATTRIBUTE_VALUE AS B ON m.VALUE_ID = B.VALUE
You can use Lateral too, its an other solution
SELECT
A.CATALOG, A.VENDOR_CODE, A.INVLINK, B.NAME_ID, M.maxmultiflag
FROM TBLINVENT_ATTRIBUTE AS A
inner join lateral
(
select max(B.multi_flag) as maxmultiflag from TBLINVENT_ATTRIBUTE C
where A.VALUE_ID = C.VALUE_ID
) M on 1=1
INNER JOIN TBLATTRIBUTE_VALUE AS B ON M.maxmultiflag = B.VALUE
table a.
Table b . I have two tables. Table A has over 8000+ records and continues to grow with time.
Table B has only 5 or so records and grows rarely but does grow sometimes.
I want to query Table A's last records where the Id for Table A matches for Table B. The problem is; I am getting all the rows from Table A. I just need the ones where Table A and B match once. These are unique Id's when a new row is inserted into table B and never get repeated.
Any help is most appreciated.
SELECT a.nshift,
a.loeeworkcellid,
b.loeeconfigworkcellid,
b.loeescheduleid,
b.sdescription,
b.sshortname
FROM oeeworkcell a
INNER JOIN dbo.oeeconfigworkcell b
ON a.loeeconfigworkcellid = b.loeeconfigworkcellid
ORDER BY a.loeeworkcellid DESC
I am assuming you want to get the only the lastest (as you said) row from the TableA but JOIN giving you all the rows.You can use the Row_Number() to get the rownumber and then apply the join and filter it with the Where clause to select only the first row from the JOIN. So what you can try as below,
;WITH CTE
AS
(
SELECT * , ROW_NUMBER() OVER(PARTITION BY loeeconfigworkcellid ORDER BY loeeworkcellid desc) AS Rn
FROM oeeworkcell
)
SELECT a.nshift,
a.loeeworkcellid,
b.loeecoonfigworkcellid,
b.loeescheduleid,
b.sdescription,
b.sshortname
FROM CTE a
INNER JOIN dbo.oeeconfigworkcell b
ON a.loeeconfigworkcellid = b.loeeconfigworkcellid
WHERE
a.Rn = 1
You need to group by your data and select only the data having the condition with min id.
SELECT a.nshift,
a.loeeworkcellid,
b.loeecoonfigworkcellid,
b.loeescheduleid,
b.sdescription,
b.sshortname
FROM oeeworkcell a
INNER JOIN dbo.oeeconfigworkcell b
ON a.loeeconfigworkcellid = b.loeeconfigworkcellid
group by
a.nshift,
a.loeeworkcellid,
b.loeecoonfigworkcellid,
b.loeescheduleid,
b.sdescription,
b.sshortname
having a.loeeworkcellid = min(a.loeeworkcellid)