使用MS Query,以下代码有效,它找到“销售价格”模式并在Excel 2007中显示。
(Connection code here has been omitted)
SELECT TOP 1 "Sales Price" AS "SPrice Mode", COUNT(*) Frequency
FROM "Sales Table" WHERE ("Date of Sale" BETWEEN ? AND ?) AND (City LIKE ?)
GROUP BY "Sales Price"
ORDER BY Frequency DESC
Enter Start Date Enter END Date Enter City
我想修改此查询,以便它还可以找到2个其他字段的模式,“每单位价格”和“每SF的价格”,两者都在“销售价格”中的“销售表”中。此外,我希望用户只需输入一次参数。搜索网后,我找不到解决方案。我尝试过使用UNION和JOIN的变体,但无济于事。任何帮助或指导将不胜感激。提前谢谢。
答案 0 :(得分:0)
您可以使用窗口函数获取多列的模式。在以下查询中,最里面的查询计算每个值的计数。下一个子查询获取最大计数,最外面的查询获取最大计数的变量值:
select max(case when cnt_sp = max_cnt_sp then "Sales Price" end) as mod_Sales_Price,
max(case when cnt_ppu = max_cnt_ppu then "Price Per Unit" end) as mod_Price_Per_Unit,
max(case when cnt_ppsf = max_cnt_ppsf then "Price Per SF" end) as mod_Price_Per_SF
from (select st.*,
max(cnt_sp) over () as max_cnt_sp,
max(cnt_ppu) over () as max_cnt_ppu,
max(cnt_ppsf) over () as max_cnt_ppsf
from (select st.*,
count(*) over (partition by "Sales Price") as cnt_sp,
count(*) over (partition by "Price Per Unit") as cnt_ppu,
count(*) over (partition by "Price Per SF") as cnt_ppsf
from "Sales Table" st
) st
) st