SELECT
tn.DELIVERY_TYPE,
tn.ROUTE_TYPE,
tn.VEHICLE_TYPE,
STR_TO_DATE(iblock_props.PROPERTY_275, '%Y-%m-%d') AS `period`,
MIN(STR_TO_DATE(iblock_props.PROPERTY_275, '%Y-%m-%d')) AS `period_start`,
SUM(tn.GOODS_VOLUME) AS `sum`,
COUNT(*) AS `cnt`,
COUNT(DISTINCT (TENDER_ID)) AS `cnt_race`,
GROUP_CONCAT(DISTINCT (RACE_ID)) AS `races`
FROM `iek_logistic_tn` AS `tn` LEFT JOIN `b_iblock_element_prop_s63` AS `iblock_props`
ON (iblock_props.IBLOCK_ELEMENT_ID = tn.TENDER_ID)
WHERE tn.VEHICLE_TYPE IN (513)
AND (tn.LOADING_DISTRICT IN
(353558, 1891915, 870136, 353560, 608578, 1527532, 2110360, 2915850, 498742, 3033679, 813568, 617218, 3787273, 672014, 613142, 353559, 353561, 353562, 472605, 2482461, 1332770, 912165, 3355175, 400424, 3355177, 626219, 2390060, 2963246, 3433264, 2563894, 2146616, 1674059, 1196620, 3761229, 3731022, 1098319, 725586, 1196629, 605271, 605272, 605273, 605276, 3568477, 685665, 1215842, 1516643, 3280234, 705649, 1827187, 422019, 3474309, 426374, 1411720, 2586001, 1255060, 1226132, 3789978, 3471521, 3263910, 3414183, 2658219, 1196470, 524472, 1877945, 526012, 568769, 3115202, 539332, 2139844, 1435597, 3214038, 616664, 2977758, 2376161, 965601, 3145697, 1261049, 3517179)
OR LOADING_DISTRICT_2 IN
(353558, 1891915, 870136, 353560, 608578, 1527532, 2110360, 2915850, 498742, 3033679, 813568, 617218, 3787273, 672014, 613142, 353559, 353561, 353562, 472605, 2482461, 1332770, 912165, 3355175, 400424, 3355177, 626219, 2390060, 2963246, 3433264, 2563894, 2146616, 1674059, 1196620, 3761229, 3731022, 1098319, 725586, 1196629, 605271, 605272, 605273, 605276, 3568477, 685665, 1215842, 1516643, 3280234, 705649, 1827187, 422019, 3474309, 426374, 1411720, 2586001, 1255060, 1226132, 3789978, 3471521, 3263910, 3414183, 2658219, 1196470, 524472, 1877945, 526012, 568769, 3115202, 539332, 2139844, 1435597, 3214038, 616664, 2977758, 2376161, 965601, 3145697, 1261049, 3517179))
AND tn.UNLOADING_CITY IN
(2052491, 353458, 2721586, 353519, 353480, 353481, 2724695, 353459, 353460, 353461, 2938654, 353462, 353463, 353520, 1997510, 353483, 2025435, 2813898, 353464, 1634319, 2563447, 3121371, 1766825, 2823515, 353465, 353466, 353467, 353521, 353468, 353469, 353470, 353522, 3807965, 3695948, 353523, 1767139, 353524, 353471, 353472, 353473, 353484, 353474, 1185102, 353525, 353475, 353485, 353476, 353526, 353477, 353527, 852485, 3272181, 353528, 353478, 483399, 483400, 353482, 2222239, 353479, 3143681, 353486, 353529, 3113415, 475745, 353487, 353488, 353530, 353531, 353532, 353533, 353534, 353454, 568772, 353489, 3173325, 353535, 2951543, 2312866, 2813420, 353536, 353537, 353538, 353490, 1215252, 1827836, 353491, 353457, 353492, 353539, 353493, 2563449, 353540, 353494, 1114095, 353541, 353455, 353495, 353496, 353497, 353542, 353498, 1490648, 353499, 353500, 353543, 353501, 2711933, 353456, 353544, 2799133, 353502, 3173335, 353503, 353504, 2375573, 353545, 353546, 353547, 353548, 353549, 353550, 353505, 353506, 353507, 353508, 353509, 353510, 353511, 353551, 353512, 353513, 353514, 353552, 353553, 353515, 353554, 353516, 353555, 353556, 1435956, 353557, 353517, 353518)
AND tn.ROUTE_TYPE IN (1, 2)
AND tn.DELIVERY_TYPE IN (1, 2, 3)
AND tn.COMPANY_ID IN
(6, 291, 595, 35, 624, 409, 154, 438, 592, 76, 475, 328, 401, 326, 325, 318, 319, 207, 188, 151, 304, 490, 495, 496, 531, 166, 61, 56, 557, 332, 179, 47, 352, 100, 174, 555, 514, 169, 227, 441, 114, 120, 99, 472, 290, 446, 637, 469, 145, 621, 41, 91, 528, 597, 124, 578, 439, 245, 212, 512, 611, 327, 361, 37, 625, 132, 49, 288, 146, 266, 129, 320, 394, 137, 633, 136, 93, 640, 580, 406, 351, 420, 263, 453, 187, 234, 233, 457, 257, 111, 66, 366, 378, 203, 155, 345, 164, 139, 631, 482, 484, 391, 284, 221, 397, 242, 210, 492, 615, 123, 177, 634, 605, 78, 83, 38, 158, 252, 23, 474, 226, 222, 21, 568, 460, 398, 382, 11, 425, 217, 560, 413, 396, 389, 423, 609, 223, 292, 274, 275, 443, 598, 60, 230, 109, 561, 165, 10, 264, 110, 201, 287, 251, 220, 534, 198, 487, 435, 300, 40, 587, 72, 537, 480, 323, 324, 117, 88, 358, 610, 29, 437, 30, 429, 567, 65, 525, 419, 422, 494, 416, 183, 354, 94, 556, 208, 184, 466, 26, 22, 52, 20, 500, 62, 67, 392, 421, 209, 380, 368, 614, 433, 549, 125, 69, 465, 436, 563, 414, 506, 176, 199, 353, 502, 387, 162, 57, 148, 294, 338, 452, 459, 428, 628, 341, 281, 231, 399, 107, 468, 604, 73, 147, 170, 138, 451, 519, 265, 44, 381, 150, 261, 599, 430, 225, 115, 347, 533, 178, 511, 204, 375, 447, 86, 426, 247, 34, 489, 171, 68, 629, 118, 377, 116, 58, 92, 641, 297, 135, 85, 101, 75, 335, 119, 276, 277, 127, 293, 594, 96, 105, 149, 509, 205, 586, 258, 333, 33, 163, 219, 299, 283, 282, 544, 191, 121, 298, 53, 639, 46, 182, 559, 13, 395, 488, 562, 405, 590, 87, 142, 310, 309, 168, 238, 372, 289, 103, 643, 64, 200, 362, 19, 376, 539, 18, 571, 8, 493, 348, 89, 334, 280, 558, 613, 607, 346, 410, 589, 355, 636, 1, 342, 2, 25, 32, 195, 476, 551, 572, 464, 16, 50, 620, 575, 337, 424, 97, 418, 445, 190, 193, 383, 363, 507, 185, 547, 526, 180, 517, 113, 256, 175, 71, 63, 523, 499, 530, 582, 301, 5, 232, 202, 623, 497, 74, 504, 478, 552, 553, 79, 540, 248, 583, 627, 82, 479, 456, 240, 386, 303, 196, 128, 384, 42, 477, 315, 141, 70, 570, 216, 24, 77, 106, 267, 239, 39, 541, 520, 608, 622, 461, 268, 269, 173, 286, 192, 642, 619, 95, 360, 498, 160, 157, 350, 585, 90, 81, 612, 156, 444, 616, 359, 458, 98, 404, 108, 618, 510, 393, 54, 408, 388, 584, 516, 440, 317, 316, 339, 370, 17, 371, 579, 573, 577, 602, 603, 566, 215, 638, 632, 635, 349, 343, 340, 336, 144, 462, 228, 593, 442, 262, 134, 12, 417, 161, 390, 415, 591, 102, 296, 295, 427, 9, 51, 515, 565, 143, 181, 122, 152, 213, 471, 278, 7, 285, 467, 473, 367, 521, 513, 250, 237, 576, 306, 305, 243, 312, 311, 229, 211, 302, 470, 407, 483, 431, 403, 218, 84, 253, 485, 356, 501, 569, 271, 270, 206, 463, 259, 491, 307, 14, 36, 55, 28, 15, 4, 3, 550, 133, 581, 400, 194, 402, 535, 432, 508, 529, 522, 172, 329, 542, 546, 536, 322, 554, 374, 186, 434, 606, 224, 385, 503, 601, 189, 626, 43, 630, 80, 600, 214, 455, 260, 357, 373, 543, 330, 344, 59, 449, 112, 31, 45, 412, 331, 538, 588, 197, 454, 27, 545, 524, 617, 486, 126, 527, 481, 48, 364, 241, 574, 130, 244, 448, 313, 314, 321, 167, 564, 411, 131, 272, 273, 450, 596, 365, 548, 308, 518, 505, 159, 153, 532, 249, 104, 255, 235, 279, 254, 236, 379, 369, 246, 140)
AND `iblock_props.PERIOD` BETWEEN '2017.08.21' AND '2017.08.21'
GROUP BY tn.DELIVERY_TYPE, tn.ROUTE_TYPE, tn.VEHICLE_TYPE, `iblock_props.PERIOD` ASC WITH ROLLUP