SELECT t1.num, t2.num, t2.num-t1.num
FROM
(
SELECT num,
ROW_NUMBER() OVER(ORDER BY num) AS rn
FROM table
) AS t1
LEFT JOIN
(
SELECT num,
ROW_NUMBER() OVER(ORDER BY num) AS rn
FROM table
) AS t2
ON t1.rn=t2.rn-1
SELECT * FROM `wallets`
LEFT JOIN `wallet_address`
ON (wallets.id = wallet_address.wallet_id)
WHERE EXISTS (SELECT user_id
FROM wallet_address AS t1
WHERE t1.user_id=1)
DECLARE @minValue INT, @maxValue INT;
IF (@v1 < @v2)
BEGIN
SET @minValue = @v1;
SET @maxValue = @v2;
END
ELSE
BEGIN
SET @minValue = @v2;
SET @maxValue = @v1;
END
SELECT *
FROM table
WHERE v BETWEEN @minValue AND @maxValue
SELECT dt,
SUM(mx) AS sm
FROM
(
-- Максимальные значения по каждому каналу за каждый день
SELECT channel_id,
CAST(created_at AS DATE) AS dt,
MAX(participants_count) AS mx
FROM table
WHERE created_at BETWEEN @dateStart AND @dateFinish
GROUP BY channel_id, CAST(created_at AS DATE)
) AS t1
GROUP BY dt
SELECT COALESCE(clients.field1, drivers.field1),
COALESCE(clients.field2, drivers.field2)
...
FROM table
LEFT JOIN clients
ON table.client_id=clients.id
LEFT JOIN drivers
ON table.driver_id=drivers.id