Get previous month and this month sums in one query - mysql

I am trying to merge two queries into one
My data:
CREATE TABLE `przychody` (
`id_przychodu` bigint(20) NOT NULL,
`id_rejonu` bigint(20) NOT NULL,
`fk_kontrahent` bigint(20) NOT NULL,
`dodal` bigint(20) NOT NULL,
`wartosc` decimal(10,2) NOT NULL,
`netto` decimal(10,2) NOT NULL,
`numer` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`z_dnia` date NOT NULL,
`dodano` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uwagi` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`vat_lacznie` decimal(11,2) NOT NULL,
`sprzedano` date NOT NULL,
`termin_platnosci` date NOT NULL,
`ilosc_dni` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `przychody` (`id_przychodu`, `id_rejonu`, `fk_kontrahent`, `dodal`, `wartosc`, `netto`, `numer`, `z_dnia`, `dodano`, `uwagi`, `vat_lacznie`, `sprzedano`, `termin_platnosci`, `ilosc_dni`) VALUES
(48, 1, 189, 3, '172.20', '140.00', '1/KOM/10/17', '2017-10-03', '2017-10-03 16:13:21', '', '32.20', '2017-10-03', '2017-11-02', 30),
(49, 1, 189, 3, '422.44', '422.44', '2/KOM/10/17', '2017-10-03', '2017-10-03 16:15:35', 'M', '0.00', '2017-10-03', '2017-11-02', 30),
(50, 3, 216, 3, '543.50', '441.87', '22/KOM/09/17', '2017-09-29', '2017-10-04 13:02:23', '', '101.63', '2017-09-29', '2017-10-18', 14),
(51, 1, 4, 3, '625.00', '625.00', '3/KOM/10/17', '2017-10-09', '2017-10-09 16:38:27', 'D 2', '0.00', '2017-10-09', '2017-11-08', 30),
(52, 3, 441, 3, '7700.00', '7700.00', '4/KOM/10/17', '2017-10-10', '2017-10-10 17:40:51', 'B17', '0.00', '2017-10-06', '2017-10-24', 14),
(53, 2, 189, 3, '553.50', '450.00', '5/KOM/10/17', '2017-10-11', '2017-10-11 17:42:50', 'BiCHER', '103.50', '2017-10-11', '2017-11-10', 30),
(54, 3, 3, 3, '3286.06', '2671.60', '6/KOM/10/17', '2017-10-17', '2017-10-17 10:50:16', 'Int', '614.46', '2017-10-17', '2017-11-16', 30),
(55, 3, 3, 3, '5388.50', '4380.90', '7/KOM/10/17', '2017-10-17', '2017-10-17 10:51:13', 'Inska', '1007.60', '2017-10-17', '2017-11-16', 30),
(56, 3, 3, 3, '1205.40', '980.00', '8/KOM/10/17', '2017-10-17', '2017-10-17 10:52:20', 'Insa', '225.40', '2017-10-17', '2017-11-16', 30),
(57, 3, 3, 3, '1033.20', '840.00', '9/KOM/10/17', '2017-10-17', '2017-10-17 10:53:10', 'Inka', '193.20', '2017-10-17', '2017-11-16', 30),
(58, 2, 437, 3, '64.80', '60.00', '10/KOM/10/17', '2017-10-17', '2017-10-17 13:29:00', 'Nume9', '4.80', '2017-10-17', '2017-11-16', 30),
(59, 2, 406, 3, '193.21', '178.90', '11/KOM/10/17', '2017-10-17', '2017-10-17 14:23:34', '', '14.31', '2017-10-17', '2017-11-16', 30),
(60, 3, 441, 3, '3575.00', '3575.00', '12/KOM/10/17', '2017-10-23', '2017-10-23 10:43:36', 'Wyk10.', '0.00', '2017-10-23', '2017-11-06', 14),
(61, 3, 4, 3, '2000.00', '2000.00', '13/KOM/10/17', '2017-10-24', '2017-10-24 15:32:23', 'Dot./16', '0.00', '2017-10-24', '2017-11-23', 30),
(62, 3, 147, 3, '8000.00', '8000.00', '14/KOM/10/17', '2017-10-24', '2017-10-24 18:29:19', 'Dota 16', '0.00', '2017-10-24', '2017-10-31', 7),
(63, 1, 189, 3, '1395.00', '1395.00', '15/KOM/10/17', '2017-10-25', '2017-10-25 13:43:50', 'Pio&M', '0.00', '2017-10-25', '2017-11-24', 30),
(64, 4, 590, 3, '775.43', '775.43', '18/KOM/08/17', '2017-08-31', '2017-10-27 12:55:31', '', '0.00', '2017-08-31', '2017-11-10', 14),
(65, 4, 590, 3, '775.43', '775.43', '23/KOM/09/17', '2017-09-29', '2017-10-27 12:56:40', '', '0.00', '2017-09-29', '2017-11-10', 14),
(66, 1, 442, 3, '282.93', '232.46', '16/KOM/10/17', '2017-10-31', '2017-10-31 12:27:55', 'Uw 6', '50.47', '2017-10-31', '2017-11-30', 30),
(68, 1, 189, 3, '399.75', '325.00', '17/KOM/10/17', '2017-10-31', '2017-10-31 12:37:26', 'Wrora', '74.75', '2017-10-31', '2017-11-30', 30),
(69, 1, 413, 3, '469.62', '434.84', '18/KOM/10/17', '2017-10-31', '2017-10-31 12:41:07', 'KsaC', '34.78', '2017-10-31', '2017-11-14', 14),
(70, 2, 111, 3, '368.87', '299.90', '19/KOM/10/17', '2017-10-31', '2017-10-31 12:46:50', '', '68.97', '2017-10-31', '2017-11-30', 30),
(71, 3, 441, 3, '2178.00', '2178.00', '1/KOM/11/17', '2017-11-02', '2017-11-02 15:37:04', '16.10-20.10.2017', '0.00', '2017-11-02', '2017-11-16', 14),
(72, 3, 441, 3, '8800.00', '8800.00', '2/KOM/11/17', '2017-11-02', '2017-11-02 15:40:11', '23.10 - 27.11.2017', '0.00', '2017-11-02', '2017-11-16', 14),
(73, 1, 413, 3, '218.19', '202.03', '20/KOM/10/17', '2017-10-31', '2017-11-06 15:55:48', 'Ksa10', '16.16', '2017-10-31', '2017-11-20', 14),
(74, 1, 132, 3, '870.47', '707.70', '21/KOM/10/17', '2017-10-31', '2017-11-06 16:22:05', '', '162.77', '2017-10-31', '2017-11-14', 14),
(75, 1, 608, 3, '413.28', '336.00', '22/KOM/10/17', '2017-10-31', '2017-11-07 13:11:58', 'Łód', '77.28', '2017-10-31', '2017-11-14', 14),
(77, 1, 146, 3, '49.20', '40.00', '23/KOM/10/17', '2017-10-31', '2017-11-07 13:26:42', 'Łź 4', '9.20', '2017-10-31', '2017-11-21', 14),
(78, 1, 590, 3, '775.43', '775.43', '24/KOM/10/17', '2017-10-31', '2017-11-07 13:31:24', '', '0.00', '2017-10-31', '2017-11-14', 14),
(79, 2, 111, 3, '2460.00', '2000.00', '25/KOM/10/17', '2017-10-31', '2017-11-07 13:39:09', '', '460.00', '2017-10-31', '2017-11-21', 14),
(81, 2, 323, 3, '3095.24', '2865.97', '26/KOM/10/17', '2017-10-31', '2017-11-07 13:41:32', '', '229.27', '2017-10-31', '2017-11-21', 14),
(82, 2, 323, 3, '1103.98', '1022.22', '27/KOM/10/17', '2017-10-31', '2017-11-07 13:54:51', '', '81.76', '2017-10-31', '2017-11-21', 14),
(83, 2, 216, 3, '2827.40', '2298.70', '28/KOM/10/17', '2017-11-07', '2017-11-07 14:16:09', '', '528.70', '2017-10-31', '2017-11-21', 14),
(84, 2, 216, 3, '4737.11', '3851.31', '29/KOM/10/17', '2017-11-07', '2017-11-07 14:18:23', '', '885.80', '2017-10-31', '2017-11-21', 14),
(85, 2, 216, 3, '1966.05', '1598.42', '30/KOM/10/17', '2017-11-07', '2017-11-07 14:36:30', '', '367.63', '2017-10-31', '2017-11-21', 14),
(86, 2, 189, 3, '615.00', '500.00', '3/KOM/11/17', '2017-11-08', '2017-11-08 10:56:24', 'Aer', '115.00', '2017-11-08', '2017-12-08', 30);
My Query attempt, I am not sure if this is the proper attempt, the result seems to be quite off if you run each query independent.
SELECT
sum(przychody.netto) as last_month,
sum(Query1.netto) AS this_month
FROM
przychody,
(SELECT
*
FROM
przychody
WHERE
przychody.sprzedano >= Last_Day(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH AND
przychody.sprzedano < Last_Day(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
) Query1
WHERE
przychody.sprzedano >= Last_Day(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
SQLFiddle playground : http://sqlfiddle.com/#!9/e18a49/1
Preferred output
Last_month | Current_month
SUM() | SUM()

You can get the sum of current and previous month in single query by using case in sum()
SELECT
SUM( CASE WHEN sprzedano >= DATE_FORMAT(NOW() ,'%Y-%m-01') AND sprzedano <= LAST_DAY(NOW()) THEN netto ELSE 0 END) this_month,
SUM( CASE WHEN sprzedano >= DATE_FORMAT(NOW() - INTERVAL 1 MONTH ,'%Y-%m-01') AND sprzedano <= LAST_DAY(NOW() - INTERVAL 1 MONTH) THEN netto ELSE 0 END) last_month
FROM `przychody`
Demo

Related

query in eloquent model

I´m traying to do this query in eloquent.
SELECT llamada_estado.desc, count(id_estado)
FROM estadisticas INNER JOIN llamada_estado ON estadisticas.id_estado = llamada_estado.id
WHERE updated_at BETWEEN '2021-11-01' AND '2021-11-30'
GROUP BY estadisticas.id_estado
i´m traying this:
$query2=Estadisticas::query()->whereDate('updated_at', '<=', $toDate)
->whereDate('updated_at', '>=', $fromDate)
->join('llamada_estado', 'estadisticas.id_estado', '=', 'llamada_estado.id')
->groupBy('estadisticas.id_estado')
->get();
but always return that x column it´s not included in group by, but in phpMyAdmin i´m doing this query:
SELECT llamada_estado.desc, count(id_estado)
FROM estadisticas
INNER JOIN llamada_estado ON estadisticas.id_estado = llamada_estado.id
WHERE updated_at BETWEEN '2021-11-01' AND '2021-11-30'
GROUP BY estadisticas.id_estado
and this it´s my result:
desc, count(id_estado)
NUEVA 399
PENDIENTE 104
ANULADA 117
CONFIRMADA 50
PASADA A COMERCIALES 175
MAYOR 196
ERRONEO NO EXISTE 649
AUSENTE 681
and i need this result in eloquent.
my tables it´s:
my table call status
CREATE TABLE `llamada_estado` (
`id` int(10) UNSIGNED NOT NULL,
`nombre` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
`desc` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`clase_span` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`clase` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`hex` varchar(7) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `llamada_estado`
--
INSERT INTO `llamada_estado` (`id`, `nombre`, `desc`, `clase_span`, `clase`, `hex`) VALUES
(1, 'nueva', 'NUEVA', 'badge bg-primary text-white', 'primary', '#5c80d1'),
(2, 'pendiente', 'PENDIENTE', 'badge bg-warning text-white', 'warning', '#f3b760'),
(3, 'anulada', 'ANULADA', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(4, 'confirmada', 'CONFIRMADA', 'badge bg-success text-white', 'success', '#46c37b'),
(5, 'comercializada', 'PASADA A COMERCIALES', 'badge bg-info text-white', 'info', '#70b9eb'),
(6, 'confirmada-ausente', 'CONFIRMADA/AUSENTE', 'badge bg-secondary text-white', 'secondary', '#6c757d'),
(7, 'confirmada-anulada', 'CONFIRMADA/ANULADA', 'badge bg-dark text-white', 'dark', '#343a40'),
(8, 'MAYOR', 'MAYOR', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(9, 'Erroneo-no existe', 'ERRONEO NO EXISTE', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(10, 'Robinson', 'ROBINSON', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(11, 'ausente', 'AUSENTE', 'badge bg-warning text-white', 'warning', '#f3b760');
my table statistics
--
-- Estructura de tabla para la tabla `estadisticas`
--
CREATE TABLE `estadisticas` (
`id` int(10) UNSIGNED NOT NULL,
`id_empleado` int(10) UNSIGNED NOT NULL,
`id_llamada` int(10) UNSIGNED NOT NULL,
`id_estado` int(10) UNSIGNED NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `estadisticas`
--
INSERT INTO `estadisticas` (`id`, `id_empleado`, `id_llamada`, `id_estado`, `created_at`, `updated_at`) VALUES
(72, 9, 89008, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(75, 9, 56617, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(77, 9, 78359, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(80, 9, 70361, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(87, 9, 89023, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(89, 9, 84052, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(90, 9, 89026, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(92, 9, 89028, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(108, 23, 89044, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(109, 23, 89045, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(112, 23, 89048, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(124, 23, 56496, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(126, 23, 89062, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(129, 23, 89065, 1, '2021-08-26 06:50:13', '2021-08-26 06:50:13'),
(130, 23, 89066, 1, '2021-08-26 06:50:13', '2021-08-26 06:50:13'),
(142, 24, 89078, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(144, 24, 55175, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(146, 24, 58093, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(149, 24, 72171, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(152, 24, 89088, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(155, 24, 89091, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(156, 24, 89092, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(161, 24, 73468, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(164, 24, 89100, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(165, 24, 89101, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(193, 26, 56101, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(195, 26, 89131, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(196, 26, 83856, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(197, 26, 78782, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(225, 11, 89161, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(227, 11, 89163, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(229, 11, 89165, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(231, 11, 85394, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(233, 11, 89169, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(236, 11, 60645, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(242, 11, 84919, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(246, 11, 59005, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(263, 11, 68628, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(264, 11, 89200, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(265, 11, 89201, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(268, 11, 89204, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(269, 11, 89205, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
My question is, how i can to do this query in eloquent?
Thanks for help and read
Try this
$query2=Estadisticas::query()->whereDate('updated_at', '<=', $toDate.' 23:59:59')
->whereDate('updated_at', '>=', $fromDate.' 00:00:00')

Mysql query to increase counter or marker by one for every empty record..?

I have a table structure as shown below, with columns id, number. I want to add column counter with desired output given below..like 1,1,1,1 then counter needs to be incremented by 1 on every empty row.
So for next rows it should be 2,2,2,2... until next empty rows, and so on. How to write query for this??
Any help would be appreciated.. thanks in advance..
Table structure:
CREATE TABLE `stack`
(
`id` int(11) NOT NULL auto_increment,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=300 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `number`) VALUES
(1, 75201),
(2, 55008),
(3, 55007),
(4, 75222),
(5, 0),
(6, 74992),
(7, 14553),
(8, 54582),
(9, 54581),
(10, 74991),
(11, 14554),
(12, 0),
(13, 71413),
(14, 71414),
(15, 71415),
(16, 71416),
(17, 0),
(18, 59823),
(19, 59824),
(20, 59821),
(21, 59825),
(22, 59826),
(23, 0),
(24, 58220),
(25, 58702),
(26, 18247),
(27, 51753),
(28, 12854),
(29, 15160),
(30, 18248),
(31, 51606),
(32, 18478),
(33, 68747),
(34, 68749),
(35, 58221),
(36, 18233),
(37, 15159),
(38, 18234),
(39, 58701),
(40, 58222),
(41, 68748),
(42, 51754),
(43, 18477),
(44, 51605),
(45, 68750),
(46, 18235),
(47, 18235),
(48, 12853),
(49, 18236),
(50, 0),
(51, 56617),
(52, 16349),
(53, 56612),
(54, 56614),
(55, 56613),
(56, 56616),
(57, 56362),
(58, 56611),
(59, 56363),
(60, 56610),
(61, 56619),
(62, 56620),
(63, 56621),
(64, 16350),
(65, 0),
(66, 64590),
(67, 64153),
(68, 64162),
(69, 64588),
(70, 64587),
(71, 64156),
(72, 64159),
(73, 64589),
(74, 0),
(75, 19152),
(76, 59425),
(77, 12959),
(78, 59426),
(79, 19151),
(80, 12960),
(81, 0),
(82, 54809),
(83, 54810),
(84, 54826),
(85, 14813),
(86, 54703),
(87, 74835),
(88, 74836),
(89, 54704),
(90, 14814),
(91, 54825),
(92, 0),
(93, 56700),
(94, 16128),
(95, 56319),
(96, 56718),
(97, 16723),
(98, 16724),
(99, 56717),
(100, 56320),
(101, 16127),
(102, 56701),
(103, 0),
(104, 56261),
(105, 22625),
(106, 12691),
(107, 16086),
(108, 12639),
(109, 12680),
(110, 22649),
(111, 12609),
(112, 12679),
(113, 12640),
(114, 66020),
(115, 16089),
(116, 17616),
(117, 12687),
(118, 66019),
(119, 16220),
(120, 12675),
(121, 12608),
(122, 16219),
(123, 16021),
(124, 22650),
(125, 12692),
(126, 12610),
(127, 7115),
(128, 56262),
(129, 16022),
(130, 12688),
(131, 22626),
(132, 22688),
(133, 12607),
(134, 16090),
(135, 12676),
(136, 16085),
(137, 17615),
(138, 12687),
(139, 22687),
(140, 7116),
(141, 0),
(142, 38716),
(143, 38455),
(144, 38302),
(145, 38703),
(146, 38402),
(147, 38404),
(148, 38304),
(149, 38702),
(150, 38803),
(151, 38406),
(152, 38306),
(153, 38408),
(154, 38704),
(155, 38101),
(156, 38401),
(157, 38805),
(158, 38410),
(159, 38403),
(160, 38301),
(161, 38802),
(162, 38051),
(163, 38412),
(164, 38308),
(165, 38807),
(166, 38102),
(167, 38405),
(168, 38706),
(169, 38414),
(170, 38707),
(171, 38310),
(172, 38407),
(173, 38202),
(174, 38303),
(175, 38409),
(176, 38416),
(177, 38809),
(178, 38104),
(179, 38708),
(180, 38204),
(181, 38105),
(182, 38710),
(183, 38811),
(184, 38420),
(185, 38413),
(186, 38415),
(187, 38422),
(188, 38601),
(189, 38106),
(190, 38810),
(191, 38813),
(192, 38424),
(193, 38417),
(194, 38312),
(195, 38419),
(196, 38426),
(197, 38305),
(198, 38709),
(199, 38428),
(200, 38711),
(201, 38812),
(202, 38421),
(203, 38602),
(204, 38501),
(205, 38713),
(206, 38430),
(207, 58002),
(208, 38307),
(209, 38432),
(210, 38814),
(211, 38717),
(212, 38423),
(213, 38434),
(214, 38819),
(215, 38314),
(216, 38425),
(217, 38816),
(218, 38719),
(219, 38316),
(220, 38436),
(221, 38318),
(222, 38818),
(223, 38502),
(224, 38429),
(225, 38718),
(226, 38431),
(227, 38720),
(228, 38438),
(229, 38820),
(230, 38107),
(231, 38721),
(232, 38440),
(233, 38722),
(234, 38109),
(235, 38435),
(236, 68007),
(237, 38201),
(238, 38442),
(239, 38309),
(240, 38437),
(241, 38108),
(242, 38444),
(243, 38311),
(244, 38446),
(245, 38110),
(246, 38441),
(247, 38448),
(248, 38206),
(249, 38723),
(250, 38724),
(251, 38313),
(252, 38450),
(253, 38726),
(254, 38315),
(255, 38445),
(256, 38452),
(257, 38447),
(258, 38826),
(259, 38317),
(260, 38831),
(261, 38728),
(262, 38449),
(263, 38725),
(264, 38454),
(265, 38828),
(266, 38451),
(267, 38727),
(268, 38456),
(269, 38319),
(270, 38453),
(271, 38830),
(272, 38321),
(273, 0),
(274, 19016),
(275, 59050),
(276, 59547),
(277, 59548),
(278, 59049),
(279, 19015),
(280, 0),
(281, 52171),
(282, 52174),
(283, 52172),
(284, 52173),
(285, 0),
(286, 12343),
(287, 55713),
(288, 55749),
(289, 75718),
(290, 7525),
(291, 55750),
(292, 7526),
(293, 75722),
(294, 55751),
(295, 55714),
(296, 75717),
(297, 75721),
(298, 12344),
(299, 55752);
Desired output i want:
CREATE TABLE `stack` (
`id` int(11) NOT NULL auto_increment,
`counter` int(5) NOT NULL,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=300 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `counter`, `number`) VALUES
(1, 1, 75201),
(2, 1, 55008),
(3, 1, 55007),
(4, 1, 75222),
(5, 2, 0),
(6, 2, 74992),
(7, 2, 14553),
(8, 2, 54582),
(9, 2, 54581),
(10, 2, 74991),
(11, 2, 14554),
(12, 3, 0),
(13, 3, 71413),
(14, 3, 71414),
(15, 3, 71415),
(16, 3, 71416),
(17, 4, 0),
(18, 4, 59823),
(19, 4, 59824),
(20, 4, 59821),
(21, 4, 59825),
(22, 4, 59826),
(23, 5, 0),
(24, 5, 58220),
(25, 5, 58702),
(26, 5, 18247),
(27, 5, 51753),
(28, 5, 12854),
(29, 5, 15160),
(30, 5, 18248),
(31, 5, 51606),
(32, 5, 18478),
(33, 5, 68747),
(34, 5, 68749),
(35, 5, 58221),
(36, 5, 18233),
(37, 5, 15159),
(38, 5, 18234),
(39, 5, 58701),
(40, 5, 58222),
(41, 5, 68748),
(42, 5, 51754),
(43, 5, 18477),
(44, 5, 51605),
(45, 5, 68750),
(46, 5, 18235),
(47, 5, 18235),
(48, 5, 12853),
(49, 5, 18236),
(50, 6, 0),
(51, 6, 56617),
(52, 6, 16349),
(53, 6, 56612),
(54, 6, 56614),
(55, 6, 56613),
(56, 6, 56616),
(57, 6, 56362),
(58, 6, 56611),
(59, 6, 56363),
(60, 6, 56610),
(61, 6, 56619),
(62, 6, 56620),
(63, 6, 56621),
(64, 6, 16350),
(65, 7, 0),
(66, 7, 64590),
(67, 7, 64153),
(68, 7, 64162),
(69, 7, 64588),
(70, 7, 64587),
(71, 7, 64156),
(72, 7, 64159),
(73, 7, 64589),
(74, 8, 0),
(75, 8, 19152),
(76, 8, 59425),
(77, 8, 12959),
(78, 8, 59426),
(79, 8, 19151),
(80, 8, 12960),
(81, 9, 0),
(82, 9, 54809),
(83, 9, 54810),
(84, 9, 54826),
(85, 9, 14813),
(86, 9, 54703),
(87, 9, 74835),
(88, 9, 74836),
(89, 9, 54704),
(90, 9, 14814),
(91, 9, 54825),
(92, 10, 0),
(93, 10, 56700),
(94, 10, 16128),
(95, 10, 56319),
(96, 10, 56718),
(97, 10, 16723),
(98, 10, 16724),
(99, 10, 56717),
(100, 10, 56320),
(101, 10, 16127),
(102, 10, 56701),
(103, 11, 0),
(104, 11, 56261),
(105, 11, 22625),
(106, 11, 12691),
(107, 11, 16086),
(108, 11, 12639),
(109, 11, 12680),
(110, 11, 22649),
(111, 11, 12609),
(112, 11, 12679),
(113, 11, 12640),
(114, 11, 66020),
(115, 11, 16089),
(116, 11, 17616),
(117, 11, 12687),
(118, 11, 66019),
(119, 11, 16220),
(120, 11, 12675),
(121, 11, 12608),
(122, 11, 16219),
(123, 11, 16021),
(124, 11, 22650),
(125, 11, 12692),
(126, 11, 12610),
(127, 11, 7115),
(128, 11, 56262),
(129, 11, 16022),
(130, 11, 12688),
(131, 11, 22626),
(132, 11, 22688),
(133, 11, 12607),
(134, 11, 16090),
(135, 11, 12676),
(136, 11, 16085),
(137, 11, 17615),
(138, 11, 12687),
(139, 11, 22687),
(140, 11, 7116),
(141, 12, 0),
(142, 12, 38716),
(143, 12, 38455),
(144, 12, 38302),
(145, 12, 38703),
(146, 12, 38402),
(147, 12, 38404),
(148, 12, 38304),
(149, 12, 38702),
(150, 12, 38803),
(151, 12, 38406),
(152, 12, 38306),
(153, 12, 38408),
(154, 12, 38704),
(155, 12, 38101),
(156, 12, 38401),
(157, 12, 38805),
(158, 12, 38410),
(159, 12, 38403),
(160, 12, 38301),
(161, 12, 38802),
(162, 12, 38051),
(163, 12, 38412),
(164, 12, 38308),
(165, 12, 38807),
(166, 12, 38102),
(167, 12, 38405),
(168, 12, 38706),
(169, 12, 38414),
(170, 12, 38707),
(171, 12, 38310),
(172, 12, 38407),
(173, 12, 38202),
(174, 12, 38303),
(175, 12, 38409),
(176, 12, 38416),
(177, 12, 38809),
(178, 12, 38104),
(179, 12, 38708),
(180, 12, 38204),
(181, 12, 38105),
(182, 12, 38710),
(183, 12, 38811),
(184, 12, 38420),
(185, 12, 38413),
(186, 12, 38415),
(187, 12, 38422),
(188, 12, 38601),
(189, 12, 38106),
(190, 12, 38810),
(191, 12, 38813),
(192, 12, 38424),
(193, 12, 38417),
(194, 12, 38312),
(195, 12, 38419),
(196, 12, 38426),
(197, 12, 38305),
(198, 12, 38709),
(199, 12, 38428),
(200, 12, 38711),
(201, 12, 38812),
(202, 12, 38421),
(203, 12, 38602),
(204, 12, 38501),
(205, 12, 38713),
(206, 12, 38430),
(207, 12, 58002),
(208, 12, 38307),
(209, 12, 38432),
(210, 12, 38814),
(211, 12, 38717),
(212, 12, 38423),
(213, 12, 38434),
(214, 12, 38819),
(215, 12, 38314),
(216, 12, 38425),
(217, 12, 38816),
(218, 12, 38719),
(219, 12, 38316),
(220, 12, 38436),
(221, 12, 38318),
(222, 12, 38818),
(223, 12, 38502),
(224, 12, 38429),
(225, 12, 38718),
(226, 12, 38431),
(227, 12, 38720),
(228, 12, 38438),
(229, 12, 38820),
(230, 12, 38107),
(231, 12, 38721),
(232, 12, 38440),
(233, 12, 38722),
(234, 12, 38109),
(235, 12, 38435),
(236, 12, 68007),
(237, 12, 38201),
(238, 12, 38442),
(239, 12, 38309),
(240, 12, 38437),
(241, 12, 38108),
(242, 12, 38444),
(243, 12, 38311),
(244, 12, 38446),
(245, 12, 38110),
(246, 12, 38441),
(247, 12, 38448),
(248, 12, 38206),
(249, 12, 38723),
(250, 12, 38724),
(251, 12, 38313),
(252, 12, 38450),
(253, 12, 38726),
(254, 12, 38315),
(255, 12, 38445),
(256, 12, 38452),
(257, 12, 38447),
(258, 12, 38826),
(259, 12, 38317),
(260, 12, 38831),
(261, 12, 38728),
(262, 12, 38449),
(263, 12, 38725),
(264, 12, 38454),
(265, 12, 38828),
(266, 12, 38451),
(267, 12, 38727),
(268, 12, 38456),
(269, 12, 38319),
(270, 12, 38453),
(271, 12, 38830),
(272, 12, 38321),
(273, 13, 0),
(274, 13, 19016),
(275, 13, 59050),
(276, 13, 59547),
(277, 13, 59548),
(278, 13, 59049),
(279, 13, 19015),
(280, 14, 0),
(281, 14, 52171),
(282, 14, 52174),
(283, 14, 52172),
(284, 14, 52173),
(285, 15, 0),
(286, 15, 12343),
(287, 15, 55713),
(288, 15, 55749),
(289, 15, 75718),
(290, 15, 7525),
(291, 15, 55750),
(292, 15, 7526),
(293, 15, 75722),
(294, 15, 55751),
(295, 15, 55714),
(296, 15, 75717),
(297, 15, 75721),
(298, 15, 12344),
(299, 15, 55752);
You can try to declare a variable #Val with CASE WHEN to make it.
using CASE WHEN to judgment number. if number is 0 or null do accumulate the #Val
Schema (MySQL v5.7)
CREATE TABLE `stack` (
`id` int(11) NOT NULL auto_increment,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `number`) VALUES
(1, 52201),
(2, 53008),
(3, 55007),
(4, 75222),
(5, 0),
(6, 74992),
(7, 14553),
(8, 54582),
(9, 54581),
(10, 74991),
(11, 14554),
(12, 0),
(13, 11413),
(14, 72414),
(15, 31415),
(16, 71416),
(17, 0),
(18, 59823),
(19, 69824),
(20, 59821),
(21, 69825),
(22, 59826);
Query #1
SET #Val= 1;
There are no results to be displayed.
Query #2
SELECT id,
(CASE WHEN coalesce(number,0) = 0 THEN #Val:=#Val+1 ELSE #Val END) counter,
number
FROM `stack` t1
order by id;
| id | number | counter |
| --- | ------ | ------- |
| 1 | 52201 | 1 |
| 2 | 53008 | 1 |
| 3 | 55007 | 1 |
| 4 | 75222 | 1 |
| 5 | 0 | 2 |
| 6 | 74992 | 2 |
| 7 | 14553 | 2 |
| 8 | 54582 | 2 |
| 9 | 54581 | 2 |
| 10 | 74991 | 2 |
| 11 | 14554 | 2 |
| 12 | 0 | 3 |
| 13 | 11413 | 3 |
| 14 | 72414 | 3 |
| 15 | 31415 | 3 |
| 16 | 71416 | 3 |
| 17 | 0 | 4 |
| 18 | 59823 | 4 |
| 19 | 69824 | 4 |
| 20 | 59821 | 4 |
| 21 | 69825 | 4 |
| 22 | 59826 | 4 |
View on DB Fiddle
You can emulate row_number functionality (for older versions of MySQL; version < 8.0).
Try the following query (SQL Fiddle Demo):
SET #row_number = 1;
SELECT
id,
number,
#row_number:= IF(IFNULL(number,0) = 0,
#row_number + 1,
#row_number) AS counter
FROM
stack
ORDER BY id ASC
Note:
Above code will also handle cases when number field value is null instead of 0.
Ifnull() function helps in returning 0 value, if a particular number is null.
Utilizing If() function, we can then check if the current value is 0 or not, and update the counter accordingly.
You can also achieve this by simply writing a procedure
1) Firstly, You can alter your table structure by adding column counter
ALTER TABLE stack ADD COLUMN counter INT NOT NULL DEFAULT 0;
2) Then create a procedure as:
DELIMITER $$
CREATE PROCEDURE `updateVal_Counter`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE result INT DEFAULT 1;
DECLARE a,b,id1 INT;
DECLARE cur1 CURSOR FOR SELECT id,number,counter FROM `test`.`stack`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop:
LOOP
FETCH cur1 INTO id1,a,b;
IF a = 0 THEN
SET result = result + 1;
END IF;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `stack` SET counter =result WHERE number=a AND id = id1;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
3) Finally call the procedure as
call updateVal_Counter;
This will add a new column with desired value in your table structure.

MYSQL- Substract two colums and get the result

I am trying to write a query wherein it should count the number of students and tell me the remaining seats available in a vehicle.
Have managaed to identify which student is associated to which bus but getting stuck to find the seat remaining
Below is data :
vehnum route seats student id
23 2 45 2345
33 3 46 6789
Below is the query :
SELECT deveh.vehicle_reg_no AS vehnum
, veh.route_code AS route
, deveh.seating_capacity AS vehseat
, class.fk_stu_id
FROM tbl_stu_class AS class
JOIN tbl_stu_route AS route
ON route.fk_stu_cls_id = class.pk_stu_cls_id
JOIN list_routes AS veh
ON route.fk_route_id = veh.pk_route_id
JOIN list_vehicles AS deveh
ON deveh.pk_vehicle_id = veh.fk_vehicle_id
WHERE class.fk_year_id = 62
AND class.current_yr = 'Y'
Added sample data :
INSERT INTO `list_vehicles` (`pk_vehicle_id`, `vehicle_reg_no`, `vehicle_type`, `regd_owner_name`, `seating_capacity`, `brand_model`, `type_of_body`, `reg_address`, `fuel_type`, `chasis_no`, `reg_authority`, `engine_no`, `color`, `reg_date`, `reg_valid_date`, `month_yr_mfg`, `fk_user_id`, `timestamp`) VALUES
(46, 'J58987', 'Bus', 'M', 30, 'VOlvo', 'Steel', 'FBD', 'Petrol', '565', 'M1', '5689', 'blue', '2016-10-02', '2016-10-02', '2014-12-31', 1, '2018-07-11 18:01:06'),
(53, 'J1234', 'Bus', 'der', 45, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '123456', 'det', '2365', 'blue', '2010-12-12', '2020-12-12', '2009-12-11', 1, '2018-07-12 06:54:50'),
(54, 'J1234er', 'Van', 'der', 46, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '12345634', 'det', '236534', 'blue', '2020-02-03', '2020-02-03', '2008-11-11', 1, '2018-07-12 06:57:59');
INSERT INTO `tbl_stu_class` (`pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`, `fk_user_id`, `timestamp`) VALUES
(1, 56, 50, 22, 10, 'N', 1, '2018-06-08 06:57:34'),
(3, 123, 50, 24, 7, 'N', 1, '2018-06-12 07:54:46'),
(4, 126, 50, 24, 7, 'N', 56, '2018-06-12 07:54:46'),
(5, 123, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(6, 126, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(7, 132, 50, 22, 9, 'Y', 1, '2018-06-24 10:27:57'),
(8, 133, 51, 23, NULL, 'Y', 1, '2018-06-24 18:22:33'),
(10, 127, 51, 23, NULL, 'Y', 0, '2018-07-11 17:47:05'),
(11, 134, 62, 22, NULL, 'Y', 0, '2018-07-13 08:11:16'),
(12, 135, 62, 21, 7, 'Y', 1, '2018-07-13 11:12:08'),
(13, 136, 62, 21, 9, 'Y', 1, '2018-07-13 14:59:04');

MySQL database queries between queries

Here is my SQL file:
-- phpMyAdmin SQL Dump
-- version 3.4.7.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 23, 2012 at 09:26 AM
-- Server version: 5.1.56
-- PHP Version: 5.2.9
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `danielle_youtube`
--
-- --------------------------------------------------------
--
-- Table structure for table `items`
--
CREATE TABLE IF NOT EXISTS `items` (
`id` int(11) NOT NULL,
`name` varchar(65) COLLATE utf8_unicode_ci NOT NULL,
`cost` float NOT NULL,
`seller_id` int(11) NOT NULL,
`bids` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `items`
--
INSERT INTO `items` (`id`, `name`, `cost`, `seller_id`, `bids`) VALUES
(1, 'Brand New iMac Computer', 149.99, 32, 3),
(2, 'used diaper from my sister', 2.04, 1, 0),
(3, 'Fresh apple pie', 14.99, 54, 32),
(4, 'New gym socks', 2.34, 90, 566),
(5, 'Weedwacker only slightly used', 4.56, 84, 2),
(6, 'New ipad stolen from best buy', 399, 32, 23),
(7, 'Book about having babies', 21.34, 44, 21),
(8, 'Woman Jeans', 49.5, 56, 123),
(9, 'traditional carpet', 25.45, 14, 75),
(10, '3 boxes of frogs', 30.49, 68, 145),
(11, '48 boxes of frogs', 74.29, 6, 99),
(12, '7 boxes of frogs', 857.75, 18, 88),
(13, 'laptop', 743.3, 89, 158),
(14, 'thumbelina', 228.05, 15, 49),
(15, 'bed', 127.15, 65, 189),
(16, 'shampoing', 12.8, 6, 105),
(17, 'stove', 37.66, 68, 111),
(18, 'cushion', 7.15, 97, 157),
(19, 'refrigerator', 657.49, 61, 129),
(20, 'gold necklace', 853.07, 10, 101),
(21, 'pan', 33.7, 7, 184),
(22, 'awesome alien computer game', 10.75, 18, 29),
(23, 'baby coat', 89.99, 14, 47),
(24, 'baby seat', 145.78, 2, 199),
(25, 'satchel', 44.71, 15, 66),
(26, 'women perfum', 110.9, 48, 84),
(27, 'conveyor belt', 1120.75, 11, 4),
(28, 'used car', 5700.5, 12, 135),
(29, 'supercomputer', 49.75, 50, 176),
(30, 'mirror', 26.8, 19, 56),
(31, 'piano', 1800.4, 13, 147),
(32, 'quitar', 88.4, 25, 164),
(33, 'trumpet', 255.15, 36, 23),
(34, 'machintosh', 3845, 20, 107),
(35, 'earphone', 10.5, 17, 110),
(36, 'computer', 418, 11, 152),
(37, 'night light', 13.87, 97, 198),
(38, 'pc bag', 50.99, 48, 65),
(39, 'babyfoot', 376.7, 2, 121),
(40, 'hairdryer', 88.9, 12, 177),
(41, 'babyliss', 130.75, 68, 79),
(42, 'door', 150.5, 98, 13),
(43, 'baby soap', 12.7, 4, 198),
(44, 'used phone', 43.75, 9, 69),
(45, 'bath', 757.15, 96, 55),
(46, 'flower', 10.75, 16, 89),
(47, 'battery charger', 48.75, 25, 87),
(48, 'air conditioner', 975, 12, 151),
(49, 'casserole', 115.75, 46, 35),
(50, 'used toilet', 180.7, 64, 11),
(51, 'teashirt', 14.98, 65, 114),
(52, 'moto', 920, 22, 174),
(53, 'saxophone', 220.9, 60, 140),
(54, 'bicycle', 180.55, 97, 35),
(55, 'man perfum', 95, 75, 199),
(56, 'table', 157.25, 91, 48),
(57, 'boat', 4890.5, 17, 177),
(58, 'iphone', 547, 8, 28),
(59, 'body milk', 50.5, 16, 90),
(60, 'new curtain for bedroom', 278.4, 92, 11),
(61, 'diamond ring', 1900, 15, 45),
(62, 'swept', 4.5, 9, 99),
(63, 'women hat', 17.55, 39, 60),
(64, 'washing machine', 680.9, 42, 125),
(65, 'baby bottle', 27.98, 91, 117),
(66, 'women sun glasses', 66.7, 18, 174),
(67, 'person weighs', 65.25, 10, 100),
(68, 'photo frame', 18, 85, 170),
(69, 'key board', 16.7, 90, 101),
(70, 'screen', 250, 81, 188),
(71, 'bucket', 2.5, 1, 19),
(72, 'lipstick', 24.75, 3, 44),
(73, 'wardrobe', 120.75, 9, 71),
(74, 'blue dress size 40', 88.9, 7, 113),
(75, 'newspaper', 1.5, 95, 172),
(76, 'scanner', 350, 14, 62),
(77, 'camera', 550.7, 17, 95),
(78, 'camcorder', 788.99, 25, 127),
(79, 'gun', 420.1, 81, 107),
(80, 'domestic dog', 200, 19, 129),
(81, 'horse', 759.5, 30, 115),
(82, 'truck', 7800.5, 32, 123),
(83, 'soccer ball', 95.49, 54, 155),
(84, 'gold earring', 385, 75, 92),
(85, 'basket', 250.45, 46, 142),
(86, 'bikini', 85.2, 12, 57),
(87, 'red skirt', 15.9, 18, 188),
(88, 'copier machine', 800.7, 50, 160),
(89, 'handbag', 35.9, 8, 108),
(90, 'bath towel', 25.1, 11, 186),
(91, 'coffee machine', 210.89, 15, 170),
(92, 'wedding dress', 690, 26, 48),
(93, 'man sun glasses', 80.7, 19, 174),
(94, 'candle', 7.5, 22, 102),
(95, 'scarf', 11.9, 7, 143),
(96, 'microwave', 150.29, 6, 11),
(97, 'electric oven', 645, 62, 171),
(98, 'play station', 256.75, 12, 188),
(99, 'dvd', 126.84, 14, 113),
(100, 'magazine', 3.5, 8, 152);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;`
Now, when I run this query:
SELECT name , MIN(cost) FROM items WHERE name LIKE '% boxes of frogs' AND seller_id IN (
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get
name: 3 boxes of frogs
MIN(cost): 30.489999771118164
But when I use:
SELECT name , MIN(cost) FROM items WHERE seller_id IN(
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get this result:
name: 3 boxes of frogs
MIN(cost): 10.75
I want to know why the results are different?
The first query returns the name of the item and the cheapest price from items of a box of frogs (see the condition on name in the external query).
The second query returns the name of the item and the cheapest price from items of any item sold by someone who is also selling a box of frogs (the condition on name is only applied in the internal condition).

trying to make a average list for a bowling club

I'm trying to make an average list for a bowling club. I have a database with the following tables:
team_medlem (medlem_id, name, address, etc)
team_samling (match_id, lag_id, borta (0 = home game, 1 = away game), date, time, etc)
team_match (id, match_id, s1, s2, etc)
team_resultat (id, medlem_id, results, series, banp, match_id)
I want to get the total average, average home and away average. result is the total points for the entire game for each player and the series is the number of played series.
I have this:
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp)
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
How can I get home average and away average?
I have tried this but it miscalculated averages
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp), avg(case when not borta then resultat/serier end) as hemmasnitt, avg(case when borta then resultat/serier end) as bortasnitt
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
Sampeldata:
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:07
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_medlem`
--
CREATE TABLE IF NOT EXISTS `team_medlem` (
`medlem_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`namn` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
PRIMARY KEY (`medlem_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Medlems register'AUTO_INCREMENT=36 ;
--
-- Dumpning av Data i tabell `team_medlem`
--
INSERT INTO `team_medlem` (`medlem_id`, `namn`) VALUES
(1, 'Sven-Åke Jansson'),
(2, 'Christer Wendel'),
(4, 'Sören Carlsson'),
(5, 'Jan Ingvarsson'),
(6, 'Lars-Göran Wetterholm'),
(7, 'Anders Svensson'),
(8, 'Bengt Carlsson'),
(9, 'Per-Olof Johansson'),
(10, 'Barsom Calan'),
(11, 'Mikael Mårtensson'),
(12, 'Andreas Johansson'),
(13, 'Jonas Wendel'),
(14, 'Sören Fransson'),
(15, 'Daniel Fransson'),
(16, 'Stefan Lord'),
(18, 'Lennart Johansson'),
(19, 'Jonas Nilsson'),
(20, 'Mikael Nilsson'),
(21, 'Patrik Emanuelsson'),
(22, 'Jörgen Norman'),
(24, 'Anders Johansson'),
(25, 'Andreas Larsson'),
(26, 'Roger Larsson'),
(27, 'Peter Ericson'),
(28, 'Dado Hrnic'),
(29, 'Maria Lord-Johansson'),
(33, 'Mats Wellermark');
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:13
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_samling`
--
CREATE TABLE IF NOT EXISTS `team_samling` (
`match_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lag_id` int(11) DEFAULT NULL,
`omg` int(2) unsigned DEFAULT NULL,
`borta` tinyint(1) unsigned DEFAULT '0',
`datum` date DEFAULT NULL,
`tid` time DEFAULT NULL,
`motstandare` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
`samling` varchar(6) COLLATE utf8_swedish_ci DEFAULT NULL,
`ovrigt` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
PRIMARY KEY (`match_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Samling' AUTO_INCREMENT=14 ;
--
-- Dumpning av Data i tabell `team_samling`
--
INSERT INTO `team_samling` (`match_id`, `lag_id`, `omg`, `borta`, `datum`, `tid`, `motstandare`, `samling`, `ovrigt`) VALUES
(1, 1, 1, 0, '2014-10-28', '12:15:00', 'Forsheda', '11:30', 'match dräkt'),
(2, 2, 1, 0, '2014-10-27', '10:00:00', 'Jönköping KK', '09:15', ''),
(3, 3, 3, 0, '2014-10-30', '15:20:00', 'Alvesta', '14:30', ''),
(4, 2, 2, 1, '2014-11-05', '12:00:00', 'Sävsjö', '09:15', 'Buss'),
(5, 2, 2, 1, '2014-11-05', '14:00:00', 'Eksjö', NULL, NULL),
(6, 3, 2, 0, '2014-11-06', '11:15:00', 'Jönköping kk', '10:30', NULL),
(11, 1, 20, 1, '2014-11-05', '12:30:00', 'test borta 2', '09:30', NULL),
(13, 1, 22, 0, '2014-11-06', '10:00:00', 'Test hemma', '09:00', NULL);
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:16
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_resultat`
--
CREATE TABLE IF NOT EXISTS `team_resultat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`medlem_id` int(11) unsigned DEFAULT NULL,
`resultat` decimal(5,2) unsigned DEFAULT NULL,
`serier` int(2) unsigned DEFAULT NULL,
`banp` int(1) unsigned DEFAULT NULL,
`match_id` int(11) unsigned DEFAULT NULL,
`resultat_sparad` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Match resultat' AUTO_INCREMENT=53 ;
--
-- Dumpning av Data i tabell `team_resultat`
--
INSERT INTO `team_resultat` (`id`, `medlem_id`, `resultat`, `serier`, `banp`, `match_id`, `resultat_sparad`) VALUES
(1, 6, '800.00', 4, 3, 2, '2014-10-11 17:11:37'),
(2, 7, '790.00', 4, 3, 2, '2014-10-11 17:11:37'),
(3, 11, '780.00', 4, 2, 2, '2014-10-11 17:11:37'),
(4, 12, '770.00', 4, 2, 2, '2014-10-11 17:11:37'),
(5, 14, '760.00', 4, 3, 2, '2014-10-11 17:11:37'),
(6, 15, '750.00', 4, 2, 2, '2014-10-11 17:11:37'),
(7, 24, '740.00', 4, 2, 2, '2014-10-11 17:11:37'),
(8, 25, '505.00', 3, 1, 2, '2014-10-11 17:11:37'),
(9, 27, '165.00', 1, 0, 2, '2014-10-11 17:11:37'),
(10, 8, '700.00', 4, 2, 1, '2014-10-26 14:29:20'),
(11, 9, '690.00', 4, 3, 1, '2014-10-26 14:29:20'),
(12, 10, '680.00', 4, 2, 1, '2014-10-26 14:29:20'),
(13, 13, '670.00', 4, 2, 1, '2014-10-26 14:29:20'),
(14, 19, '660.00', 4, 2, 1, '2014-10-26 14:29:20'),
(15, 20, '650.00', 4, 1, 1, '2014-10-26 14:29:20'),
(16, 22, '640.00', 4, 2, 1, '2014-10-26 14:29:20'),
(17, 25, '640.00', 4, 1, 1, '2014-10-26 14:29:20'),
(19, 1, '800.00', 4, 2, 3, '2014-10-26 14:31:00'),
(20, 2, '790.00', 4, 2, 3, '2014-10-26 14:31:00'),
(21, 4, '780.00', 4, 2, 3, '2014-10-26 14:31:00'),
(22, 5, '770.00', 4, 2, 3, '2014-10-26 14:31:00'),
(23, 7, '760.00', 4, 2, 3, '2014-10-26 14:31:00'),
(24, 8, '750.00', 4, 2, 3, '2014-10-26 14:31:00'),
(25, 14, '740.00', 4, 3, 3, '2014-10-26 14:31:00'),
(26, 15, '500.00', 3, 1, 3, '2014-10-26 14:31:00'),
(27, 18, '150.00', 1, 1, 3, '2014-10-26 14:31:00'),
(28, 2, '800.00', 4, 2, 4, '2014-10-26 16:57:47'),
(29, 6, '790.00', 4, 2, 4, '2014-10-26 16:57:47'),
(30, 11, '780.00', 4, 2, 4, '2014-10-26 16:57:47'),
(31, 12, '770.00', 4, 2, 4, '2014-10-26 16:57:47'),
(32, 14, '760.00', 4, 2, 4, '2014-10-26 16:57:47'),
(33, 15, '750.00', 4, 2, 4, '2014-10-26 16:57:47'),
(34, 25, '740.00', 4, 2, 4, '2014-10-26 16:57:47'),
(35, 33, '730.00', 4, 2, 4, '2014-10-26 16:57:47'),
(36, 2, '800.00', 4, 2, 5, '2014-10-26 16:58:26'),
(37, 6, '790.00', 4, 2, 5, '2014-10-26 16:58:26'),
(38, 11, '780.00', 4, 2, 5, '2014-10-26 16:58:26'),
(39, 12, '770.00', 4, 2, 5, '2014-10-26 16:58:26'),
(40, 14, '760.00', 4, 2, 5, '2014-10-26 16:58:26'),
(41, 15, '750.00', 4, 2, 5, '2014-10-26 16:58:26'),
(42, 25, '740.00', 4, 2, 5, '2014-10-26 16:58:26'),
(43, 33, '730.00', 4, 2, 5, '2014-10-26 16:58:26'),
(44, 10, '700.00', 4, 2, 6, '2014-10-26 16:59:15'),
(45, 13, '690.00', 4, 2, 6, '2014-10-26 16:59:15'),
(46, 16, '680.00', 4, 2, 6, '2014-10-26 16:59:15'),
(47, 19, '670.00', 4, 2, 6, '2014-10-26 16:59:15'),
(48, 18, '660.00', 4, 2, 6, '2014-10-26 16:59:15'),
(49, 21, '650.00', 4, 2, 6, '2014-10-26 16:59:15'),
(50, 26, '640.00', 4, 2, 6, '2014-10-26 16:59:15'),
(51, 27, '520.00', 3, 2, 6, '2014-10-26 16:59:15'),
(52, 29, '160.00', 1, 2, 6, '2014-10-26 16:59:15');
expected results
Namn Snitt Hemma Borta Banp
Sven-Åke Jansson 200,00 200,00 0 2
Christer Wendel 199,17 197,50 200,00 6
Lars-Göran Wetterholm 198,33 200,00 197,50 7
Mikael Mårtensson 195,00 195,00 195,00 6
Sören Carlsson 195,00 195,00 0 2
Anders Svensson 193,75 193,75 0 5
Andreas Johansson 192,50 192,50 192,50 6
Jan Ingvarsson 192,50 192,50 0 2
Sören Fransson 188,75 187,50 190,00 10
Anders Johansson 185,00 185,00 0 2
Daniel Fransson 183,33 178,57 187,50 7
Mats Wellermark 182,50 0 182,50 4
Bengt Carlsson 181,25 181,25 0 4
Andreas Larsson 175,00 163,57 185,00 6
Barsom Calan 172,50 172,50 0 4
Per-Olof Johansson 172,50 172,50 0 3
Peter Ericson 171,25 171,25 0 2
Jonas Wendel 170,00 170,00 0 4
Stefan Lord 170,00 170,00 0 2
Jonas Nilsson 166,25 166,25 0 4
Mikael Nilsson 162,50 162,50 0 1
Patrik Emanuelsson 162,50 162,50 0 2
Lennart Johansson 162,00 162,00 0 3
Jörgen Norman 160,00 160,00 0 2
Maria Lord-Johansson 160,00 160,00 0 2
Roger Larsson 160,00 160,00 0 2