Google Fusion Tables SQL Query ORDER BY and GROUP BY in same query doesn't work - group-by

I'm writing a mobile web page using Google Fusion Tables for my data and I need to use both ORDER BY (lat and long) as well as GROUP BY (in place of DISTINCT - which GFT doesn't support).
But, it seems the two do not play well together. If I use GROUP BY, the statement seems to simply ignore the ORDER BY.
SQL statement:
SELECT Count(), Facility_Name FROM 2206340 GROUP BY Facility_Name
ORDER BY ST_DISTANCE(Lat, LATLNG(" + lat + "," + lng + "))LIMIT 10
Has anyone else run into this scenario?

You can't order by a column that isn't included in the select/group by clause (unless you use an aggregate function)

Related

How do I pass results from one Custom Query to another?

I am using Tableau to create custom queries, I need to pass the results from one custom query into another. So for example one custom query might have the following query:
select *
from bime.test_tab
where record_date = '2018-05-21'.
I then create another custom query which uses the results from the query above. Is this possible?
Sure you can by making this custom query as a subquery
select * from (select *
from bime.test_tab
where record_date = '2018-05-21') a
As you might already know that we do not have a leverage to use temp tables in Tableau custom queries, hence we usually create subqueries for that.
Considering your case,
SELECT BIM_TABLE.* FROM
(SELECT * FROM
BIME.TEST_TAB WHERE RECORD_DATE= '2018-05-21')BIM_TABLE
You can post your actual problem with the relevant columns if you need further help.
PS - It's advised to do most of your data manipulations in SQL itself (if you are running your workbook on live connection), as Tableau is not able to properly optimize part of the 'Custom SQL' query.

Using custom variables in Sphinx queries

I need execute 2 queries by one queue and passing result of first query to IF expression of other query as well as ordinary MySQL queries.
As example, I try pass #average variable into second query:
SET #average=(SELECT AVG(weight()) avg_rank FROM common WHERE match('query text') OPTION ranker=expr('sum(word_count)*100 + sum(lcs*user_weight)*100 + bm25 + sum(exact_order)*200');
SELECT *, weight() as rank, 2000 * exp( - 9.594E-5 * abs(1486121357 - _rank_date)/1000) AS date_rank, IF(_importance > #average,_importance,0) AS importance_rank, (rank + date_rank + importance_rank) as total_rank FROM common WHERE match('query text') OPTION ranker=expr('sum(word_count)*100 + sum(lcs*user_weight)*100 + bm25 + sum(exact_order)*200')
But i had parse error. How i can do it?
Don't think you will be able to do that in Sphinx as such.
The application would just have to run the first query, capture the value, and write it explicitly into the second query.
But it also seems that the expression jsut modifies what is returned anyway (rather than say reordering or filtering results), so rather than getting sphinx to compute the IF expressions, just do it in the application.

Kentico GET Form Data

I am currently trying to use the Kentico API to gather the data from all forms within Kentico.
So far I've found that there are two places to view form data and they can be found at these endpoints:
/rest/cms.forms <---- Returns all form definitions (excluding field data types
/rest/bizformitem.bizform.FORM_NAME/ <---- Returns all form data (inserted by end users)
What I am trying to do is keep a record of all of the form data on a daily basis. Is there a better way to do this using the API rather than making 'x' number of calls (one per form).
EDIE: Out of 100+ forms I only need to pull 15-20 of them on a daily basis.
You can get all in sql and it depends how many forms you have. Each form is separate sql table that has a record in CMS_Class table
-- this will give the list of all tables that you need query
select ClassTableName from CMS_Class where ClassIsForm = 1
Then you can find out the ones that were updated let say with in the 24 hrs
SELECT
[db_name] = d.name
, [table_name] = SCHEMA_NAME(o.[schema_id]) + '.' + o.name
, s.last_user_update
FROM sys.dm_db_index_usage_stats s
JOIN sys.databases d ON s.database_id = d.database_id
JOIN sys.objects o ON s.[object_id] = o.[object_id]
WHERE o.[type] = 'U'
AND s.last_user_update IS NOT NULL
AND s.last_user_update BETWEEN DATEADD(day, -1, GETDATE()) AND GETDATE()
and s.[object_id] in (select OBJECT_ID(ClassTableName)
from CMS_Class where ClassIsForm =1 )
You might have a few hundred forms... to go and query few hundred tables might be unproductive. I usually get 18-20 out 100+ we have.
There is Kentico API (not a REST API) that allows you to get all data you need at code behind. You can find examples here.

Converting complex query with inner join to tableau

I have a query like this, which we use to generate data for our custom dashboard (A Rails app) -
SELECT AVG(wait_time) FROM (
SELECT TIMESTAMPDIFF(MINUTE,a.finished_time,b.start_time) wait_time
FROM (
SELECT max(start_time + INTERVAL avg_time_spent SECOND) finished_time, branch
FROM mytable
WHERE name IN ('test_name')
AND status = 'SUCCESS'
GROUP by branch) a
INNER JOIN
(
SELECT MIN(start_time) start_time, branch
FROM mytable
WHERE name IN ('test_name_specific')
GROUP by branch) b
ON a.branch = b.branch
HAVING avg_time_spent between 0 and 1000)t
GROUP BY week
Now I am trying to port this to tableau, and I am not being able to find a way to represent this data in tableau. I am stuck at how to represent the inner group by in a calculated field. I can also try to just use a custom sql data source, but I am already using another data source.
columns in mytable -
start_time
avg_time_spent
name
branch
status
I think this could be achieved new Level Of Details formulas, but unfortunately I am stuck at version 8.3
Save custom SQL for rare cases. This doesn't look like a rare case. Let Tableau generate the SQL for you.
If you simply connect to your table, then you can usually write calculated fields to get the information you want. I'm not exactly sure why you have test_name in one part of your query but test_name_specific in another, so ignoring that, here is a simplified example to a similar query.
If you define a calculated field called worst_case_test_time
datediff(min(start_time), dateadd('second', max(start_time), avg_time_spent)), which seems close to what your original query says.
It would help if you explained what exactly you are trying to compute. It appears to be some sort of worst case bound for avg test time. There may be an even simpler formula, but its hard to know without a little context.
You could filter on status = "Success" and avg_time_spent < 1000, and place branch and WEEK(start_time) on say the row and column shelves.
P.S. Your query seems a little off. Don't you need an aggregation function like MAX or AVG after the HAVING keyword?

SQL Select rows by comparison of value to aggregated function result

I have a table listing (gameid, playerid, team, max_minions) and I want to get the players within each team that have the lowest max_minions (within each team, within each game). I.e. I want a list (gameid, team, playerid_with_lowest_minions) for each game/team combination.
I tried this:
SELECT * FROM MinionView GROUP BY gameid, team
HAVING MIN(max_minions) = max_minions;
Unfortunately, this doesn't seem to work as it seems to select a random row from the available rows for each (gameid, team) and then does the HAVING comparison. If the randomly selected row doesn't match, it's simply skipped.
Using WHERE won't work either since you can't use aggregate functions within WHERE clauses.
LIMIT won't work since I have many more games and LIMIT limits the total number of rows returned.
Is there any way to do this without adding another table/view that contains (gameid, teamid, MIN(max_minions))?
Example data:
sqlite> SELECT * FROM MinionView;
gameid|playerid|team|champion|max_minions
21|49|100|Champ1|124
21|52|100|Champ2|18
21|53|100|Champ3|303
21|54|200|Champ4|356
21|57|200|Champ5|180
21|58|200|Champ6|21
64|49|100|Champ7|111
64|50|100|Champ8|208
64|53|100|Champ9|8
64|54|200|Champ0|226
64|55|200|ChampA|182
64|58|200|ChampB|15
...
Expected result (I mostly care about playerid, but included champion, max_minions here for better overview):
21|52|100|Champ2|18
21|58|200|Champ6|21
64|53|100|Champ9|8
64|58|200|ChampB|15
...
I'm using Sqlite3 under Python 3.1 if that matters.
This is in SQL Server, hopefully the syntax works for you too:
SELECT
MV.*
FROM
(
SELECT
team, gameid, min(max_minions) as maxmin
FROM
MinionView
GROUP BY
team, gameid
) groups
JOIN MinionView MV ON
MV.team = groups.team
AND MV.gameid = groups.gameid
AND MV.max_minions = groups.maxmin
In words, first you make the usual grouping query (the nested one). At this point you have the min value for each group but you don't know to which row it belongs. For this you join with the original table and match the "keys" (team, game and min) to get the other columns as well.
Note that if a team will have more than one member with the same value for max_minions then all these rows will be selected. If you only want one of them then that's probably a bit more complicated.