Насколько я могу собрать, вы ищете
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 <> скрипка здесь