SQL Custom Query for Tableau - sql

I am trying to create a custom SQL query in tableau to create 2 fields and am a little stuck on how to do this. I want to create a field1 that is unanswered scores and field 2 that is max point differential during the game. The relevant data fields are game_id, drive_id, result(each result would have different score value), and team.
For field1 the logic in standard language would be for each game what are most scoring results that occur for one team over the other.
For field2 the logic in standard language would be turn the results into a points value and then find the score of the game for each team at the time of the drive. Drive id is in numerical order.
Thanks for any help

Related

Is it possible to use SQL to show the average of some values in one column and then in subsequent columns display the individual values?

I have a bunch of data and I want the output to display an average of all the data points but also the individual data points in subsequent columns. Ideally it would look something like this:
Compound | Subject | Avg datapoint | Datapoint Experiment 1 | Datapoint Exp 2 | ...
..........XYZ......|.....ABC....|............40...............|...............20..............................|...............60...............|......
..........TUV......|.....ABC....|............30...............|...............20..............................|...............40...............|......
..........TUV......|.....DEF....|............20...............|...............10..............................|...............30...............|......
One problem I'm running in to is that I get repetitive lines of information. Another is that I have some rows pulling in info that doesn't apply, such that some of the individual datapoints in, say, row 2 would have info from subject DEF when I only want it to have info from subject ABC.
I hope this makes sense! I'm currently using inner join with a ton of where qualifiers. I'm close but not quite there. Any help is appreciate and let me know if I can provide additional info to help you help me.
The SQL language has a very strict rule requiring you to know the exact number of columns for your result set in advance, before looking at any data in your tables.
Therefore, if this average is based off a known fixed number of columns, or if the number of potential columns is reasonably small, where you can manually setup placeholders, then this will be possible. The key search terms to learn how to do this is "conditional aggregation", where you may also need to join the table to itself for each field.
Otherwise, you will need to pivot and aggregate your data in your client code or reporting tool.

Tableau count values after a GROUP BY in SQL

I'm using Tableau to show some schools data.
My data structure gives a table that has all de school classes in the country. The thing is I need to count, for example, how many schools has Primary and Preschool (both).
A simplified version of my table should look like this:
In that table, if I want to know the number needed in the example, the result should be 1, because in only one school exists both Primary and Preschool.
I want to have a multiple filter in Tableau that gives me that information.
I was thinking in the SQL query that should be made and it needs a GROUP BY statement. An example of the consult is here in a fiddle: Database example query
In the SQL query I group by id all the schools that meet either one of the conditions inside de IN(...) and then count how many of them meet both (c=2).
Is there a way to do something like this in Tableau? Either using groups or sets, using advanced filters or programming a RAW SQL calculated fiel?
Thanks!
Dubafek
PS: I add a link to my question in Tableu's forum because you can download my testing workbook there: Tableu's forum question
I've solved the issue using LODs (specifically INCLUDE and EXCLUDE statements).
I created two calculated fields having the aggregation I needed:
Then I made a calculated field that leaves only the School IDs that matches the number of types they have (according with the filtering) with the number of types selected in the multiple filter (both of the fields shown above):
Finally, I used COUNTD([Condition]) to display the amounts of schools matching with at least the School types selected.
Hope this helps someone with similar issue.
PS: If someone wants the Workbook with the solution I've uploaded it in an answer in the Tableau Forum

SQL query for game rental website - game delivery report

http://sqlfiddle.com/#!3/f2da70/13
I am creating a game rental website using MS SQL 08 and I have to create a report which will give me a list of all the games that need to be posted and to who.
This query will have to go through different stages:-
List the users that have not reached their game quota for that month based on the subscription package they have chosen and rental table.
Compare this list of users to their favourites list and asign a game to them based on what game is their highest priority and by date.
The game or games selected must be in stock which needs compared to quantity levels in games table.
I have a SQL query but it is not taking into consideration the dates that a user added a game to their favourites list, it works solely on priority level. I need to change this query so it picks the oldest date added if the priority of the games are the same. In this sample data in SQL Fiddle it should be giving me barcode 27 to match with Need for Speed Rivals but it is giving me FIFA 14 as it is the first favourite record it sees.
Also the report needs to show more than the game barcode, it needs to show user name and game details. It is also only working for a single user at the moment with with me entering a user ID manually but it should just go through all users.
If anyone can help me with this query it would be much appreicated it, just really dont know where to go from here.
change
ORDER BY Favourites.Priority
to
ORDER BY Favourites.Priority, Favourites.DatePicked asc

MS Access: sort, select & lookup combo

This is a question about recommended approach and syntax.
I have a sports-management program in which swimmers are grouped into teams of four, with a team pace. I need Access to determine the team pace for each team based on the slowest member of each team.
Specifically, I have a Participants table with each participant's pace and the name of each participant's team, and a Paces lookup table of sorted and color coded paces. I need Access to compare the participant paces on a team, choose the slowest pace on thta team (which has the lowest index# on the pace lookup table) and return the color code for that pace, such as "Green". (This team would then be one of the Green teams.)
That data value is then written to a Teams table that lists all teams by color.
Where do I start? Thx.
Here is a linear straight forward approach: Create a view that returns the team name and maximum pace using the MAX function using the Participants table; GROUP BY team name. Then create a second view that joins the first view to your Paces look up table WHERE MAX pace is BETWEEN the minimum and maximum values for a color.

How to autofill table column in MS Access?

I am trying to develop a simple database that stores taking information for a taxi daily figure etc. and there are some calculations that I would like to have auto-filled from basic information supplied by the driver, such as:
gross takings given a start and end value from a metering device km's
driven given an odometer reading driver owner split given the days takings
The problem I have is I would like to store all these values in a single attribute to make retrieval and entry into another third party system easier. Currently this is paper based and I am trying to digitize this process
The operations are simple mathematical expressions such as addition subtraction and percentage split (multiplication or division)
I've tried various sql commands like
INSERT INTO table (fieldname)
select
table.feildname1, table.feildname2, [feildname2]-[fieldname1]
from
table
I will be using a input form for data entry that will display the basic data input and a drivers share of takings/expenses based upon these calculations
And I'm drawing a blank I'm using ms access 2007
You can do:
INSERT INTO table (fieldname)
SELECT CStr(table.feildname1) & CStr(table.feildname2) & CStr([feildname2]-[fieldname1])
FROM table;
But as #Tarik said, it is not recommended to store all fields in one column, unless it is some temp table or just for view.