Using the socrata $order param in DESC order - socrata

I've been playing around with this API and cannot get the $order parameter to work when paired with DESC:
https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC
Not only is the data not being sorted accordingly, but the column name/json key (score) I'm attempting to sort against is being omitted from the query results!
According to Socrata docs, you can set to ascending order by replacing the 'DESC' with 'ASC'. The same can be accomplished by not specifying it at all - it will default to ASC. And both of these work fine when I test.
But I can't get DESC to work at all. Thanks.

For some entries, the score column contains nulls, which are sorted first because SQL is weird.
Try adding a filter for $where=score IS NOT NULL:
GET https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC&$where=score%20IS%20NOT%20NULL

Related

SQL sorting by asc/ desc

How do i sort my data in sql in asc or desc order?
I am using :
ORDER BY DESC
but my data is not in order. The data for my codes are below :
AC1, AC2 ,AC3 ..., AC30,AC31
The sql sorts the data by the first letter and not the value of the data so my data is sorted like this
AC10, AC11, AC12, AC13 ... AC19
Next in line would be :
AC20, AC21, AC22, AC23
I would like my data to be sorted from the very first number (AC1,AC2,AC3...) and not (AC1,AC10,AC11)
The order by is working correctly. Your expectation is wrong. Strings are sorted alphabetically.
You can easily get what you want. Assuming the column starts with two letters, you can do:
order by char_length(col) desc, col desc
If you want the default sort to work, then just pad the numbers . . . AC01, AC02, and so on.

Mysql WHERE IN not working when i put swap the comma seperated value

I have this below weird problem with my mysql query. Correct me if i am wrong.
This below query( Which i have printed from Codeigniter ) giving me result with subject = 4.
WHERE `notes`.`subject` IN ('4,2') GROUP BY `notes`.`id` ORDER BY `created_date` DESC
But when i changed this
WHERE `notes`.`subject` IN ('2,4') GROUP BY `notes`.`id` ORDER BY `created_date` DESC
It is not returning any result. Why is that?
Lets say i have only one result in table and i am using codeigniter for this.
$this->db->where_in('notes.subject',$this->input->get('subject'));
in should have parameter string like
IN ('2','4'); // for varchar
IN (2,4) // for Integer values
the issue is here
$this->db->where_in('notes.subject',$this->input->get('subject'));
the input variable subject is a string for eg '2,4'
you can use $this->db->where_in('notes.subject',explode(",",$this->input->get('subject')));
which will pass an array to where_in
and as per the User guide
Second parameter to where_in() is an array
Try like this:
WHERE `notes`.`subject` IN ('4','2') ....
for Integers
WHERE `notes`.`subject` IN (4,2) ....

Typo3 Extbase query result sorting by title after offset sorted by date

i have a extbase query result set with an offset of let's say 15 objects sorted by date (crdate DESC), so the 15 latest elements are skipped. The sorting of my actual result set should now be sorted by a different field, e.g. the 'title' field.
I have this case because I display the latest 15 elements in a different plugin on the site before. At the bottom of the website I want to display all but the first 15 elements in an archive list that has a filter function to filter by date ASC or title DESC.
I guess this is not possible with the default Extbase API, and I have to build a mySQL query myself. How would such a query look like ?
thanks!!!
I found out that it's possible to do that in mySQL with 'encapsulated' queries such as the following
SELECT *
FROM (
SELECT *
FROM `tx_dentalarticle_domain_model_article`
WHERE `type` = 0
ORDER BY `tx_dentalarticle_domain_model_article`.`date` DESC
LIMIT 15 , 9999
) AS articles
ORDER BY `title` DESC
to use it in extbase you have to use a raw statement such as:
$query = $this->createQuery();
$query->getQuerySettings()->setReturnRawQueryResult(TRUE);
$query->statement($sql);
return $query->execute();

How to sort the columns in the mysql database

I am having column named rating in the mysql database table with multiple values from 1+,2+,................9+,10+,12+. when i am sorting this column with query
select * from tbl_app order by rating desc
I am getting 9+ as highest value, can any one tell me how to get 12+ as highest value
SELECT rating,SUBSTR(rating,1,LENGTH(rating)-1) FROM tbl_app ORDER BY CAST(SUBSTR(rating,1,LENGTH(rating)-1) as SIGNED) DESC;
if the last char is always a '+',the sql above will work.
what have you kept the datatype of the column rating ? If you have kept it varchar or text then this query will not work for sorting values as per descending order.
Probably the easiest thing to do in MySQL is cast those odd looking strings to numbers:
order by cast(rating as unsigned) desc
-- or less explicitly
order by rating + 0 desc
Both of those casts will stop trying to convert the string to a number when they hit the + so you'll get them sorted numerically.
Simply removing the plus signs from the strings will still leave you with strings and '10' < '2' is just as true for strings as '10+' < '2+'. That's actually your whole problem: you're storing numbers as decorated strings when you should be storing them as integers and adding the + decorations when you display them. You really should fix your schema to make sense instead of adding ugly hacks to work around your schema's strange ideas.
try this :
select convert(replace(rating,'+',' '),unsigned integer) as x from tab order by x desc
sql_fiddle_demo

ORDER BY "ENUM field" in MYSQL

There is a field 'noticeBy' enum('email','mobile','all','auto','nothing') NOT NULL DEFAULT 'auto'. As it known ordering by ENUM field performs relative to its index. However, how it possible make order by its values?
As documented under Sorting:
ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a'). The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.
To prevent unexpected results when using the ORDER BY clause on an ENUM column, use one of these techniques:
Specify the ENUM list in alphabetic order.
Make sure that the column is sorted lexically rather than by index number by coding ORDER BY CAST(col AS CHAR) or ORDER BY CONCAT(col).
Per the second bullet, you can therefore sort on the column after it has been cast to a string:
ORDER BY CAST(noticeBy AS CHAR)
This also works:
ORDER BY FIELD(noticeBy, 'all','auto','email','mobile','nothing')
(I don't believe that there is a setting to achieve this, you have to provide the sort-values.)
You can define your order however you wish:
ORDER BY CASE noticeBy
WHEN 'email' THEN 1
WHEN 'mobile' THEN 2
WHEN 'all' THEN 3
WHEN 'auto' THEN 4
ELSE 5
END
This will return the rows in the following order: email, mobile, all, auto, nothing.
In my case, I had to sort enum results by the "ENUM" field and also make sure the values are in DESCENDING order. My enum had the following values: 'Open','Closed'
So when I used ORDER BY CAST(status AS CHAR), the results were in this order:
Closed
Open
Open
But I wanted the Open status tickets to be shown first and then the Closed tickets. So I used the following:
ORDER BY CAST(status AS CHAR) DESC
This gave me the order that I was looking for i.e.
Open
Open
Closed
Summary:
Just using ORDER BY CAST on an enum did not seem to help. To sort the results in a specific order, mentioning ASC or DESC as well, did the trick.
The best option to me:
ORDER BY FIELD(status, 'publish','not-publish','expirated','deleted'), creation DESC
Status is the field in my BBDD, and values in '' are the values that has in enum options.
I hope that help u too! :)