Sorting by boolean fields in sql by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Here is a trick I have found:

Assume we have a table abilities with field available.

select abilities.* from abilities order by 
(case when available then 1 when available is null then 2 else 3 end) asc

It replaces true as 1, null as 2 and false as 3 and then sorts by integers.

Do you know another solution?

#sql

Group by float column in postgresql by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

If you have same float values in your database you can't group by it.

But you are able to convert it to numeric and round it. For example:

SELECT COUNT(*) AS count_all, lat::float::numeric(9, 6), lng::float::numeric(9, 6) AS lat_float_numeric_9_6_lng_float_numeric_9_6 FROM "locations" GROUP BY lat::float::numeric(9, 6), lng::float::numeric(9, 6)

#sql #postgresql

Copying column in mysql by makaroni4

E302c3320cd14b02cbe237b479d7f884?size=52

Recently in my #rails I had a task to split one column into two - it was daily weather forecast and I made it day time forecast and night time. But in database there were already thousands of forecasts and logic was pretty simple - since the daily forecast was the average, night forecast was treated like the daily one where sun was switched to the moon :) So it is quite obvious the change I made was needed. More under the cut

Remove duplicates from join table by makaroni4

E302c3320cd14b02cbe237b479d7f884?size=52

I am quite sure that nowadays every #rails developer adds indexes to migrations preventing creation of duplicates and making #db queries fast. But sometimes you face old projects with a lot of duplication which is needed to be removed.

In this post I will show you some techniques to remove duplications. More under the cut