Skip to content

Instantly share code, notes, and snippets.

@morontt
Created June 9, 2015 12:15
Show Gist options
  • Save morontt/d7b467c1237bc6b1175c to your computer and use it in GitHub Desktop.
Save morontt/d7b467c1237bc6b1175c to your computer and use it in GitHub Desktop.
SQL query
CREATE TABLE IF NOT EXISTS `phone_call` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user` varchar(16) NOT NULL,
`flag` int(10) unsigned NOT NULL,
`length` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `phone_call` (`id`, `user`, `flag`, `length`) VALUES
(1, 'pupkin', 0, 12),
(2, 'pupkin', 1, 13),
(3, 'pupkin', 0, 11),
(4, 'vasya', 1, 23),
(5, 'pupkin', 1, 2),
(6, 'vasya', 1, 3),
(7, 'pupkin', 0, 5),
(8, 'vasya', 0, 7);
SELECT `user`, SUM(`length` * `flag`) AS `day`, SUM(`length` * (1 - `flag`)) AS `night`
FROM `phone_call`
GROUP BY `user`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment