嗨,我是sql的初学者,我将非常感谢任何建议
我有主表
Address Val1 Mat
tbla 10 Mat1
tbla 2 Mat2
tbla 23 Mat3
tbla 5 Mat4
此行需要像这样每月在
中显示 Output
Address Val1 Mat MonthCreated
tbla 10 Mat1 January
tbla 2 Mat2 January
tbla 23 Mat3 January
tbla 5 Mat4 January
tbla 10 Mat1 February
tbla 2 Mat2 February
tbla 23 Mat3 February
tbla 5 Mat4 February
直到12月,但我需要从其他表中获得收入 我的查询就是这样
SELECT dttry.*, all_months.*
FROM (SELECT
t1.Address,t1.mat,
COUNT( DISTINCT t1.id ) AS `Val1`,t1.mat as 'Mat'
, COALESCE(i.Income , 0 ) AS `Income`
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id
AND mp.t_type = 'try'
AND mp.t_system_id = 'ok'
left join (
SELECT
CAST( SUM(r.t_payment_total) AS decimal(18, 2) ) AS `Income`
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status != 'Pending'
AND r.t_status != 'Booked'
AND r.c_mid = m.id AND (t.c_start_date) BETWEEN '2018/01/01' AND '2018/01/31'
GROUP BY
t.t_type_id
) as i on t1.t_type_id = i.t_type_id
->this is income for january
left join (
SELECT
CAST( SUM(r.t_payment_total) AS decimal(18, 2) ) AS `Income`
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status != 'Pending'
AND r.t_status != 'Booked'
AND r.c_mid = m.id AND (t.c_start_date) BETWEEN '2018/02/01' AND '2018/02/31'
GROUP BY
t.t_type_id
) as ifebruary on t1.t_type_id = i.t_type_id
->this is income for february
GROUP BY
t1.t_tool_type_id) as dttry
CROSS JOIN
(SELECT 'January' AS MonthCreated UNION ALL
SELECT 'February' UNION ALL
SELECT 'March' UNION ALL
SELECT 'April' UNION ALL
SELECT 'May' UNION ALL
SELECT 'June' UNION ALL
SELECT 'July' UNION ALL
SELECT 'August' UNION ALL
SELECT 'September' UNION ALL
SELECT 'October' UNION ALL
SELECT 'November' UNION ALL
SELECT 'December') AS all_months
Output
Address Val1 Mat Income MonthCreated
tbla 10 Mat1 0 January
tbla 2 Mat2 30 January
tbla 23 Mat3 10 January
tbla 5 Mat4 0 January
tbla 10 Mat1 0 February
tbla 2 Mat2 30 February
tbla 23 Mat3 10 February
tbla 5 Mat4 0 February
,但对于2月的收入列,它应与1月的收入列不同 应该是
income
1
2
7
8
Desired Output
Output
Address Val1 Income MonthCreated
tbla 10 0 January
tbla 2 30 January
tbla 23 10 January
tbla 5 0 January
tbla 10 1 February
tbla 2 2 February
tbla 23 7 February
tbla 5 8 February
答案 0 :(得分:1)
除非您可以提供所有表的示例数据,否则不可能给出完整的答案,但是几乎可以肯定,每个收入月份都不需要子查询,我可以通过合并月份数字而不是名称来简化生活,然后之后贴上标签。例如
drop table if exists maintable;
create table maintable(
Address varchar(10), Val1 int, Mat varchar(10));
insert into maintable values
( 'tbla' , 10 , 'Mat1'),
( 'tbla' , 2 , 'Mat2'),
( 'tbla' , 23 , 'Mat3'),
( 'tbla' , 5 , 'Mat4');
drop table if exists income;
create table income(id int auto_increment primary key, mat varchar(10),dt date,amount int);
insert into income (mat,dt,amount) values
('mat2','2018-01-01',10),('mat2','2018-01-01',20),('mat3','2018-01-01',10),
('mat1','2018-02-01',1),('mat2','2018-02-01',7),('mat3','2018-02-01',8),('mat4','2018-02-01',9),
('mat3','2018-10-01',10);
select s.address,s.mat,s.val1,
coalesce(i.income,0) income,
case when s.monthcreated = 1 then 'jan'
when s.monthcreated = 2 then 'feb'
when s.monthcreated = 3 then 'mar'
when s.monthcreated = 4 then 'apr'
when s.monthcreated = 5 then 'may'
when s.monthcreated = 6 then 'jun'
when s.monthcreated = 7 then 'jul'
when s.monthcreated = 8 then 'aug'
when s.monthcreated = 9 then 'sep'
when s.monthcreated = 10 then 'oct'
when s.monthcreated = 11 then 'nov'
when s.monthcreated = 1 then 'dec'
end as mmm
from
(
select all_months.* , maintable.*
from
(SELECT 1 AS MonthCreated UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12) AS all_months
cross join maintable
) s
left join
(
select mat mati,month(dt) mm ,sum(Amount) income
from income
where dt between '2018-01-01' and '2018-12-31'
group by mat,month(dt)
) i
on i.mati = s.mat and i.mm = s.monthcreated
order by s.monthcreated,s.mat;
“收入”子查询在哪里计算出每个席位的收入。
结果
+---------+------+------+--------+------+
| Address | Mat | Val1 | income | mmm |
+---------+------+------+--------+------+
| tbla | Mat1 | 10 | 0 | jan |
| tbla | Mat2 | 2 | 30 | jan |
| tbla | Mat3 | 23 | 10 | jan |
| tbla | Mat4 | 5 | 0 | jan |
| tbla | Mat1 | 10 | 1 | feb |
| tbla | Mat2 | 2 | 7 | feb |
| tbla | Mat3 | 23 | 8 | feb |
| tbla | Mat4 | 5 | 9 | feb |
| tbla | Mat1 | 10 | 0 | mar |
| tbla | Mat2 | 2 | 0 | mar |
| tbla | Mat3 | 23 | 0 | mar |
| tbla | Mat4 | 5 | 0 | mar |
| tbla | Mat1 | 10 | 0 | apr |
| tbla | Mat2 | 2 | 0 | apr |
| tbla | Mat3 | 23 | 0 | apr |
| tbla | Mat4 | 5 | 0 | apr |
| tbla | Mat1 | 10 | 0 | may |
| tbla | Mat2 | 2 | 0 | may |
| tbla | Mat3 | 23 | 0 | may |
| tbla | Mat4 | 5 | 0 | may |
| tbla | Mat1 | 10 | 0 | jun |
| tbla | Mat2 | 2 | 0 | jun |
| tbla | Mat3 | 23 | 0 | jun |
| tbla | Mat4 | 5 | 0 | jun |
| tbla | Mat1 | 10 | 0 | jul |
| tbla | Mat2 | 2 | 0 | jul |
| tbla | Mat3 | 23 | 0 | jul |
| tbla | Mat4 | 5 | 0 | jul |
| tbla | Mat1 | 10 | 0 | aug |
| tbla | Mat2 | 2 | 0 | aug |
| tbla | Mat3 | 23 | 0 | aug |
| tbla | Mat4 | 5 | 0 | aug |
| tbla | Mat1 | 10 | 0 | sep |
| tbla | Mat2 | 2 | 0 | sep |
| tbla | Mat3 | 23 | 0 | sep |
| tbla | Mat4 | 5 | 0 | sep |
| tbla | Mat1 | 10 | 0 | oct |
| tbla | Mat2 | 2 | 0 | oct |
| tbla | Mat3 | 23 | 10 | oct |
| tbla | Mat4 | 5 | 0 | oct |
| tbla | Mat1 | 10 | 0 | nov |
| tbla | Mat2 | 2 | 0 | nov |
| tbla | Mat3 | 23 | 0 | nov |
| tbla | Mat4 | 5 | 0 | nov |
| tbla | Mat1 | 10 | 0 | NULL |
| tbla | Mat2 | 2 | 0 | NULL |
| tbla | Mat3 | 23 | 0 | NULL |
| tbla | Mat4 | 5 | 0 | NULL |
+---------+------+------+--------+------+
48 rows in set (0.00 sec)