Order by Multiple Columns with SoQL - socrata

How do I format a query with multiple order by columns. The data I'm working with has a date column and a time column and I want to order by both of them. I know how to do this query in regular SQL but I can't make it work in SoQL. Here is what I've tried:
This works ('date DESC') but isn't what I'm trying to do:
http://data.sfgov.org/resource/tmnf-yvry.json?$order=date+DESC
This fails ('date DESC, time DESC') with a 403 error:
http://data.sfgov.org/resource/tmnf-yvry.json?$order=date+DESC%2Ctime+ASC
This fails ("'date DESC, time DESC'") with a 403 error:
http://data.sfgov.org/resource/tmnf-yvry.json?$order=%27date+DESC%2C+time+DESC%27

Currently, sorting on multiple columns at the same time is something you unfortunately can't do with the SODA API. It'll respond with a "query.execution.queryTooComplex" error like you're seeing.
However, this is something that'll be fixed in the future as we migrate datasets to our new backend. Details on this process and how to tell when/if a dataset has been migrated will be available soon.
Note: You also need to use the $order parameter in your query, not just order. I'll edit your URLs above to match.

Related

Get the value of COUNT(*) into an html table

I'm using a servlet to make the jdbc connection, write the PreparedStatements and execute ResultSets. I am able to display the data into a webpage just fine, however I also want to be able to count the number of entries. I know there are other ways to count how many rows I have using java code, but I want to use SQL statements and I saw this
SELECT COUNT(*) FROM table_name;
and made a preparedstatement and tried to execute. However, it is not returning the value of the count, I instead get
"com.mysql.jdbc.JDBC42ResultSet#4a9b1e8b" or "com.mysql.jdbc.JDBC42PreparedStatement#4a9b1e8b" (because I tried getting the value of the count using both).
Basically, I am wondering how to get the count value in my html table from the servlet, not the long statements above.
Many thanks, I'm a beginner.
When you execute your SQL query with JDBC, you get a Resultset even when you get only one record with only one field as in your question.
You need to call the getInt or the getLong method of your recordset to get the actual value.
long countValue = rs.getLong(1);
Have a look at Oracle's documentation on JDBC
You can also have a look at this post on SO

Sort a View SQL Server 2014

I am trying to find a way to sort the results in a view. I know the Order by command does not work. The reason for this is that we are using a BI tool 'Board' to drill through to the raw data. Unfortunately there is no way in the program to sort a drill through as I would a regular query since the Where clause is dynamic and the program always puts it at the end of the query.
Hence an Order by would appear prior to a Where which results in an error.
Any suggestions would be greatly appreciated.
Thanks,
Scott
So there's a couple of ways to achieve this, one is to make sure that the data the view is reading is already sorted, to do this, you make sure that the indexes on the table/s that the view is reading from are sorted the way you want, if this isn't possible, then you could also create a clustered index on the view itself and specify the sort order there.
If creating indexes is not possible, there is another way that I have found to work, but is not always ideal as it creates an extra column in your result set that doesn't mean anything, you can do this:
SELECT ROW_NUMBER() OVER ( ORDER BY [col1] ASC ) AS [sort_col] ,
[col1] ,
[col2] ,
[col3]
FROM [dbo].[table];
If you do this, you will end up with the extra column giving the row number, but the data will be sorted by [col1]. One thing to mention with this is that if you do put this into the view definition, you must still select that column in your query for the sorting to take place.

group by in rails return strange data

I am trying to apply group active record command in rails rest api, however my database is in MySql.
When I query without group by I get correct data but when I use group on the same query I get strange data collection. I am using group to decrease query time coz in original it takes alot of time to retrieve data from database
Here is my original query
Records.owned_by(User.find_by_email(params[:user].to_s).id).where(device_id: params[:did]).includes(:record_students, :record_employees, :record_admins, :record_others)
but when I use group to increase the efficiency the returned data set is not valid
here is my new query with group
Records.owned_by(User.find_by_email(params[:user].to_s).id).where(device_id: params[:did]).includes(:record_students, :record_employees, :record_admins, :record_others).group("date(created_at)")
any idea what is wrong. Thanks

Using Socrata SODA API to query most recent rows by datetime

I am new to this site and this is my first question. I am trying to query the "Seattle Real Time Fire 911 Calls" database from the Socrata Seattle Open Data site: https://data.seattle.gov/Public-Safety/Seattle-Real-Time-Fire-911-Calls/kzjm-xkqj. I'm not an expert at using the SODA API, and I'm having difficulty figuring out how to query the most recent entries in the database. All attempts to use the "order" or "where" SoQL statements give me data from 2010 or 2011, and I cannot figure out how to query the most recent 300 entries. Querying the "top" rows yields the oldest entries. Using a full OData feed pull yields data as recent as today, but I need to use a fast json or csv SODA API query.
Note: The datetime field does not respond to any "where" statements that I use.
Thank you!
OK, a few tips to get started:
The $order parameter sorts by default in ascending (ASC) order, so you'll want to actually order by datetime DESC to get the latest records first
Unfortunately Seattle has a number of crimes that are listed with no datetime, so you'll also want to filter with a $where query to only retrieve results in a date range. $where=datetime > '2014-07-01' works for me, for example
To only get the top 300 results, you'll want to pass a $limit=300 parameter as well.
Here's a sample request in Runscope for you to try out.

MySQL Order By Date

I have an a MySQL database. My database contains documents with a datetime column called "created". I want to group by day in order to have the document count per day. However, some days have zero documents and as a result they are not part of the output. For example I need '2001-01-01' to have a zero count if documents do not exist.
I am thinking of creating an extra table with the date range I am interested on and the to Do an outer join with my table. Then I can group by date to have my results.
Is there any better way of doing such a thing?
My SQL code:
Select date(created_at),c.text from Dates d left outer join classifier c on d.n=DATE(c.created_at)
where c.classifier="2014streamlrall"
and date(c.created_at)>='2014-03-01' and date(c.created_at)<='2014-05-01'
order by d.n;
The left join still does not work.
There is no better way for that in MySQL.
It lacks both a method to generate an arbitrary length resultset (similar to PostgreSQL's generate_series) and recursive SQL required to emulate such a method (which is used in SQL Server and Oracle).
Even on SQL Server, populating and keeping a table with 100 years worth of dates (which takes but a little more than 73K records) gives much better performance on reports similar to yours than using a generated resultset.
Is your real issue with having a create a row with '2001-01-01: 0'? If so, just wrap the MySQL with PHP and control your output with PHP. Then you just echo (or create an XML entry, whatever formatting you need) with the date and the result, even if it's 0. Even if you're currently know nothing about PHP, there are plenty of tutorials on how to run MySQL queries in PHP and output results. Good luck.