Generating Random Number In Each Row

Случайное число в каждой строке выборки.
Проблемка с SQL Server, в каждой строке выборки одно и то же значение, у PostgreSQL, MySQL, Oracle, SQLite всё нормально.
Выкручиваемся создав представление и функцию.
create view m_vw_randView
as
select rand() as random_number
go

create function getRand()
returns float
as
begin
    declare @returnValue float
	select @returnValue = random_number from m_vw_randView
    return @returnValue
end
go

select rand() [rand], dbo.getRand() [getRand], * from (values(1),(2),(3))t(c1)

rand			getRand			c1
0,63453611784667	0,96496883196861	1
0,63453611784667	0,0616408952549143	2
0,63453611784667	0,280773494594934	3
PostgreSQL v12
select *, random() from generate_series(1,3) t1(c1);
| c1  | random              |
| --- | ------------------- |
| 1   | 0.28302426362094124 |
| 2   | 0.38171190130398003 |
| 3   | 0.35524341401745474 |
MySQL v8.0
with recursive t1(c1) AS
(
  select 1
  union all
  select c1 + 1 from t1 where c1 + 1 <= 3
)
select *, rand() from t1;
| c1  | rand()             |
| --- | ------------------ |
| 1   | 0.8239167740791261 |
| 2   | 0.6890084265628909 |
| 3   | 0.9732918413107208 |
Oracle Database 11g
select level c1, dbms_random.value(0,1) random from dual connect by level <= 3
C1	RANDOM
1	0,457075791676919
2	0,885762460182671
3	0,555152770728942

Латеральное объединение (LATERAL JOIN)

Ну вот, хоть в чем-то MySQL переплюнул MariaDB и это LATERAL (на дворе осень 2020)


create table t1(id int, val int);
create table t2(id int, val int);

insert into t1(id, val)
with recursive
r1 as (
  select 1 id, (rand()*100 div 1) r 
  union all
  select r1.id+1 id, (rand()*100 div 1) r from r1 
   where r1.id<100
 ) 
  select id, r from r1
  ;
  
insert into t2(id, val)
with recursive
r1 as (
  select 1 id, (rand()*100 div 1) r 
  union all
  select r1.id+1 id, (rand()*100 div 1) r from r1 
   where r1.id<100
 ) 
  select id, r from r1 where r between 40 and 60
 ;  
 
 select t1.*, t22.* from t1
  join lateral (
  select max(t2.id) ma, min(t2.id) mi from t2 where t1.val between t2.val-10 and t2.val+10
 ) t22 on (true) -- по сути своей left join ибо "on (true)"
-- ) t22 on (t22.ma is not null) -- как костыль inner join можно получить так
 ;

Recursive CTE Postgres vs MySQL(MariaDB)

Простая задача построение дерева с динамическим количеством уровней вложенности и возможностью настраиваемой сортировки ветвей и листьев

**Schema (PostgreSQL v12)**

create table catalog
    (id int, parent int, name text, ordered int);
    
insert into catalog (id, parent, name, ordered)
    VALUES
    (1, null, 'root10', 10),
    (2, null, 'root2', 8),
    (3, null, 'root1', 1),
    (4, 3, 'child1-root1', 1),
    (5, 3, 'child3-root1', 3),
    (6, 3, 'child2-root1', 2),
    (7, 1, 'child3-root10', 30),
    (8, 1, 'child2-root10', 20),
    (9, 1, 'child1-root10', 10),
    (7, 2, 'child3-root2', 30),
    (8, 2, 'child1-root2', 10),
    (9, 2, 'child2-root2', 20)
    ;
---

**Query #1**

    with recursive
    r as (
    select c1.*, 1 lvl, (row_number() over (partition by parent order by ordered))::text path from catalog c1 where c1.parent is null
      union all
    select c2.*, r.lvl + 1 lvl, concat(r.path, row_number() over (partition by c2.parent order by c2.ordered)) path  from catalog c2, r where c2.parent = r.id
    )
    select * from r
     order by path;

| id  | parent | name          | ordered | lvl | path |
| --- | ------ | ------------- | ------- | --- | ---- |
| 3   |        | root1         | 1       | 1   | 1    |
| 4   | 3      | child1-root1  | 1       | 2   | 11   |
| 6   | 3      | child2-root1  | 2       | 2   | 12   |
| 5   | 3      | child3-root1  | 3       | 2   | 13   |
| 2   |        | root2         | 8       | 1   | 2    |
| 8   | 2      | child1-root2  | 10      | 2   | 21   |
| 9   | 2      | child2-root2  | 20      | 2   | 22   |
| 7   | 2      | child3-root2  | 30      | 2   | 23   |
| 1   |        | root10        | 10      | 1   | 3    |
| 9   | 1      | child1-root10 | 10      | 2   | 31   |
| 8   | 1      | child2-root10 | 20      | 2   | 32   |
| 7   | 1      | child3-root10 | 30      | 2   | 33   |

С Postgres все ОК, в Oracle и MS SQL Server тоже все работает.

MySQL и MariaDB заявляют о поддержке рекурсивных обобщенных табличных выражений, но похоже не полностью.


with recursive
r as (
select c1.*, 1 lvl, convert(row_number() over (partition by parent order by ordered), char) path from catalog c1 where c1.parent is null
  union all
select c2.*, r.lvl + 1 lvl, concat(r.path, convert(row_number() over (partition by c2.parent order by c2.ordered), char)) path  from catalog c2, r where c2.parent = r.id
)
select * from r order by path

MySQL 8.0.12
Query Error: Error: UNKNOWN_CODE_PLEASE_REPORT: Recursive Common Table Expression 'r' can contain neither aggregation nor window functions in recursive query block
---
MariaDB 10.4.12-MariaDB
ER_NOT_STANDARD_COMPLIANT_RECURSIVE (conn=2466, no: 4008, SQLState: HY000) Restrictions imposed on recursive definitions are violated for table 'r' sql: with recursive r as ( select c1.*, 1 lvl, convert(row_number() over (partition by parent order by ordered), char) path from catalog c1 where c1.parent is null union all select c2.*, r.lvl + 1 lvl, concat(r.path, convert(row_number() over (partition by c2.parent order by c2.ordered), char)) path from catalog c2, r where c2.parent = r.id ) select * from r order by path - parameters:[]

А вот если из второго запроса убрать оконную функцию row_number, и немного изменив логику формирования пути сортировки, то получим нужный результат.


**Schema (MySQL v8.0)**

    create table catalog
    (id int, parent int, name text, ordered int);
    
    insert into catalog
    (id, parent, name, ordered)
    VALUES
    (1, null, 'root10', 10),
    (2, null, 'root2', 8),
    (3, null, 'root1', 1),
    (4, 3, 'child1-root1', 1),
    (5, 3, 'child3-root1', 3),
    (6, 3, 'child2-root1', 2),
    (7, 1, 'child3-root10', 30),
    (8, 1, 'child2-root10', 20),
    (9, 1, 'child1-root10', 10),
    (10, 2, 'child3-root2', 30),
    (11, 2, 'child1-root2', 10),
    (12, 2, 'child2-root2', 20),
    (13, 8, 'child1-child2-root10', 2),
    (14, 8, 'child2-child2-root10', 3)
    ;    

---

**Query #1**

    with recursive
    r as (
    select c1.*, 1 lvl, convert(row_number() over (partition by parent order by ordered), char) path from catalog c1 where c1.parent is null
      union all
    select c2.*, r.lvl + 1 lvl, concat(r.path, convert(c2.ordered, char)) path  from catalog c2, r where c2.parent = r.id
    )
    select * from r
     order by path;

| id  | parent | name                 | ordered | lvl | path |
| --- | ------ | -------------------- | ------- | --- | ---- |
| 3   |        | root1                | 1       | 1   | 1    |
| 4   | 3      | child1-root1         | 1       | 2   | 11   |
| 6   | 3      | child2-root1         | 2       | 2   | 12   |
| 5   | 3      | child3-root1         | 3       | 2   | 13   |
| 2   |        | root2                | 8       | 1   | 2    |
| 11  | 2      | child1-root2         | 10      | 2   | 210  |
| 12  | 2      | child2-root2         | 20      | 2   | 220  |
| 10  | 2      | child3-root2         | 30      | 2   | 230  |
| 1   |        | root10               | 10      | 1   | 3    |
| 9   | 1      | child1-root10        | 10      | 2   | 310  |
| 8   | 1      | child2-root10        | 20      | 2   | 320  |
| 13  | 8      | child1-child2-root10 | 2       | 3   | 3202 |
| 14  | 8      | child2-child2-root10 | 3       | 3   | 3203 |
| 7   | 1      | child3-root10        | 30      | 2   | 330  |

UPDATE

Ан, нет, проглядел я, без использования оконной функции во втором подзапросе правильный «путь» не получить. В примере все хорошо, так как данные удачно легли.

А если, так, то беда


**Schema (MySQL v8.0)**

    create table catalog
    (id int, parent int, name text, ordered int);
    
    insert into catalog
    (id, parent, name, ordered)
    VALUES
    (1, null, 'root10', 10),
    (2, null, 'root2', 8),
    (3, null, 'root1', 1),
    (4, 3, 'child1-root1', 1),
    (5, 3, 'child3-root1', 10),
    (6, 3, 'child2-root1', 2),
    (7, 1, 'child3-root10', 30),
    (8, 1, 'child2-root10', 20),
    (9, 1, 'child1-root10', 10),
    (7, 2, 'child3-root2', 30),
    (8, 2, 'child1-root2', 10),
    (9, 2, 'child2-root2', 20)
    ;
    
    

---

**Query #1**

    with recursive
    r as (
    select c1.*, 1 lvl, convert(row_number() over (partition by parent order by ordered), char) path from catalog c1 where c1.parent is null  
      union all
    select c2.*, r.lvl + 1 lvl, concat(r.path, convert(c2.ordered, char)) path  from catalog c2, r where c2.parent = r.id
    )
    select * from r
     order by path;

| id  | parent | name          | ordered | lvl | path |
| --- | ------ | ------------- | ------- | --- | ---- |
| 3   |        | root1         | 1       | 1   | 1    |
| 4   | 3      | child1-root1  | 1       | 2   | 11   |
| 5   | 3      | child3-root1  | 10      | 2   | 110  |
| 6   | 3      | child2-root1  | 2       | 2   | 12   |
| 2   |        | root2         | 8       | 1   | 2    |
| 8   | 2      | child1-root2  | 10      | 2   | 210  |
| 9   | 2      | child2-root2  | 20      | 2   | 220  |
| 7   | 2      | child3-root2  | 30      | 2   | 230  |
| 1   |        | root10        | 10      | 1   | 3    |
| 9   | 1      | child1-root10 | 10      | 2   | 310  |
| 8   | 1      | child2-root10 | 20      | 2   | 320  |
| 7   | 1      | child3-root10 | 30      | 2   | 330  |

---

UPDATE 2

Есть выход, но нужно будет немного усложнить механизм сохранения значений сортировки в разрезе ветви одного уровня. Например, добиваем «0»-ми до максимального кол-ва знаков в числе по ветви одного уровня


**Schema (MySQL v8.0)**

    create table catalog
    (id int, parent int, name text, ordered varchar(10));
    
    insert into catalog
    (id, parent, name, ordered)
    VALUES
    (1, null, 'root10', '10'),
    (2, null, 'root2', '08'),
    (3, null, 'root1', '01'),
    (4, 3, 'child1-root1', '01'),
    (5, 3, 'child3-root1', '10'),
    (6, 3, 'child2-root1', '02'),
    (7, 1, 'child3-root10', '30'),
    (8, 1, 'child2-root10', '20'),
    (9, 1, 'child1-root10', '10'),
    (7, 2, 'child3-root2', '30'),
    (8, 2, 'child1-root2', '10'),
    (9, 2, 'child2-root2', '20')
    ;
    
    

---

**Query #1**

    with recursive
    r as (
    select c1.*, 1 lvl, c1.ordered path from catalog c1 where c1.parent is null  
      union all
    select c2.*, r.lvl + 1 lvl, concat(r.path, c2.ordered) path  from catalog c2, r where c2.parent = r.id
    )
    select * from r
     order by path;

| id  | parent | name          | ordered | lvl | path |
| --- | ------ | ------------- | ------- | --- | ---- |
| 3   |        | root1         | 01      | 1   | 01   |
| 4   | 3      | child1-root1  | 01      | 2   | 0101 |
| 6   | 3      | child2-root1  | 02      | 2   | 0102 |
| 5   | 3      | child3-root1  | 10      | 2   | 0110 |
| 2   |        | root2         | 08      | 1   | 08   |
| 8   | 2      | child1-root2  | 10      | 2   | 0810 |
| 9   | 2      | child2-root2  | 20      | 2   | 0820 |
| 7   | 2      | child3-root2  | 30      | 2   | 0830 |
| 1   |        | root10        | 10      | 1   | 10   |
| 9   | 1      | child1-root10 | 10      | 2   | 1010 |
| 8   | 1      | child2-root10 | 20      | 2   | 1020 |
| 7   | 1      | child3-root10 | 30      | 2   | 1030 |

MariaDB update records or cteate table from CTE

CTE в марии это хорошо, но как сохранить результаты. В документации не нашел, аналогия с SQL Server не работает.

    update load0 l
        join 
         ( with t1 as (
             select row_number() over (partition by col1, substring(col2, 1, 15), col3,
              regexp_replace(regexp_substr(col4, '^[^|]*'),'[^а-я]',''),
               col6, regexp_replace(regexp_substr(col7, '^[^|]*'),'(http)*(s)*(www.)*[\/\:]',''), lower(col8), col9, col10 order by col11_id, col12_id) rn, l0.* from load0 l0
            )
                select * from t1
          ) t on (l.id=t.id)
            set l.client_number = t.rn
;
            create table col12 
            with t1 as (
            select col11, col12, count(*) cnt from load0
             group by col11, col12
            ), t2 as (
                select *, dense_rank() over (order by col11 desc) col11_id, dense_rank() over (partition by col11 order by col12) col12_id  from t1
            )
            , t3 as (
                select distinct col11, col11_id from t2
            )
                select col11_id, col12_id, col12 from t2
;

MariaDB(MySQL) recursive CTE and Window Functions

Ну да, конечно, в MySQL, нет ничего подобного и уже наверное не будет, иначе Oracle сделал бы этот функционал много лет назад. Ставим вместо MySQL MariaDB и получаем обобщенные выражения и рекурсивные в том числе, оконные функции, обработку строки регекспом и тд. PHP и не заметит подмены. Для примера, решаем задачу, генерация «чистая», никаких лишних строк.

/*
* календарь на месяц, по умолчанию текущий - "select 0 monthShift from dual"
* начало недели с понедельника
* с отображением дней предыдущего и следующего месяцев в неполных неделях
*/
with recursive gen50 as (
 select -10 rn 
 union all
 select rn+1 from gen50 where rn<40 /* генерируем 50 строк */
), calend as (
 select *, substr(dds, -2) ddH from (
 select *, min(ttrimTop) over () trimTop, max(ttrimBottom) over () trimBottom from (
 select *,  first_value(rn) over (partition by curMonth order by weekdayIdx asc, rn desc) ttrimTop, 
            first_value(rn) over (partition by curMonth order by weekdayIdx desc, rn asc) ttrimBottom,
            min(case when ld = dds and weekdayIdx = 6 then rn else 99 end) over() ttrimBottom2, 
            max(case when fd = dds and weekdayIdx = 0 then rn else -99 end) over() ttrimTop2
 from (
 select *, case when dds < fd then -1 when dds > ld then 0 else 1 end curMonth, dds = current_date curDay, weekday(dds) weekdayIdx from ( 
 select *, dd.fd + interval (gen50.rn) day as dds from gen50 
  cross join (select last_day(now() + interval monthShift month ) ld, last_day(now() + interval monthShift month  ) + interval 1 day - interval 1 month as fd from (select 0 monthShift from dual) dd) dd
  ) tt ) tt ) tt ) tt
  where rn between trimTop and trimBottom /* обрезать по начальной и конечной неделе */
   and rn between ttrimTop2 and ttrimBottom2 /* если число месяца на границе недели, то нужно "отрезать" раньше */
)
select * from calend
 order by rn;

+----+------------+------------+------------+----------+--------+------------+----------+-------------+--------------+-----------+---------+------------+------+
| rn | ld         | fd         | dds        | curMonth | curDay | weekdayIdx | ttrimTop | ttrimBottom | ttrimBottom2 | ttrimTop2 | trimTop | trimBottom | ddH  |
+----+------------+------------+------------+----------+--------+------------+----------+-------------+--------------+-----------+---------+------------+------+
| -6 | 2017-10-31 | 2017-10-01 | 2017-09-25 |       -1 |      0 |          0 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 25   |
| -5 | 2017-10-31 | 2017-10-01 | 2017-09-26 |       -1 |      0 |          1 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 26   |
| -4 | 2017-10-31 | 2017-10-01 | 2017-09-27 |       -1 |      0 |          2 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 27   |
| -3 | 2017-10-31 | 2017-10-01 | 2017-09-28 |       -1 |      0 |          3 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 28   |
| -2 | 2017-10-31 | 2017-10-01 | 2017-09-29 |       -1 |      0 |          4 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 29   |
| -1 | 2017-10-31 | 2017-10-01 | 2017-09-30 |       -1 |      0 |          5 |       -6 |          -7 |           99 |       -99 |      -6 |         35 | 30   |
|  0 | 2017-10-31 | 2017-10-01 | 2017-10-01 |        1 |      0 |          6 |       29 |           0 |           99 |       -99 |      -6 |         35 | 01   |
|  1 | 2017-10-31 | 2017-10-01 | 2017-10-02 |        1 |      0 |          0 |       29 |           0 |           99 |       -99 |      -6 |         35 | 02   |
|  2 | 2017-10-31 | 2017-10-01 | 2017-10-03 |        1 |      0 |          1 |       29 |           0 |           99 |       -99 |      -6 |         35 | 03   |
|  3 | 2017-10-31 | 2017-10-01 | 2017-10-04 |        1 |      0 |          2 |       29 |           0 |           99 |       -99 |      -6 |         35 | 04   |
|  4 | 2017-10-31 | 2017-10-01 | 2017-10-05 |        1 |      0 |          3 |       29 |           0 |           99 |       -99 |      -6 |         35 | 05   |
|  5 | 2017-10-31 | 2017-10-01 | 2017-10-06 |        1 |      0 |          4 |       29 |           0 |           99 |       -99 |      -6 |         35 | 06   |
|  6 | 2017-10-31 | 2017-10-01 | 2017-10-07 |        1 |      0 |          5 |       29 |           0 |           99 |       -99 |      -6 |         35 | 07   |
|  7 | 2017-10-31 | 2017-10-01 | 2017-10-08 |        1 |      0 |          6 |       29 |           0 |           99 |       -99 |      -6 |         35 | 08   |
|  8 | 2017-10-31 | 2017-10-01 | 2017-10-09 |        1 |      1 |          0 |       29 |           0 |           99 |       -99 |      -6 |         35 | 09   |
|  9 | 2017-10-31 | 2017-10-01 | 2017-10-10 |        1 |      0 |          1 |       29 |           0 |           99 |       -99 |      -6 |         35 | 10   |
| 10 | 2017-10-31 | 2017-10-01 | 2017-10-11 |        1 |      0 |          2 |       29 |           0 |           99 |       -99 |      -6 |         35 | 11   |
| 11 | 2017-10-31 | 2017-10-01 | 2017-10-12 |        1 |      0 |          3 |       29 |           0 |           99 |       -99 |      -6 |         35 | 12   |
| 12 | 2017-10-31 | 2017-10-01 | 2017-10-13 |        1 |      0 |          4 |       29 |           0 |           99 |       -99 |      -6 |         35 | 13   |
| 13 | 2017-10-31 | 2017-10-01 | 2017-10-14 |        1 |      0 |          5 |       29 |           0 |           99 |       -99 |      -6 |         35 | 14   |
| 14 | 2017-10-31 | 2017-10-01 | 2017-10-15 |        1 |      0 |          6 |       29 |           0 |           99 |       -99 |      -6 |         35 | 15   |
| 15 | 2017-10-31 | 2017-10-01 | 2017-10-16 |        1 |      0 |          0 |       29 |           0 |           99 |       -99 |      -6 |         35 | 16   |
| 16 | 2017-10-31 | 2017-10-01 | 2017-10-17 |        1 |      0 |          1 |       29 |           0 |           99 |       -99 |      -6 |         35 | 17   |
| 17 | 2017-10-31 | 2017-10-01 | 2017-10-18 |        1 |      0 |          2 |       29 |           0 |           99 |       -99 |      -6 |         35 | 18   |
| 18 | 2017-10-31 | 2017-10-01 | 2017-10-19 |        1 |      0 |          3 |       29 |           0 |           99 |       -99 |      -6 |         35 | 19   |
| 19 | 2017-10-31 | 2017-10-01 | 2017-10-20 |        1 |      0 |          4 |       29 |           0 |           99 |       -99 |      -6 |         35 | 20   |
| 20 | 2017-10-31 | 2017-10-01 | 2017-10-21 |        1 |      0 |          5 |       29 |           0 |           99 |       -99 |      -6 |         35 | 21   |
| 21 | 2017-10-31 | 2017-10-01 | 2017-10-22 |        1 |      0 |          6 |       29 |           0 |           99 |       -99 |      -6 |         35 | 22   |
| 22 | 2017-10-31 | 2017-10-01 | 2017-10-23 |        1 |      0 |          0 |       29 |           0 |           99 |       -99 |      -6 |         35 | 23   |
| 23 | 2017-10-31 | 2017-10-01 | 2017-10-24 |        1 |      0 |          1 |       29 |           0 |           99 |       -99 |      -6 |         35 | 24   |
| 24 | 2017-10-31 | 2017-10-01 | 2017-10-25 |        1 |      0 |          2 |       29 |           0 |           99 |       -99 |      -6 |         35 | 25   |
| 25 | 2017-10-31 | 2017-10-01 | 2017-10-26 |        1 |      0 |          3 |       29 |           0 |           99 |       -99 |      -6 |         35 | 26   |
| 26 | 2017-10-31 | 2017-10-01 | 2017-10-27 |        1 |      0 |          4 |       29 |           0 |           99 |       -99 |      -6 |         35 | 27   |
| 27 | 2017-10-31 | 2017-10-01 | 2017-10-28 |        1 |      0 |          5 |       29 |           0 |           99 |       -99 |      -6 |         35 | 28   |
| 28 | 2017-10-31 | 2017-10-01 | 2017-10-29 |        1 |      0 |          6 |       29 |           0 |           99 |       -99 |      -6 |         35 | 29   |
| 29 | 2017-10-31 | 2017-10-01 | 2017-10-30 |        1 |      0 |          0 |       29 |           0 |           99 |       -99 |      -6 |         35 | 30   |
| 30 | 2017-10-31 | 2017-10-01 | 2017-10-31 |        1 |      0 |          1 |       29 |           0 |           99 |       -99 |      -6 |         35 | 31   |
| 31 | 2017-10-31 | 2017-10-01 | 2017-11-01 |        0 |      0 |          2 |       36 |          35 |           99 |       -99 |      -6 |         35 | 01   |
| 32 | 2017-10-31 | 2017-10-01 | 2017-11-02 |        0 |      0 |          3 |       36 |          35 |           99 |       -99 |      -6 |         35 | 02   |
| 33 | 2017-10-31 | 2017-10-01 | 2017-11-03 |        0 |      0 |          4 |       36 |          35 |           99 |       -99 |      -6 |         35 | 03   |
| 34 | 2017-10-31 | 2017-10-01 | 2017-11-04 |        0 |      0 |          5 |       36 |          35 |           99 |       -99 |      -6 |         35 | 04   |
| 35 | 2017-10-31 | 2017-10-01 | 2017-11-05 |        0 |      0 |          6 |       36 |          35 |           99 |       -99 |      -6 |         35 | 05   |
+----+------------+------------+------------+----------+--------+------------+----------+-------------+--------------+-----------+---------+------------+------+
42 rows in set (0.00 sec)
Добавим к CTE еще пару запросов, что-то вроде этого,

 , aa as (
 SELECT dd.dds dds2, GROUP_CONCAT(CONCAT_WS(CHAR(9), a.id, a.publish_date IS NOT NULL, a.publish_date IS NULL AND (a.for_date < now()), a.publish_date IS NULL AND (a.for_date > now()) ) SEPARATOR '\n ') aas
    FROM dd 
     JOIN articles a WHERE a.deleted IS NULL
     AND a.for_date BETWEEN dds AND dds + INTERVAL 1 DAY - INTERVAL 1 SECOND
      AND (a.responsible = $user OR $user = -1)
     GROUP BY dd.dds
 )
и в итоге, будет как-то так. calend

Век живи, век учись, вариант выделения дробной части

Задача, положить дробное число раздельно в два столбца, воспользуемся оператором взятия остатка от деления %, это Лёха подсказал
drop table #test17;

create table #test17(c1 int, c2 decimal(10,10) /*отдадим все 10 знаков на хранение дробной части*/);

insert into #test17
 select c,c from (select 12.34567 c) tt;
-- для MSSQL Arithmetic overflow error converting numeric to data type numeric.
-- The statement has been terminated.
-- или в MySQL Data truncation: Out of range value for column 'c2' at row 1

insert into #test17
 select c,c%1 from (select 12.34567 c) tt;

select * from #test17;

c1	c2
12	0.3456700000

для Оракла функция mod(c, d)