MySQL在另一个选择中选择

时间:2014-08-04 09:21:15

标签: mysql select join

我的查询如下

select
  Sum(If(departments.vat, If(weeklytransactions.weekendingdate Between
  '2011-01-04' And '2099-12-31', weeklytransactions.takings / 1.2,
  If(weeklytransactions.weekendingdate Between '2008-11-30' And '2010-01-01',
  weeklytransactions.takings / 1.15, weeklytransactions.takings / 1.175)),
  weeklytransactions.takings)) As Total,
  weeklytransactions.weekendingdate,......

和另一个返回增值税率如下

select format(Max(Distinct vat_rates.Vat_Rate),3) From vat_rates Where
vat_rates.Vat_From <= '2011-01-03'

我想用较低的查询替换硬编码的if语句,用lowertransactions.weekendingdate替换较低查询中的日期。

在Kevin的评论之后,这是我正在努力工作的完整查询;

Select Max(vat_rates.vat_rate) As r, 
If(departments.vat, weeklytransactions.takings / r, weeklytransactions.takings) As Total, 
weeklytransactions.weekendingdate, 
Week(weeklytransactions.weekendingdate), 
round(datediff(weekendingdate,     (if(month(weekendingdate)>5,concat(year(weekendingdate),'-06-01'),concat(year(weekendingdate)-1,'-06-01'))))/7,0)+1 as fyweek, 
cast((Case When Month(weeklytransactions.weekendingdate) >5 Then Concat(Year(weeklytransactions.weekendingdate), '-',Year(weeklytransactions.weekendingdate) + 1) Else Concat(Year(weeklytransactions.weekendingdate) - 1, '-',Year(weeklytransactions.weekendingdate)) End) as char) As fy,
business_units.business_unit 
From departments Inner Join (business_units Inner Join weeklytransactions On business_units.buID = weeklytransactions.businessUnit) On departments.deptid = weeklytransactions.departmentId 
Where (vat_rates.vat_from <= weeklytransactions.weekendingdate and business_units.Active = true and business_units.sales=1) 
Group By weeklytransactions.weekendingdate, business_units.business_unit Order By fy desc, business_unit, fyweek

此致 皮特

1 个答案:

答案 0 :(得分:0)

假设我正确地阅读了您的问题,您的问题是关于您的主查询结果是否已经返回另一个SELECT的结果(另外,取决于您对SQL的熟悉程度,也许您没有&# 39; t有机会了解JOIN s?)。

如果您在SELECT子句中定义了数据,则可以从FROM中提取数据。以下查询将起作用,例如:

SELECT A.a, B.b
FROM A
    JOIN (SELECT aggregate(c) FROM C) AS B

请注意,子查询中没有对表A的引用。事实是,你不能像查询一样添加它,因为子查询不知道它是一个子查询。所以下面的工作没有成功:

SELECT A.a, B.b
FROM A
    JOIN (SELECT aggregate(c) FROM C WHERE C.someValue = A.someValue) AS B

回到基础。你想在这里明显做的是聚合一些与另一个表的每个记录相关的数据。为此,您需要合并SELECT个查询并使用GROUP BY

SELECT A.a, aggregate(C.c)
FROM A, C
WHERE C.someValue = A.someValue
GROUP BY A.a

回到您的表格,以下内容应该有效:

SELECT w.weekendingdate, FORMAT(MAX(v.Vat_Rate, 3)
FROM weeklytransactions AS w, vat_rates AS v
WHERE v.Vat_From <= w.weekendingdate
GROUP BY w.weekendingdate

随意添加和删除您认为合适的字段和条件(我不会感到惊讶的是,当您从vat_rates过滤记录时,您也希望使用下限,因为我的方式如上所述,对于给定的周末日期,您可以获得该周+前几周的记录!)。


所以看起来我的第一次尝试没有解决实际问题。通过评论中提供的其他信息以及新的完整查询,让我们看看这是如何发生的。

我们仍然缺少错误消息,但通常所写的查询应该导致MySQL有以下投诉:

ERROR 1109 (42S02): Unknown table 'vat_rates' in field list

为什么呢?因为vat_rates表没有出现在FROM子句中,所以它应该出现在business_units子句中。通过简化查询,删除对SELECT MAX(vat_rates.vat_rate) AS r, IF(d.vat, w.takings / r, w.takings) AS Total FROM departments AS d INNER JOIN weeklytransactions AS w ON w.departmentId = d.deptid WHERE vat_rates.vat_from <= w.weekendingdate GROUP BY w.weekendingdate 表的所有引用以及不添加或删除任何问题的字段,计算和顺序,让我们更加明显,给我们留下以下内容:

FOREIGN ID

这不起作用,并会产生上述错误。看起来weeklytransactionsvat_rates表之间没有CROSS JOIN,所以我们别无选择,只能暂时做WHERE,希望条件在r子句和用于获取r的聚合函数足以满足此处的业务逻辑。以下查询应该返回预期的数据而不是错误消息(我也删除了SELECT IF(d.vat, w.takings / MAX(v.vat_rate), w.takings) AS Total FROM vat_rates AS v, departments AS d INNER JOIN weeklytransactions AS w ON w.departmentId = d.deptid WHERE v.vat_from <= w.weekendingdate GROUP BY w.weekendingdate ,因为这似乎是一个中间值,由所写的注释判断):

{{1}}

从那里开始,假设它有效,你只需要放回我删除的所有部分以获得最终查询。我对这里的增值税税率的方式有点怀疑,但我不知道你在这方面的要求是什么,所以我把它留给你,以确保按预期工作。