我有两个表格。They are in Heidi 我需要像Rezult
这样的表格我尝试下一个SELECT:
SELECT accounts.name, accounts.account_id,date,
(case when accounts.account_id = results.account_id then date end) AS Dat,
results.account_id
FROM results
INNER JOIN accounts
group by date,dat,name
order by accounts.account_id ,date
但我有不必要的领域。帮助摆脱它们。
此外: 我必须这样表格:
--------------------01.2010----02.2010-----03.2010---nextDate
Revenues ----------'x'------------'x'---------'null'---------------------
Personal Costs-----'x'------------'x'---------'x'---------------------
Amortisation---------'x'------------'x'---------'null'---------------------
Summa---------------'3x'-----------'3x'---------'1x'---------------------
我使用下一个选择:
SELECT
GROUP_CONCAT(revenues SEPARATOR ';')as Renenues,
GROUP_CONCAT(personal SEPARATOR ';')as Personal,
GROUP_CONCAT(amortisation SEPARATOR ';')as Amortisation
from (SELECT date,
sum(case when accounts.account_id = 1 then 1 else 0 end) as revenues,
sum(case when accounts.account_id = 2 then 1 else 0 end) as personal ,
sum(case when accounts.account_id = 3 then 1 else 0 end) as amortisation
from accounts
left JOIN results on accounts.account_id = results.account_id
group by date
) t
UNION ALL
SELECT
sum(case when results.account_id = 1 then 1 ELSE 0 end) ,
sum(case when results.account_id = 2 then 1 else 0 end) ,
sum(case when results.account_id = 3 then 1 else 0 end)
from results
然后我将从我的字符串中显示表格行:'GROUP_CONCAT'。 可以使SELECT不知道'accounts.account_id'的数量或优化这个SELECT? 我尝试了第一个SELECT但得到了很多不必要的信息。
答案 0 :(得分:0)
您应该将查询修改为
SELECT accounts.name,
accounts.account_id as accounts_account_id,
accounts.date,
results.account_id as results_account_id
FROM results
LEFT JOIN accounts ON accounts.account_id = results.account_id
group by accounts.date,accounts.name
order by accounts.account_id ,accounts.date;