Problem with GROUP BY statement (SQL) - sql

I have a table GAMES with this information:
Id_Game Id_Player1 Id_Player2 Week
--------------------------------------
1211 Peter John 2
1215 John Louis 13
1216 Louis Peter 17
I would like to get a list of the last week when each player has played, and the number of games, which should be this:
Id_Player Week numberGames
-----------------------------
Peter 17 2
John 13 2
Louis 17 2
But instead I get this one (notice on Peter week):
Id_Player Week numberGames
-----------------------------
Peter 2 2
John 13 2
Louis 17 2
What I do is this:
SELECT Id_Player,
MAX(Week) AS Week,
COUNT(*) as numberGames
FROM ((SELECT Id_Player1 as Id_Player, Week
FROM Games)
UNION ALL
(SELECT Id_Player2 as Id_Player, Week
FROM Games)) AS g2
GROUP BY Id_Player;
Could anyone help me to find the mistake?

What is the datatype of the Week column? If the datatype of Week is varchar you would get this behavior.

Related

Query to get every sample with at least 3 occurrences at every category

I'm struggling to select every patient which has at least 3 visits at every specialist in 2019, but so far all my attempts have failed.
Could someone has any idea how to achieve?
Thanks in advance.
Table A:
PacjentId
Name
Surname
DateOfVisit
SpecializationId
1
john
doe
2019-04-05
1
1
john
doe
2019-05-06
2
2
mary
jane
2019-02-03
1
2
mary
jane
2019-09-07
2
3
mark
twain
2020-01-01
1
...
...
...
...
...
Table B:
Id
NameOfSpecialisation
1
Diabetologist
2
Internist
...
...
You just need a COUNT(DISTINCT in a HAVING clause:
SELECT PacjentId, Name, Surname
FROM A
WHERE DateOfVisit >= '2019-01-01' AND DateOfVisit < '2020-01-01'
GROUP BY PacjentId, Name, Surname
HAVING COUNT(DISTINCT SpecializationId) >= 3
Your table design appears to be denormalized: A represents Visits and B represents Specializations, therefore there should be another Patients table containing Name, Surname

get extra rows for each group where date doesn't exist

I've been playing with this for days, and can't seem to come up with something. I have this query:
select
v.emp_name as Name
,MONTH(v.YearMonth) as m
,v.SalesTotal as Amount
from SalesTotals
Which gives me these results:
Name m Amount
Smith 1 123.50
Smith 2 40.21
Smith 3 444.21
Smith 4 23.21
Jones 1 121.00
Jones 2 499.00
Jones 3 23.23
Jones 4 41.82
etc....
What I need to do is use a JOIN or something, so that I get a NULL value for each month (1-12), for each name:
Name m Amount
Smith 1 123.50
Smith 2 40.21
Smith 3 444.21
Smith 4 23.21
Smith 5 NULL
Smith 6 NULL
Smith ... NULL
Smith 12 NULL
Jones 1 121.00
Jones 2 499.00
Jones 3 23.23
Jones 4 41.82
Jones 5 NULL
Jones ... NULL
Jones 12 NULL
etc....
I have a "Numbers" table, and have tried doing:
select
v.emp_name as Name
,MONTH(v.YearMonth) as m
,v.SalesTotal as Amount
from SalesTotals
FULL JOIN Number n on n.Number = MONTH(v.YearMonth) and n in(1,2,3,4,5,6,7,8,9,10,11,12)
But that only gives me 6 additional NULL rows, where what I want is actually 6 NULL rows for each group of names. I've tried using Group By, but not sure how to use it in a JOIN statement like that, and not even sure if that's the correct route to take.
Any advice or direction is much appreciated!
Here's one way to do it:
select
s.emp_name as Name
,s.Number as m
,st.salestotal as Amount
from (
select distinct emp_name, number
from salestotals, numbers
where number between 1 and 12) s left join salestotals st on
s.emp_name = st.emp_name and s.number = month(st.yearmonth)
Condensed SQL Fiddle
You could do:
SELECT EN.emp_name Name,
N.Number M,
ST.SalesTotal Amount
FROM ( SELECT Number
FROM NumberTable
WHERE Number BETWEEN 1 AND 12) N
CROSS JOIN (SELECT DISTINCT emp_name
FROM SalesTotals) EN
LEFT JOIN SalesTotals ST
ON N.Number = MONTH(ST.YearMonth)
AND EN.emp_name = ST.emp_name

Limiting records of combinations from 2 columns

looking for some help limiting the results while querying combinations between 2 columns. Here's an example of the kind of table I am working with..
id name group state
1 Bob A NY
2 Jim A NY
3 Dan A NY
4 Mike A FL
5 Tim B NY
6 Sam B FL
7 Brad B FL
8 Glen B FL
9 Ben C FL
I am trying to display all records of all combinations of "group" and "state", but limiting to displaying only up to 2 records for each combination. The result should look like the following..
id name group state
1 Bob A NY
2 Jim A NY
4 Mike A FL
5 Tim B NY
6 Sam B FL
7 Brad B FL
9 Ben C FL
Thanks for the help.
Assuming you always want the two rows for each group and state combination with the lowest id
SELECT *
FROM (SELECT a.*,
row_number() over (partition by group, state
order by id asc) rnk
FROM your_table a)
WHERE rnk <= 2
Of course, since group is a reserved word, I assume your column is actually named something else... You'd need to adjust my query to use the correct column name.

Replicate recent location

I have table like below
ID User Date Location
1 Tom 6-Mar-2012 US
2 Tom 4-Feb-2012 UK
3 Tom 6-Jan-2012 Uk
4 Bob 6-Mar-2012 UK
5 Bob 4-Feb-2012 UK
6 Bob 6-Jan-2012 AUS
7 Dev 6-Mar-2012 US
8 Dev 4-Feb-2012 AUS
9 Nic 6-Jan-2012 US
I have to get each employee recent location in same table.
ID User Date Location CurrentLocation
1 Tom 6-Mar-2012 US US
2 Tom 4-Feb-2012 UK US
3 Tom 6-Jan-2012 Uk US
4 Bob 6-Mar-2012 UK UK
5 Bob 4-Feb-2012 UK UK
6 Bob 6-Jan-2012 AUS UK
7 Dev 6-Mar-2012 US US
8 Dev 4-Feb-2012 AUS US
9 Nic 6-Jan-2012 US US
I have tired with temp tables. can I get this done using single query. This is in middle of implementation. I have already created many temp tables.
Thanks in Advance.
Try this:
select *, CurrentLocation
from tbl x
outer apply
(
select top 1 location as CurrentLocation
from tbl
where [user] = x.[user]
and id <= x.id
order by id
) y
order by id
Output:
ID USER DATE LOCATION CURRENTLOCATION
1 Tom 2012-03-06 US US
2 Tom 2012-02-04 UK US
3 Tom 2012-01-06 Uk US
4 Bob 2012-03-06 UK UK
5 Bob 2012-02-04 UK UK
6 Bob 2012-01-06 AUS UK
7 Dev 2012-03-06 US US
8 Dev 2012-02-04 AUS US
9 Nic 2012-01-06 US US
Live test: http://www.sqlfiddle.com/#!3/83a6a/7
select
id,
user,
date,
location,
first_value(location) over(partition by user order by date desc) as current_location
from your_table s;
The above is valid only in Oracle.
This is my try in absence of first_value analytic function:
select
a.id,
a.usr,
a.date,
a.location,
m.location as current_location
from
a
join
(select usr, location
from
(select usr,
location,
row_number() over(partition by usr order by date desc) as rnk
from a
)b
where rnk = 1) m
on m.usr = a.usr;
The m inner query contains the records with most recent user entries.
After this I make a join with this view, to obtain users's location.
See here sqlfiddle test.

DB2 SQL SUM and GROUPING

I am having problems with querying and grouping.
I am needing the following output:
officr, cbal, sname
ABC, 500.00, TOM JONES
ABC, 200.00, SUE JONES
ABC TOTAL 700.00
RAR, 100.10, JOE SMITH
RAR, 200.05, MILES SMITH
RAR TOTAL 300.15
SQL below produces the error:
[DB2 for i5/OS]SQL0122 - Column SNAME or expression in SELECT list not valid.
SELECT
lnmast.officr, SUM(LNMAST.CBAL), lnmast.sname
FROM
LNMAST
WHERE LNMAST.RATCOD IN (6,7,8) AND STATUS NOT IN ('2','8')
group by lnmast.officr
GROUP BY GROUPING SETS is a POWERFUL tool for grouping/cubing data. It lets you combine non-aggregated data with aggregated data in one query result.
SELECT lnmast.officr, SUM(LNMAST.CBAL), lnmast.sname
FROM LNMAST
WHERE LNMAST.RATCOD IN (6,7,8)
AND STATUS NOT IN ('2','8')
GROUP BY GROUPING SETS ((lnmast.officr, lnmast.sname),(lnmast.officr))
An example from IBM DOCS: www.ibm.com/support/knowledgecenter/en/... :
SELECT WEEK(SALES_DATE) AS WEEK,
DAYOFWEEK(SALES_DATE) AS DAY_WEEK,
SALES_PERSON, SUM(SALES) AS UNITS_SOLD
FROM SALES
WHERE WEEK(SALES_DATE) = 13
GROUP BY GROUPING SETS ( (WEEK(SALES_DATE), SALES_PERSON),
(DAYOFWEEK(SALES_DATE), SALES_PERSON))
ORDER BY WEEK, DAY_WEEK, SALES_PERSON
This results in:
WEEK DAY_WEEK SALES_PERSON UNITS_SOLD
----------- ----------- --------------- -----------
13 - GOUNOT 32
13 - LEE 33
13 - LUCCHESSI 8
- 6 GOUNOT 11
- 6 LEE 12
- 6 LUCCHESSI 4
- 7 GOUNOT 21
- 7 LEE 21
- 7 LUCCHESSI 4