用Mysql计算一个月内的周数

时间:2015-02-19 07:35:53

标签: mysql

通过Mysql查询计算该年特定月份的确切周数。 例如,我必须通过mysql确定19-2-2015这个日期的周数?

我尝试了这个查询,但它在2015年取得了错误的结果:

SELECT WEEK(my_date_field,5) - 
WEEK(DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY),5)+1

我只想根据mysql的当前日历计算当月的当前周数。

我有一个数据库表:

CREATE TABLE IF NOT EXISTS `lcsi` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `batchID` datetime default NULL,
  `ticker` varchar(8) default NULL,
  `price` float(4,2) default NULL,
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

并插入数据,如

INSERT INTO `lcsi` (`id`, `batchID`, `ticker`, `price`, `date`) VALUES
(1, '2003-12-16 11:26:04', 'ACMTA', 11.65, '2003-12-16 16:27:37'),
(2, '2003-12-16 11:26:04', 'ACO', 16.61, '2003-12-16 16:27:37'),
(3, '2003-12-16 11:26:04', 'ADLTQ.OB', 0.31, '2003-12-16 16:27:37'),
(4, '2003-12-16 11:26:04', 'AHH', 20.75, '2003-12-16 16:27:38'),
(5, '2003-12-16 11:26:04', 'ALG', 15.18, '2003-12-16 16:27:38'),
(6, '2003-12-16 11:26:04', 'AMN', 34.89, '2003-12-16 16:27:38'),
(7, '2003-12-16 11:26:04', 'ARTW', 4.53, '2008-01-02 16:12:56'),
(8, '2003-12-16 11:26:04', 'ASVI', 30.59, '2003-12-16 16:27:38'),
(9, '2003-12-16 11:26:04', 'BZH', 99.66, '2003-12-16 16:27:39'),
(10, '2003-12-16 11:26:04', 'CALC', 10.85, '2003-12-16 16:27:39'),
(11, '2003-12-16 11:26:04', 'CAT', 80.26, '2003-12-16 16:27:39'),
(12, '2003-12-16 11:26:04', 'CAV', 2.98, '2003-12-16 16:27:39'),
(13, '2003-12-16 11:26:04', 'CENT', 26.00, '2003-12-16 16:27:40'),
(14, '2003-12-16 11:26:04', 'CLWY', 0.46, '2003-12-16 16:27:40'),
(15, '2003-12-16 11:26:04', 'CNH', 15.40, '2003-12-16 16:27:40'),
(16, '2003-12-16 11:26:04', 'DE', 63.82, '2003-12-16 16:27:40'),
(17, '2003-12-16 11:26:04', 'DEVC', 7.12, '2003-12-16 16:27:41'),
(18, '2003-12-16 11:26:04', 'DOW', 40.01, '2003-12-16 16:27:41'),
(19, '2003-12-16 11:26:04', 'ERSN.PK', 0.00, '2003-12-16 16:27:41'),
(20, '2003-12-16 11:26:04', 'F', 14.29, '2003-12-16 16:27:41'),
(21, '2003-12-16 11:26:04', 'FBST', 6.58, '2003-12-16 16:27:42'),
(22, '2003-12-16 11:26:04', 'FRK', 55.96, '2003-12-16 16:27:42'),
(23, '2003-12-16 11:26:04', 'FSHOX', 33.74, '2003-12-16 16:27:42'),
(24, '2003-12-16 11:26:04', 'GEHL', 12.52, '2003-12-16 16:27:42'),
(25, '2003-12-16 11:26:04', 'GM', 49.35, '2003-12-16 16:27:43'),
(26, '2003-12-16 11:26:04', 'GRIF', 14.35, '2003-12-16 16:27:43'),
(27, '2003-12-16 11:26:04', 'GVA', 23.50, '2003-12-16 16:27:43'),
(28, '2003-12-16 11:26:04', 'HAN', 34.28, '2003-12-16 16:27:43'),
(29, '2003-12-16 11:26:04', 'HBP', 2.90, '2008-01-02 16:12:44'),
(30, '2003-12-16 11:26:04', 'HD', 34.01, '2003-12-16 16:27:44'),
(31, '2003-12-16 11:26:04', 'HORT', 4.52, '2003-12-16 16:27:44'),
(32, '2003-12-16 11:26:04', 'JCTCF', 4.80, '2003-12-16 16:27:44'),
(33, '2003-12-16 11:26:04', 'KNUR.OB', 0.14, '2003-12-16 16:27:45'),
(34, '2003-12-16 11:26:04', 'LAF', 38.80, '2003-12-16 16:27:45'),
(35, '2003-12-16 11:26:04', 'LMT', 49.01, '2003-12-16 16:27:45'),
(36, '2003-12-16 11:26:04', 'LOW', 52.97, '2003-12-16 16:27:45'),
(37, '2003-12-16 11:26:04', 'LSCO', 13.46, '2003-12-16 16:27:46'),
(38, '2003-12-16 11:26:04', 'MHCO.PK', 1.60, '2003-12-16 16:27:46'),
(39, '2003-12-16 11:26:04', 'MHP', 68.05, '2003-12-16 16:27:46'),
(40, '2003-12-16 11:26:04', 'MKTAY', 9.55, '2003-12-16 16:27:46'),
(41, '2003-12-16 11:26:04', 'MLM', 45.42, '2003-12-16 16:27:47'),
(42, '2003-12-16 11:26:04', 'MRGO', 7.11, '2003-12-16 16:27:47'),
(43, '2003-12-16 11:26:04', 'MVCO', 1.60, '2003-12-16 16:27:47'),
(44, '2003-12-16 11:26:04', 'NEQS.PK', 0.05, '2003-12-16 16:27:47'),
(45, '2003-12-16 11:26:04', 'NWL', 21.83, '2003-12-16 16:27:48'),
(46, '2003-12-16 11:26:04', 'PHHM', 17.56, '2003-12-16 16:27:48'),
(47, '2003-12-16 11:26:04', 'POOL', 31.84, '2003-12-16 16:27:48'),
(48, '2003-12-16 11:26:04', 'RMIX', 6.25, '2003-12-16 16:27:48'),
(49, '2003-12-16 11:26:04', 'SEHI', 2.18, '2003-12-16 16:27:49'),
(50, '2003-12-16 11:26:04', 'SNA', 30.77, '2003-12-16 16:27:49'),
(51, '2003-12-16 11:26:04', 'TECUA', 45.64, '2003-12-16 16:27:49'),
(52, '2003-12-16 11:26:04', 'TII', 34.26, '2003-12-16 16:27:49'),
(53, '2003-12-16 11:26:04', 'TSCO', 39.24, '2003-12-16 16:27:49'),
(54, '2003-12-16 11:26:04', 'TTC', 45.84, '2003-12-16 16:27:50'),
(55, '2003-12-16 11:26:04', 'URI', 18.59, '2003-12-16 16:27:50'),
(56, '2003-12-16 11:26:04', 'USHG.OB', 0.55, '2003-12-16 16:27:50'),
(57, '2003-12-16 11:26:04', 'VOLVY', 29.99, '2003-12-16 16:27:50'),
(58, '2003-12-16 11:26:04', 'WIKS.OB', 0.35, '2003-12-16 16:27:51'),
(59, '2003-12-16 11:26:04', 'WMSI', 4.07, '2003-12-16 16:27:51'),
(60, '2003-12-16 11:26:04', 'WPEC.OB', 0.35, '2003-12-16 16:27:51'),
(61, '2003-12-16 11:27:16', 'ACMTA', 11.65, '2003-12-16 16:28:49'),
(62, '2003-12-16 11:27:16', 'ACO', 16.56, '2003-12-16 16:28:49'),
(63, '2003-12-16 11:27:16', 'ADLTQ.OB', 0.31, '2003-12-16 16:28:50'),
(64, '2003-12-16 11:27:16', 'AHH', 20.75, '2003-12-16 16:28:50'),
(65, '2003-12-16 11:27:16', 'ALG', 15.18, '2003-12-16 16:28:50'),
(66, '2003-12-16 11:27:16', 'AMN', 34.89, '2003-12-16 16:28:50'),
(67, '2003-12-16 11:27:16', 'ARTW', 4.53, '2003-12-16 16:28:51'),
(68, '2003-12-16 11:27:16', 'ASVI', 30.59, '2003-12-16 16:28:51'),
(69, '2003-12-16 11:27:16', 'BZH', 99.70, '2003-12-16 16:28:51'),
(70, '2003-12-16 11:27:16', 'CALC', 10.85, '2003-12-16 16:28:51'),
(71, '2003-12-16 11:27:16', 'CAT', 80.25, '2003-12-16 16:28:52'),
(72, '2003-12-16 11:27:16', 'CAV', 2.98, '2003-12-16 16:28:52'),
(73, '2003-12-16 11:27:16', 'CENT', 26.00, '2003-12-16 16:28:52'),
(74, '2003-12-16 11:27:16', 'CLWY', 0.46, '2003-12-16 16:28:52'),
(75, '2003-12-16 11:27:16', 'CNH', 15.35, '2003-12-16 16:28:52'),
(76, '2003-12-16 11:27:16', 'DE', 63.89, '2003-12-16 16:28:53'),
(77, '2003-12-16 11:27:16', 'DEVC', 7.12, '2003-12-16 16:28:53'),
(78, '2003-12-16 11:27:16', 'DOW', 40.01, '2003-12-16 16:28:53'),
(79, '2003-12-16 11:27:16', 'ERSN.PK', 0.00, '2003-12-16 16:28:53'),
(80, '2003-12-16 11:27:16', 'F', 14.28, '2003-12-16 16:28:54'),
(81, '2003-12-16 11:27:16', 'FBST', 6.58, '2003-12-16 16:28:54'),
(82, '2003-12-16 11:27:16', 'FRK', 56.00, '2003-12-16 16:28:54'),
(83, '2003-12-16 11:27:16', 'FSHOX', 33.74, '2003-12-16 16:28:55'),
(84, '2003-12-16 11:27:16', 'GEHL', 12.52, '2003-12-16 16:28:55'),
(85, '2003-12-16 11:27:16', 'GM', 49.41, '2003-12-16 16:28:55'),
(86, '2003-12-16 11:27:16', 'GRIF', 14.35, '2003-12-16 16:28:55'),
(87, '2003-12-16 11:27:16', 'GVA', 23.50, '2003-12-16 16:28:56'),
(88, '2003-12-16 11:27:16', 'HAN', 34.28, '2003-12-16 16:28:56'),
(89, '2003-12-16 11:27:16', 'HBP', 2.90, '2003-12-16 16:28:56'),
(90, '2003-12-16 11:27:16', 'HD', 34.02, '2003-12-16 16:28:57'),
(91, '2003-12-16 11:27:16', 'HORT', 4.52, '2003-12-16 16:28:57'),
(92, '2003-12-16 11:27:16', 'JCTCF', 4.80, '2003-12-16 16:28:57'),
(93, '2003-12-16 11:27:16', 'KNUR.OB', 0.14, '2003-12-16 16:28:57'),
(94, '2003-12-16 11:27:16', 'LAF', 38.80, '2003-12-16 16:28:58'),
(95, '2003-12-16 11:27:16', 'LMT', 49.01, '2003-12-16 16:28:58'),
(96, '2003-12-16 11:27:16', 'LOW', 53.01, '2003-12-16 16:28:58'),
(97, '2003-12-16 11:27:16', 'LSCO', 13.46, '2003-12-16 16:28:59'),
(98, '2003-12-16 11:27:16', 'MHCO.PK', 1.60, '2003-12-16 16:28:59'),
(99, '2003-12-16 11:27:16', 'MHP', 68.07, '2003-12-16 16:28:59'),
(100, '2003-12-16 11:27:16', 'MKTAY', 9.55, '2003-12-16 16:28:59'),
(101, '2003-12-16 11:27:16', 'MLM', 45.44, '2003-12-16 16:29:00'),
(102, '2003-12-16 11:27:16', 'MRGO', 7.11, '2003-12-16 16:29:00'),
(103, '2003-12-16 11:27:16', 'MVCO', 1.60, '2003-12-16 16:29:00'),
(104, '2003-12-16 11:27:16', 'NEQS.PK', 0.05, '2003-12-16 16:29:01'),
(105, '2003-12-16 11:27:16', 'NWL', 21.84, '2003-12-16 16:29:01'),
(106, '2003-12-16 11:27:16', 'PHHM', 17.56, '2003-12-16 16:29:01'),
(107, '2003-12-16 11:27:16', 'POOL', 31.83, '2003-12-16 16:29:01'),
(108, '2003-12-16 11:27:16', 'RMIX', 6.21, '2003-12-16 16:29:02'),
(109, '2003-12-16 11:27:16', 'SEHI', 2.18, '2003-12-16 16:29:02'),
(110, '2003-12-16 11:27:16', 'SNA', 30.77, '2003-12-16 16:29:02'),
(111, '2003-12-16 11:27:16', 'TECUA', 45.64, '2003-12-16 16:29:03'),
(112, '2003-12-16 11:27:16', 'TII', 34.34, '2003-12-16 16:29:03'),
(113, '2003-12-16 11:27:16', 'TSCO', 39.25, '2003-12-16 16:29:03'),
(114, '2003-12-16 11:27:16', 'TTC', 45.84, '2003-12-16 16:29:03'),
(115, '2003-12-16 11:27:16', 'URI', 18.59, '2003-12-16 16:29:04'),
(116, '2003-12-16 11:27:16', 'USHG.OB', 0.55, '2003-12-16 16:29:04'),
(117, '2003-12-16 11:27:16', 'VOLVY', 29.99, '2003-12-16 16:29:04'),
(118, '2003-12-16 11:27:16', 'WIKS.OB', 0.35, '2003-12-16 16:29:05'),
(119, '2003-12-16 11:27:16', 'WMSI', 4.07, '2003-12-16 16:29:05'),
(120, '2003-12-16 11:27:16', 'WPEC.OB', 0.35, '2003-12-16 16:29:05'),
(121, '2003-12-16 11:47:36', 'ACMTA', 11.65, '2003-12-16 16:49:09'),
(122, '2003-12-16 11:47:36', 'ACO', 16.62, '2003-12-16 16:49:09'),
(123, '2003-12-16 11:47:36', 'ADLTQ.OB', 0.31, '2003-12-16 16:49:09'),
(124, '2003-12-16 11:47:36', 'AHH', 20.85, '2003-12-16 16:49:10'),
(125, '2003-12-16 11:47:36', 'ALG', 15.20, '2003-12-16 16:49:10'),
(126, '2003-12-16 11:47:36', 'AMN', 34.95, '2003-12-16 16:49:10'),
(127, '2003-12-16 11:47:36', 'ARTW', 4.53, '2003-12-16 16:49:10'),
(128, '2003-12-16 11:47:36', 'ASVI', 30.65, '2003-12-16 16:49:11'),
(129, '2003-12-16 11:47:36', 'BZH', 99.99, '2003-12-16 16:49:11'),
(130, '2003-12-16 11:47:36', 'CALC', 10.85, '2003-12-16 16:49:11'),
(131, '2003-12-16 11:47:36', 'CAT', 80.35, '2003-12-16 16:49:11'),
(132, '2003-12-16 11:47:36', 'CAV', 3.00, '2003-12-16 16:49:12'),
(133, '2003-12-16 11:47:36', 'CENT', 26.20, '2003-12-16 16:49:12'),
(134, '2003-12-16 11:47:36', 'CLWY', 0.46, '2003-12-16 16:49:12'),
(135, '2003-12-16 11:47:36', 'CNH', 15.37, '2003-12-16 16:49:12'),
(136, '2003-12-16 11:47:36', 'DE', 63.92, '2003-12-16 16:49:13'),
(137, '2003-12-16 11:47:36', 'DEVC', 7.12, '2003-12-16 16:49:13'),
(138, '2003-12-16 11:47:36', 'DOW', 40.00, '2003-12-16 16:49:13'),
(139, '2003-12-16 11:47:36', 'ERSN.PK', 0.00, '2003-12-16 16:49:14'),
(140, '2003-12-16 11:47:36', 'F', 14.29, '2003-12-16 16:49:14'),
(141, '2003-12-16 11:47:36', 'FBST', 6.59, '2003-12-16 16:49:14'),
(142, '2003-12-16 11:47:36', 'FRK', 56.07, '2003-12-16 16:49:14'),
(143, '2003-12-16 11:47:36', 'FSHOX', 33.74, '2003-12-16 16:49:15'),
(144, '2003-12-16 11:47:36', 'GEHL', 12.47, '2003-12-16 16:49:15'),
(145, '2003-12-16 11:47:36', 'GM', 49.48, '2003-12-16 16:49:15'),
(146, '2003-12-16 11:47:36', 'GRIF', 14.35, '2003-12-16 16:49:16'),
(147, '2003-12-16 11:47:36', 'GVA', 23.54, '2003-12-16 16:49:16'),
(148, '2003-12-16 11:47:36', 'HAN', 34.25, '2003-12-16 16:49:17'),
(149, '2003-12-16 11:47:36', 'HBP', 2.85, '2003-12-16 16:49:17'),
(150, '2003-12-16 11:47:36', 'HD', 34.10, '2003-12-16 16:49:17'),
(151, '2003-12-16 11:47:36', 'HORT', 4.52, '2003-12-16 16:49:17'),
(152, '2003-12-16 11:47:36', 'JCTCF', 4.80, '2003-12-16 16:49:18'),
(153, '2003-12-16 11:47:36', 'KNUR.OB', 0.19, '2003-12-16 16:49:18'),
(154, '2003-12-16 11:47:36', 'LAF', 38.90, '2003-12-16 16:49:18'),
(155, '2003-12-16 11:47:36', 'LMT', 48.98, '2003-12-16 16:49:18'),
(156, '2003-12-16 11:47:36', 'LOW', 53.20, '2003-12-16 16:49:19'),
(157, '2003-12-16 11:47:36', 'LSCO', 13.46, '2003-12-16 16:49:19'),
(158, '2003-12-16 11:47:36', 'MHCO.PK', 1.60, '2003-12-16 16:49:19'),
(159, '2003-12-16 11:47:36', 'MHP', 68.11, '2003-12-16 16:49:20'),
(160, '2003-12-16 11:47:36', 'MKTAY', 9.55, '2003-12-16 16:49:20'),
(161, '2003-12-16 11:47:36', 'MLM', 45.58, '2003-12-16 16:49:20'),
(162, '2003-12-16 11:47:36', 'MRGO', 7.11, '2003-12-16 16:49:21'),
(163, '2003-12-16 11:47:36', 'MVCO', 1.60, '2003-12-16 16:49:21'),
(164, '2003-12-16 11:47:36', 'NEQS.PK', 0.05, '2003-12-16 16:49:21'),
(165, '2003-12-16 11:47:36', 'NWL', 21.79, '2003-12-16 16:49:21'),
(166, '2003-12-16 11:47:36', 'PHHM', 17.67, '2003-12-16 16:49:22'),
(167, '2003-12-16 11:47:36', 'POOL', 31.92, '2003-12-16 16:49:22'),
(168, '2003-12-16 11:47:36', 'RMIX', 6.25, '2003-12-16 16:49:22'),
(169, '2003-12-16 11:47:36', 'SEHI', 2.19, '2003-12-16 16:49:22'),
(170, '2003-12-16 11:47:36', 'SNA', 30.79, '2003-12-16 16:49:22'),
(171, '2003-12-16 11:47:36', 'TECUA', 45.73, '2003-12-16 16:49:23'),
(172, '2003-12-16 11:47:36', 'TII', 34.49, '2003-12-16 16:49:23'),
(173, '2003-12-16 11:47:36', 'TSCO', 39.26, '2003-12-16 16:49:23'),
(174, '2003-12-16 11:47:36', 'TTC', 46.15, '2003-12-16 16:49:23'),
(175, '2003-12-16 11:47:36', 'URI', 18.74, '2003-12-16 16:49:24'),
(176, '2003-12-16 11:47:36', 'USHG.OB', 0.55, '2003-12-16 16:49:24'),
(177, '2003-12-16 11:47:36', 'VOLVY', 29.99, '2003-12-16 16:49:24'),
(178, '2003-12-16 11:47:36', 'WIKS.OB', 0.35, '2003-12-16 16:49:24'),
(179, '2003-12-16 11:47:36', 'WMSI', 4.07, '2003-12-16 16:49:25'),
(180, '2003-12-16 11:47:36', 'WPEC.OB', 0.35, '2003-12-16 16:49:25'),
(181, '2003-12-16 11:50:33', 'ACMTA', 11.65, '2003-12-16 16:52:06'),
(182, '2003-12-16 11:50:33', 'ACO', 16.61, '2003-12-16 16:52:07'),
(183, '2003-12-16 11:50:33', 'ADLTQ.OB', 0.31, '2003-12-16 16:52:07'),
(184, '2003-12-16 11:50:33', 'AHH', 20.90, '2003-12-16 16:52:07'),
(185, '2003-12-16 11:50:33', 'ALG', 15.20, '2003-12-16 16:52:07'),
(186, '2003-12-16 11:50:33', 'AMN', 35.07, '2003-12-16 16:52:08'),
(187, '2003-12-16 11:50:33', 'ARTW', 4.53, '2003-12-16 16:52:08'),
(188, '2003-12-16 11:50:33', 'ASVI', 30.73, '2003-12-16 16:52:08'),
(189, '2003-12-16 11:50:33', 'BZH', 99.99, '2003-12-16 16:52:08'),
(190, '2003-12-16 11:50:33', 'CALC', 10.85, '2003-12-16 16:52:08'),
(191, '2003-12-16 11:50:33', 'CAT', 80.35, '2003-12-16 16:52:09'),
(192, '2003-12-16 11:50:33', 'CAV', 3.00, '2003-12-16 16:52:09'),
(193, '2003-12-16 11:50:33', 'CENT', 26.20, '2003-12-16 16:52:09'),
(194, '2003-12-16 11:50:33', 'CLWY', 0.46, '2003-12-16 16:52:09'),
(195, '2003-12-16 11:50:33', 'CNH', 15.32, '2003-12-16 16:52:10'),
(196, '2003-12-16 11:50:33', 'DE', 63.95, '2003-12-16 16:52:10'),
(197, '2003-12-16 11:50:33', 'DEVC', 7.12, '2003-12-16 16:52:10'),
(198, '2003-12-16 11:50:33', 'DOW', 40.01, '2003-12-16 16:52:10'),
(199, '2003-12-16 11:50:33', 'ERSN.PK', 0.00, '2003-12-16 16:52:11'),
(200, '2003-12-16 11:50:33', 'F', 14.30, '2003-12-16 16:52:11'),
(201, '2003-12-16 11:50:33', 'FBST', 6.59, '2003-12-16 16:52:11'),
(202, '2003-12-16 11:50:33', 'FRK', 56.06, '2003-12-16 16:52:11'),
(203, '2003-12-16 11:50:33', 'FSHOX', 33.74, '2003-12-16 16:52:12'),
(204, '2003-12-16 11:50:33', 'GEHL', 12.47, '2003-12-16 16:52:12'),
(205, '2003-12-16 11:50:33', 'GM', 49.47, '2003-12-16 16:52:12'),
(206, '2003-12-16 11:50:33', 'GRIF', 14.35, '2003-12-16 16:52:12'),
(207, '2003-12-16 11:50:33', 'GVA', 23.52, '2003-12-16 16:52:13'),
(208, '2003-12-16 11:50:33', 'HAN', 34.25, '2003-12-16 16:52:13'),
(209, '2003-12-16 11:50:33', 'HBP', 2.85, '2003-12-16 16:52:13'),
(210, '2003-12-16 11:50:33', 'HD', 34.20, '2003-12-16 16:52:13'),
(211, '2003-12-16 11:50:33', 'HORT', 4.52, '2003-12-16 16:52:14'),
(212, '2003-12-16 11:50:33', 'JCTCF', 4.80, '2003-12-16 16:52:14'),
(213, '2003-12-16 11:50:33', 'KNUR.OB', 0.20, '2003-12-16 16:52:14'),
(214, '2003-12-16 11:50:33', 'LAF', 38.90, '2003-12-16 16:52:14'),
(215, '2003-12-16 11:50:33', 'LMT', 48.96, '2003-12-16 16:52:15'),
(216, '2003-12-16 11:50:33', 'LOW', 53.13, '2003-12-16 16:52:15'),
(217, '2003-12-16 11:50:33', 'LSCO', 13.93, '2003-12-16 16:52:15'),
(218, '2003-12-16 11:50:33', 'MHCO.PK', 1.60, '2003-12-16 16:52:15'),
(219, '2003-12-16 11:50:33', 'MHP', 68.12, '2003-12-16 16:52:16'),
(220, '2003-12-16 11:50:33', 'MKTAY', 9.55, '2003-12-16 16:52:16'),
(221, '2003-12-16 11:50:33', 'MLM', 45.59, '2003-12-16 16:52:16'),
(222, '2003-12-16 11:50:33', 'MRGO', 7.11, '2003-12-16 16:52:16'),
(223, '2003-12-16 11:50:33', 'MVCO', 1.60, '2003-12-16 16:52:17'),
(224, '2003-12-16 11:50:33', 'NEQS.PK', 0.05, '2003-12-16 16:52:17'),
(225, '2003-12-16 11:50:33', 'NWL', 21.79, '2003-12-16 16:52:17'),
(226, '2003-12-16 11:50:33', 'PHHM', 17.67, '2003-12-16 16:52:17'),
(227, '2003-12-16 11:50:33', 'POOL', 31.92, '2003-12-16 16:52:18'),
(228, '2003-12-16 11:50:33', 'RMIX', 6.23, '2003-12-16 16:52:18'),
(229, '2003-12-16 11:50:33', 'SEHI', 2.15, '2003-12-16 16:52:18'),
(230, '2003-12-16 11:50:33', 'SNA', 30.79, '2003-12-16 16:52:18'),
(231, '2003-12-16 11:50:33', 'TECUA', 45.83, '2003-12-16 16:52:19'),
(232, '2003-12-16 11:50:33', 'TII', 34.50, '2003-12-16 16:52:19'),
(233, '2003-12-16 11:50:33', 'TSCO', 39.20, '2003-12-16 16:52:19'),
(234, '2003-12-16 11:50:33', 'TTC', 46.07, '2003-12-16 16:52:19'),
(235, '2003-12-16 11:50:33', 'URI', 18.75, '2003-12-16 16:52:20'),
(236, '2003-12-16 11:50:33', 'USHG.OB', 0.55, '2003-12-16 16:52:20'),
(237, '2003-12-16 11:50:33', 'VOLVY', 30.06, '2003-12-16 16:52:20'),
(238, '2003-12-16 11:50:33', 'WIKS.OB', 0.35, '2003-12-16 16:52:20'),
(239, '2003-12-16 11:50:33', 'WMSI', 4.07, '2003-12-16 16:52:21'),
(240, '2003-12-16 11:50:33', 'WPEC.OB', 0.35, '2003-12-16 16:52:21'),
(241, '2003-12-16 11:55:30', 'ACMTA', 11.65, '2003-12-16 16:57:03'),
(242, '2003-12-16 11:55:30', 'ACO', 16.59, '2003-12-16 16:57:03'),
(243, '2003-12-16 11:55:30', 'ADLTQ.OB', 0.31, '2003-12-16 16:57:03'),
(244, '2003-12-16 11:55:30', 'AHH', 20.94, '2003-12-16 16:57:03'),
(245, '2003-12-16 11:55:30', 'ALG', 15.20, '2003-12-16 16:57:03'),
(246, '2003-12-16 11:55:30', 'AMN', 35.07, '2003-12-16 16:57:03'),
(247, '2003-12-16 11:55:30', 'ARTW', 4.53, '2003-12-16 16:57:03'),
(248, '2003-12-16 11:55:30', 'ASVI', 30.72, '2003-12-16 16:57:04'),
(249, '2003-12-16 11:55:30', 'BZH', 99.99, '2003-12-16 16:57:04'),
(250, '2003-12-16 11:55:30', 'CALC', 10.85, '2003-12-16 16:57:04'),
(251, '2003-12-16 11:55:30', 'CAT', 80.31, '2003-12-16 16:57:04'),
(252, '2003-12-16 11:55:30', 'CAV', 3.00, '2003-12-16 16:57:04'),
(253, '2003-12-16 11:55:30', 'CENT', 26.20, '2003-12-16 16:57:04'),
(254, '2003-12-16 11:55:30', 'CLWY', 0.46, '2003-12-16 16:57:04'),
(255, '2003-12-16 11:55:30', 'CNH', 15.34, '2003-12-16 16:57:04'),
(256, '2003-12-16 11:55:30', 'DE', 63.86, '2003-12-16 16:57:05'),
(257, '2003-12-16 11:55:30', 'DEVC', 7.12, '2003-12-16 16:57:05'),
(258, '2003-12-16 11:55:30', 'DOW', 40.00, '2003-12-16 16:57:05'),
(259, '2003-12-16 11:55:30', 'ERSN.PK', 0.00, '2003-12-16 16:57:05'),
(260, '2003-12-16 11:55:30', 'F', 14.30, '2003-12-16 16:57:05'),
(261, '2003-12-16 11:55:30', 'FBST', 6.59, '2003-12-16 16:57:05'),
(262, '2003-12-16 11:55:30', 'FRK', 56.04, '2003-12-16 16:57:05'),
(263, '2003-12-16 11:55:30', 'FSHOX', 33.74, '2003-12-16 16:57:05'),
(264, '2003-12-16 11:55:30', 'GEHL', 12.47, '2003-12-16 16:57:05'),
(265, '2003-12-16 11:55:30', 'GM', 49.48, '2003-12-16 16:57:06'),
(266, '2003-12-16 11:55:30', 'GRIF', 14.35, '2003-12-16 16:57:06'),
(267, '2003-12-16 11:55:30', 'GVA', 23.50, '2003-12-16 16:57:06'),
(268, '2003-12-16 11:55:30', 'HAN', 34.25, '2003-12-16 16:57:06'),
(269, '2003-12-16 11:55:30', 'HBP', 2.85, '2003-12-16 16:57:06'),
(270, '2003-12-16 11:55:30', 'HD', 34.16, '2003-12-16 16:57:06'),
(271, '2003-12-16 11:55:30', 'HORT', 4.52, '2003-12-16 16:57:06'),
(272, '2003-12-16 11:55:30', 'JCTCF', 4.80, '2003-12-16 16:57:06'),
(273, '2003-12-16 11:55:30', 'KNUR.OB', 0.19, '2003-12-16 16:57:07'),
(274, '2003-12-16 11:55:30', 'LAF', 38.95, '2003-12-16 16:57:07'),
(275, '2003-12-16 11:55:30', 'LMT', 48.95, '2003-12-16 16:57:07'),
(276, '2003-12-16 11:55:30', 'LOW', 53.18, '2003-12-16 16:57:07'),
(277, '2003-12-16 11:55:30', 'LSCO', 13.93, '2003-12-16 16:57:07'),
(278, '2003-12-16 11:55:30', 'MHCO.PK', 1.60, '2003-12-16 16:57:07'),
(279, '2003-12-16 11:55:30', 'MHP', 68.10, '2003-12-16 16:57:07'),
(280, '2003-12-16 11:55:30', 'MKTAY', 9.55, '2003-12-16 16:57:07'),
(281, '2003-12-16 11:55:30', 'MLM', 45.56, '2003-12-16 16:57:08'),
(282, '2003-12-16 11:55:30', 'MRGO', 7.11, '2003-12-16 16:57:08'),
(283, '2003-12-16 11:55:30', 'MVCO', 1.60, '2003-12-16 16:57:08'),
(284, '2003-12-16 11:55:30', 'NEQS.PK', 0.05, '2003-12-16 16:57:08'),
(285, '2003-12-16 11:55:30', 'NWL', 21.70, '2003-12-16 16:57:08'),
(286, '2003-12-16 11:55:30', 'PHHM', 17.67, '2003-12-16 16:57:08'),
(287, '2003-12-16 11:55:30', 'POOL', 31.92, '2003-12-16 16:57:08'),
(288, '2003-12-16 11:55:30', 'RMIX', 6.25, '2003-12-16 16:57:08'),
(289, '2003-12-16 11:55:30', 'SEHI', 2.15, '2003-12-16 16:57:09'),
(290, '2003-12-16 11:55:30', 'SNA', 30.79, '2003-12-16 16:57:09'),
(291, '2003-12-16 11:55:30', 'TECUA', 45.95, '2003-12-16 16:57:09'),
(292, '2003-12-16 11:55:30', 'TII', 34.50, '2003-12-16 16:57:09'),
(293, '2003-12-16 11:55:30', 'TSCO', 39.27, '2003-12-16 16:57:09'),
(294, '2003-12-16 11:55:30', 'TTC', 46.13, '2003-12-16 16:57:09');

我正在使用此查询:

SELECT week( batchID ) Week, year( batchID ) Year, month( batchID )
MONTH , SUM( price ) WeekAvg, ticker,batchID,
WEEK(batchID,5) - 
WEEK(DATE_SUB(batchID, INTERVAL DAYOFMONTH(batchID)-1 DAY),5)+1 as    WeekNumber
FROM lcsi_new_entry
WHERE batchID >= '2015-01-1 00:00:00'
AND batchID <= '2015-03-30 00:00:00'
GROUP BY week( batchID ) , year( batchID )  ,ticker
ORDER BY `batchID` DESC

2 个答案:

答案 0 :(得分:0)

SELECT WEEKOFYEAR("2015-02-01"); // = 5

这将为您提供2015年2月1日(5)的周数

SELECT WEEKOFYEAR("2015-02-19"); // = 8

这将为您提供2月19日的周数(8) 所以,feb 19th是在2015年2月的第四周......

2月1日是星期天。第五周的最后一天。 2月2日是在第6周......现在我们已经是4号了。只需进行这个小计算并添加1!

答案 1 :(得分:0)

您一直在使用&#34;模式5&#34;本周计算即

  1. 一周从星期一开始,
  2. 第1周从今年的第一个星期一开始。
  3. 更改这些因素会改变计算结果。

    SQL Fiddle

    MySQL 5.5.32架构设置

    CREATE TABLE Table1
        (`dt1` datetime, `dt2` datetime, `mode` int)
    ;
    
    INSERT INTO Table1
        (`dt1`, `dt2`, `mode`)
    VALUES
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 1),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 2),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 3),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 4),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 5),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 6),
        ('2015-02-01 00:00:00', '2015-02-19 00:00:00', 7)
    ;
    
    CREATE TABLE WeekModes
        (`Mode` int, `First_day` varchar(6), `Range` varchar(4), `Week_1_is` varchar(29))
    ;
    
    INSERT INTO WeekModes
        (`Mode`, `First_day`, `Range`, `Week_1_is`)
    VALUES
        (0, 'Sunday', '0-53', 'with a Sunday in this year'),
        (1, 'Monday', '0-53', 'with 4 or more days this year'),
        (2, 'Sunday', '1-53', 'with a Sunday in this year'),
        (3, 'Monday', '1-53', 'with 4 or more days this year'),
        (4, 'Sunday', '0-53', 'with 4 or more days this year'),
        (5, 'Monday', '0-53', 'with a Monday in this year'),
        (6, 'Sunday', '1-53', 'with 4 or more days this year'),
        (7, 'Monday', '1-53', 'with a Monday in this year')
    ;
    

    查询1

    SELECT 
          t1.dt1, t1.dt2, t1.mode, week(t1.dt1,t1.mode), week(t1.dt2,t1.mode), w.First_day, w.Week_1_is
    FROM table1 t1
    inner join weekmodes w on t1.mode = w.mode
    

    <强> Results

    |                             DT1 |                             DT2 | MODE | WEEK(T1.DT1,T1.MODE) | WEEK(T1.DT2,T1.MODE) | FIRST_DAY |                     WEEK_1_IS |
    |---------------------------------|---------------------------------|------|----------------------|----------------------|-----------|-------------------------------|
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    1 |                    5 |                    8 |    Monday | with 4 or more days this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    2 |                    5 |                    7 |    Sunday |    with a Sunday in this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    3 |                    5 |                    8 |    Monday | with 4 or more days this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    4 |                    5 |                    7 |    Sunday | with 4 or more days this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    5 |                    4 |                    7 |    Monday |    with a Monday in this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    6 |                    5 |                    7 |    Sunday | with 4 or more days this year |
    | February, 01 2015 00:00:00+0000 | February, 19 2015 00:00:00+0000 |    7 |                    4 |                    7 |    Monday |    with a Monday in this year |