Fantasy Football Data postgre sql Get first instance - sql

So i have a table that has draft history. call it "Draft Results".
team_id pick_number round position playerID
0002 01 01 WR 12
0002 01 02 QB 09
0002 01 03 TE 32
0002 01 04 RB 23
0034 02 01 WR 43
0034 02 02 WR 24
0034 02 03 QB 04
0034 02 04 QB 11
Only each team id has a pick for 20 rounds. I have about 7000 team_ids. I want to total which round each team took their first qb and I need help figuring out a sql query to accomplish that.
SELECT "Round", COUNT("Team_Id") FROM public."Draft Results"
WHERE "Position" = 'QB'
GROUP BY "Round"
ORDER BY "Round" asc
there are 5 different positions: 'QB', 'RB', 'WR', 'TE', 'D'
In this instance i would want the query to show that here was 1 team with a first qb selected in the 2nd round and 1 team where their first QB was selected in the 3rd. Currently my query would show what i mentioned AND that 1 team selected a QB in the 4th round. Even though that was not that teams FIRST QB selected (they already picked one in the 3rd)

Isn't it simple aggregation with min:
select team_id,
min(round) as round
from "Draft Results"
where position = 'QB'
group by team_id;

Perhaps try MIN() aggregate function on the "Round" column
SELECT MIN("Round"), COUNT("Team_Id") FROM public."Draft Results"
WHERE "Position" = 'QB'
GROUP BY "Round"
ORDER BY "Round" asc
This would then grab the minimum value for the "Round" column which would be the first or lowest 'round' that Position would be 'QB'. In your example, team_id 0034 selected a 'QB' in round 03 and round 04 but selecting the MIN() value of those two would select the first instance or lowest round they chose a 'QB' in which would be the 3rd round. I hope this helps and that I understand what you are trying to accomplish.

If you want to see the whole table, but constrained to the rows you want returned by virtue of the earliest round logic, I would use this:
SELECT *
FROM draft_results d
INNER JOIN (
SELECT team_id, MIN(round_id) minRndQBPicked
FROM draft_results d
WHERE position = 'QB'
GROUP BY team_id
) minRnd ON d.team_id = minRnd.team_id AND d.round_id = minRnd.round_id

Related

How can I filter out a ROLAP pattern on SQL Server 2012?

How to remove a pattern from a 'CUBE' query:
I have this query:
select
coalesce(left(k.SubCodiceStab,12),'AUSL_TOTALE') as Stabilimento,
1.0 * sum(k.PotenzialmenteInappropriato) / (count(k.Progr) - sum(k.PotenzialmenteInappropriato)) as Rapporto,
coalesce(DescrDisciplina,'TOT. STABILIMENTO') AS Disciplina,
coalesce(K.TipoDRG,'TOT. DRG') as TipoDRG,
anno
from
my_table k
where
k.Mese in (1,2)
group by
SubCodiceStab, DescrDisciplina, TipoDRG, anno with cube
I don't need to take out the 'cumulative total' over multiple years for the single Disciplina, but I still want to be able to do the 'Grand'cumulative.
this is an exeplum of the my query result (I, by now, don't have 2017 but will be added soon):
Stabilimento Rapporto Disciplina TipoDRG anno
------------ ------------------- ---------- -------- -----------
(#)AUSL_TOT. 0.225806451612 AUSL_TOT TOT. DRG NULL
AUSL_TOTALE 0.000000000000 AUSL_TOT C NULL
AUSL_TOTALE 0.229508196721 AUSL_TOT M NULL
04 0.000000000000 ASTAN C 2018
04 0.000000000000 ASTAN C NULL
04 0.229508196721 ASTAN M 2018
04 0.229508196721 ASTAN M NULL
04 0.225806451612 ASTAN TOT. DRG 2018
(*)04 0.225806451612 ASTAN TOT. DRG NULL
I'd like to remove the last line without the possibility of having the 'GRAND TOTAL'. Basically I still need row (#) [what I think is the empty pattern] but I also want to discard row (*).
In general terms I would remove a specific pattern from the cube (without affecting other patterns)

How do I add specific values from one different table to columns in a row based off of values in another table?

In SQL Server I have 2 tables that looks like this:
TEST SCRIPT 'a collection of test scripts'
(PK)
ID Description Count
------------------------
A12 Proj/Num/Dev 12
B34 Gone/Tri/Tel 43
C56 Geff/Ben/Dan 03
SCRIPT HISTORY 'the history of the aforementioned scripts'
(FK) (PK)
ScriptID ID Machine Date Time Passes
----------------------------------
A12 01 DEV012 6/26/15 16:54 4
A12 02 DEV596 6/28/15 13:12 9
A12 03 COM199 3/12/14 14:22 10
B34 04 COM199 6/30/13 15:45 12
B34 05 DEV012 6/30/15 13:13 14
B34 06 DEV444 6/12/15 11:14 14
C56 07 COM321 6/29/14 02:19 12
C56 08 ANS042 6/24/14 20:10 18
C56 09 COM432 6/30/15 12:24 4
C56 10 DEV444 4/20/12 23:55 2
In a single query, how would I write a select statement that takes just one entry for each DISTINCT script in TEST SCRIPT and pairs it with the values in only the TOP 1 most recent run time in SCRIPT HISTORY?
For example, the solution to the example tables above would be:
OUTPUT
ScriptID ID Machine Date Time Passes
---------------------------------------------------
A12 02 DEV596 6/28/15 13:12 9
B34 05 DEV012 6/30/15 13:13 14
C56 09 COM432 6/30/15 12:24 4
The way you describe the problem is almost directly as cross apply:
select h.*
from testscript ts cross apply
(select top 1 h.*
from history h
where h.scriptid = ts.id
order by h.date desc, h.time desc
) h;
Please try something like this:
select *
from SCRIPT SCR
left join (select MAX(SCRIPT_HISTORY.Date) as Date, SCRIPT_HISTORY.ScriptID
from SCRIPT_HISTORY
group by SCRIPT_HISTORY.ScriptID
) SH on SCR.ID = SH.ScriptID

Count matching pairs

This is a little complicated so bear with me. Below is a table named "list" with 2 columns. The table has data of each member and the films that they like. i.e member 01 likes film 02, 05, 14, 21 and 25. What I want to find out is how many similar films does each member have with another member. For example, member 01 and member 02 have one film in common (film 14). Is there any way to write this in SQL?
List
------ ------
member film
------ ------
01 02
01 05
01 14
01 21
01 25
02 03
02 09
02 14
03 01
03 05
03 17
03 21
You can write a general query for this using a self-join and aggregation. The following summarizes the results for each pair of users:
select l1.member, l2.member, count(*) as NumFilmsInCommon
from list l1 join
list l2
on l1.member < l2.member and l1.film = l2.film
group by l1.member, l2.member;
The < condition just ensures that each pair of members only appears once in the result set.

Add a zero record when count is 0

I want to list BR, BRANCHNAME and the number of people employed in it. There are 5 branches it total and only 4 of them have people employed in it; Branch 05 has no employees in it. After using the following code, the branch 05 will not be shown as the row of branch 05 will not be included after the where statement. I want to show a row of "05 Br05 0".
SELECT EMPLOYEE.BR, BRANCHNAME, Count(*) AS Number
FROM EMPLOYEE, BRANCH
WHERE (EMPLOYEE.BR = BRANCH.BR)
GROUP BY EMPLOYEE.BR, BRANCHNAME;
The result is:
BR BRANCHNAME Number
01 Br01 6
02 Br02 4
03 Br03 5
04 Br04 6
I want to have the following result:
BR BRANCHNAME Number
01 Br01 6
02 Br02 4
03 Br03 5
04 Br04 6
05 Br05 0
It would seem you want a LEFT JOIN which gives a countable row with a null result even if there is no matching employee.
Since you've not added your table structure, I assume branchname is a field in the branch table.
SELECT branch.br, branch.branchname, COUNT(employee.br) AS Number
FROM branch
LEFT JOIN employee
ON branch.br = employee.br
GROUP BY branch.br, branch.branchname
An SQLfiddle to test with (based on SQL Server since Access is not available)

SQL: how to select IDs according to a condition?

I just started to program in SQL and I have a bit of a problem (n.b., I am working of a tabl that come from a game). My table is something like this, where ID refers to a single person, H to a certain hour of playing and IF to a certain condition:
ID H IF
01 1 0
01 2 0
01 3 0
02 1 0
02 2 1
03 1 0
03 2 1
03 3 0
03 4 1
In this case player 01 played for three hours, player 02 for two hours and player 03 for four hours. In each of these hours they may or may have not performed an action. If they did, a 1 appears in the IF column.
Now, my doubt is: how can I query so that I have a table with only the ID of the people who never performed the action? I do not want to rule out only the row with IF = 1, I want to rule out all the row with that ID. In this case it should become:
01 1 0
01 2 0
01 3 0
Any help?
This should do it.
select *
from table
where Id not in (select Id from table where IF = 1)
SELECT ID FROM Table GROUP BY ID HAVING SUM(IF)=0