我在access中写了一个查询,但是我不知道它是否正常(它可以工作),而且正如我的c ++程序员朋友所说的那样,“它先工作就可以了”,但是我对sql还是陌生的,我想改进它,而不是把东西砸下来。
我的代码包括将列中的行转换为列,并根据折扣标准检索每种产品的折扣总额。
TRANSFORM sum(c.[min value# %]) AS sum_min_value
SELECT c.supplier, c.sku, c.dept, c.UN, a.BRAND, sum(c.[min value# %]) AS
Total_of_discount
FROM (SELECT a.*, d.* FROM price AS a LEFT JOIN (SELECT * FROM CGF AS b
WHERE (b.variable="cmp" and b.order="1")) AS d ON IIf(d.brand="-",(Not
((d.[structure exclusion]=cstr(a.dept)) Or (d.[structure
exclusion]=cstr(a.un)))) And (d.structure=a.dept Or d.structure=a.un Or
d.structure=25) And (a.supplier=d.[# supplier]),(Not ((d.[structure
exclusion]=cstr(a.dept)) Or (d.[structure exclusion]=cstr(a.un)))) And
(d.structure=a.dept Or d.structure=a.un Or d.structure=25) And
(a.supplier=d.[# supplier]) And (d.brand=a.brand) And Not (d.[brand
exclusion]=a.brand))) AS c
GROUP BY c.supplier, c.sku, c.dept, c.un, a.BRAND
PIVOT c.[HEADING CGF];
我更怀疑的部分是if语句。你能帮忙吗ty
答案 0 :(得分:0)
我看不到“ ON”如何工作。请在此查询中发布示例输出行。
我希望看到“ a.somecolumn1 = d.somecolumn2)。
但是我看到的是IIF x,然后是一对类似的逻辑子句。将使用两个备选的join语句?
TRANSFORM SUM(c.[min value# %]) AS sum_min_value
SELECT c.supplier,
c.sku,
c.dept,
c.un,
a.brand,
SUM(c.[min value# %]) AS Total_of_discount
FROM (SELECT a.*,
d.*
FROM price AS a
LEFT JOIN (SELECT *
FROM cgf AS b
WHERE ( b.variable = "cmp" AND b.ORDER = "1" )
) AS d
ON Iif(d.brand = "-", ( NOT ( ( d.[structure exclusion] = Cstr(a.dept) )
OR( d.[structure exclusion] = Cstr(a.un) ) ) )
AND ( d.structure = a.dept
OR d.structure = a.un
OR d.structure = 25 )
AND ( a.supplier = d.[# supplier] )
,
( NOT ( ( d.[structure exclusion] = Cstr(a.dept) )
OR ( d.[structure exclusion] = Cstr(a.un) ) ) )
AND ( d.structure = a.dept
OR d.structure = a.un
OR d.structure = 25 )
AND ( a.supplier = d.[# supplier] )
AND ( d.brand = a.brand )
AND NOT ( d.[brand exclusion] = a.brand ))
) AS c
GROUP BY c.supplier,
c.sku,
c.dept,
c.un,
a.brand
PIVOT c.[HEADING CGF];