как получить результат по группам и количеству полей в MySql - PullRequest
0 голосов
/ 17 февраля 2020

моя таблица

CNT_ID  REGION   GRADE  MAX-CAPACITY  TOTAL-CAPACITY LASTDATE

1001    TN       FOOD   97             100            2020-02-17
1001    BG       BEV    76              90            2020-02-17
1001    KA       IND    88              90            2020-02-17
1001    BG       FOOD   91             100            2020-02-17
1001    ER       IND    90             100            2020-02-15
1001    TN       FOOD   78              80            2020-02-17
1001    TN       BEV    95             100            2020-02-17
1001    TN       FOOD   92             100            2020-02-16

Я хочу получить результат на основе ... списка по РЕГИОНАМ -> И СЧЕТА MaxCapacity из (FOOD, BEV, IND) И ПОРЯДОК ВЫПОЛНЕНИЯ LASTDATE des c

ожидаемый результат:

REGION  FOODGRADE   BEVGRADE    INDGRADE    TOTALCAPACITY   LEFT    LASTDATE
TN      267             95          0       380             18      2020-02-17
BG      91              76          0       190             23      2020-02-17
KA      0               0           88      90              2       2020-02-17
ER      0               0           90      100             10      2020-02-17

Я ПОПРОБОВАЛ СЛЕДУЮЩЕЕ ... но не повезло:

SELECT COUNT(IF(GRADE='FOOD',1,NULL)) 'Food', COUNT(IF(GRADE='BEV',1,NULL)) 'Beve', * 
FROM txn_viewinfo  
WHERE LASTDATE = (SELECT MAX(LASTDATE) 
                  FROM txn_viewinfo ) 
ORDER BY LASTDATE DESC;

Ответы [ 2 ]

1 голос
/ 17 февраля 2020

Насколько я могу собрать, вы ищете

SELECT
  tv.`REGION`
  , SUM(IF(`GRADE` = 'FOOD', 1,0)) Food
  , SUM(IF(`GRADE` = 'BEV', 1,0)) bev
  , SUM(IF(`GRADE` = 'IND', 1,0)) ind
  , MAX(`MAX-CAPACITY`)
  , MAX(`TOTAL-CAPACITY`)
  ,LASTDATE
FROM txn_viewinfo tv INNER JOIN (SELECT `REGION`,MAX(LASTDATE) maxldate 
                                 FROM txn_viewinfo 
                                 GROUP BY `REGION`)  ld
                     ON tv.`LASTDATE` = ld.maxldate AND tv.`REGION` = ld.`REGION`
GROUP BY LASTDATE,`REGION`
ORDER BY LASTDATE DESC;

Это дает вам счет оценки в последний день въезда

CREATE TABLE txn_viewinfo (
  `CNT_ID` INTEGER,
  `REGION` VARCHAR(2),
  `GRADE` VARCHAR(4),
  `MAX-CAPACITY` INTEGER,
  `TOTAL-CAPACITY` INTEGER,
  `LASTDATE` date
);

INSERT INTO txn_viewinfo
  (`CNT_ID`, `REGION`, `GRADE`, `MAX-CAPACITY`, `TOTAL-CAPACITY`, `LASTDATE`)
VALUES
  ('1001', 'TN', 'FOOD', '97', '100', '2020-02-17'),
  ('1001', 'BG', 'BEV', '76', '90', '2020-02-17'),
  ('1001', 'KA', 'IND', '88', '90', '2020-02-17'),
  ('1001', 'BG', 'FOOD', '91', '100', '2020-02-17'),
  ('1001', 'ER', 'IND', '90', '100', '2020-02-15'),
  ('1001', 'TN', 'FOOD', '78', '80', '2020-02-17'),
  ('1001', 'TN', 'BEV', '95', '100', '2020-02-17'),
  ('1001', 'TN', 'FOOD', '92', '100', '2020-02-16'),
  ('1001', 'TN', 'FOOD', '92', '100', '2020-02-16');
✓

✓
SELECT
  tv.`REGION`
  , SUM(IF(`GRADE` = 'FOOD', 1,0)) Food
  , SUM(IF(`GRADE` = 'BEV', 1,0)) bev
  , SUM(IF(`GRADE` = 'IND', 1,0)) ind
  , MAX(`MAX-CAPACITY`)
  , MAX(`TOTAL-CAPACITY`)
  ,LASTDATE
FROM txn_viewinfo tv INNER JOIN (SELECT `REGION`,MAX(LASTDATE) maxldate 
                                 FROM txn_viewinfo 
                                 GROUP BY `REGION`)  ld
                     ON tv.`LASTDATE` = ld.maxldate AND tv.`REGION` = ld.`REGION`

GROUP BY LASTDATE,`REGION`
ORDER BY LASTDATE DESC;
REGION | Food | bev | ind | MAX(`MAX-CAPACITY`) | MAX(`TOTAL-CAPACITY`) | LASTDATE  
:----- | ---: | --: | --: | ------------------: | --------------------: | :---------
BG     |    1 |   1 |   0 |                  91 |                   100 | 2020-02-17
KA     |    0 |   0 |   1 |                  88 |                    90 | 2020-02-17
TN     |    2 |   1 |   0 |                  97 |                   100 | 2020-02-17
ER     |    0 |   0 |   1 |                  90 |                   100 | 2020-02-15
SELECt tv.*,ld.*
FROM txn_viewinfo tv INNER JOIN (SELECT `REGION`,MAX(LASTDATE) maxldate 
                                 FROM txn_viewinfo 
                                 GROUP BY `REGION`)  ld
                     ON tv.`LASTDATE` = ld.maxldate AND tv.`REGION` = ld.`REGION`
#WHERE tv.`LASTDATE` = ld.maxldate
CNT_ID | REGION | GRADE | MAX-CAPACITY | TOTAL-CAPACITY | LASTDATE   | REGION | maxldate  
-----: | :----- | :---- | -----------: | -------------: | :--------- | :----- | :---------
  1001 | TN     | FOOD  |           97 |            100 | 2020-02-17 | TN     | 2020-02-17
  1001 | BG     | BEV   |           76 |             90 | 2020-02-17 | BG     | 2020-02-17
  1001 | KA     | IND   |           88 |             90 | 2020-02-17 | KA     | 2020-02-17
  1001 | BG     | FOOD  |           91 |            100 | 2020-02-17 | BG     | 2020-02-17
  1001 | ER     | IND   |           90 |            100 | 2020-02-15 | ER     | 2020-02-15
  1001 | TN     | FOOD  |           78 |             80 | 2020-02-17 | TN     | 2020-02-17
  1001 | TN     | BEV   |           95 |            100 | 2020-02-17 | TN     | 2020-02-17
SELECT COUNT(IF(GRADE='FOOD',1,NULL)) 'Food'
, COUNT(IF(GRADE='BEV',1,NULL)) 'Beve'
, * 
FROM txn_viewinfo  
WHERE LASTDATE = (SELECT MAX(LASTDATE) FROM txn_viewinfo ) ORDER BY LATEST_READTIME DESC;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* 
FROM txn_viewinfo  
WHERE LASTDATE = (SELECT MAX(LASTDATE) FROM txn_viewinfo ' at line 3

db <> скрипка здесь

1 голос
/ 17 февраля 2020

Вы можете использовать GROUP BY, как показано ниже:

select *
from (
  select
    region,
    sum(case when grade = 'FOOD' then max_capacity else 0 end) as foodgrade,
    sum(case when grade = 'BEV' then max_capacity else 0 end) as bevgrade,
    sum(case when grade = 'IND' then max_capacity else 0 end) as indgrade,
    sum(total_capacity) as totalcapacity,
    sum(total_capacity - max_capacity) as lft,
    max(lastdate) as lastdate
  from txn_viewinfo
  group by region
) x
order by lastdate
...