Skip to content

Tag: sql

Using a MIN MAX query over a DISTINCT one in MySQL?

With my new job, I am needing to use SQL more and more. As I need to remove duplicate columns I thought that using a DISTINCT statement would be best. However, I was indicated that a MIN MAX statement might be more suited. As I am still fairly new to SQL I was wondering if I could get some advice

SQL Month YTD and prior YTD data

I am trying to write a query to return Month-To-Date data for the Current Year, and also the Previous Year. Meaning I need data from 2017-07-01 to 2017-07-26 and also 2016-07-01 to 2016-07-26. However, my query currently returns data from 2017-07-01 to 2017-07-26 (correctly) but also returns data from 2016-07…

Unique constraint keys not invoked when a field is NULL

I have a table named AQI, with the following column Only one unique value of date, pollutant_code, source_id and time are able to exist, so I defined a unique constraint source_code_time_uq. And the upsert query like The upsert method work normally when all the field available, but when I put NULL in the time…

Sql query to Count Total Consecutive Years from latest year

I have a table Temp: I want to calculate the total consecutive years starting from the most recent Year. Result should look like this: Answer e.g. for ID=1: As long as there’s no gap, both sequences increase the same. Now check for equal sequences and count the rows: Edit: Based on your year zero commen…