我要试着说清楚。
首先是表格:
CARROSSERIE
MARQ MODEL SILHOUETTE ID
citroen c3 coupe 1
citroen c3 sport 2
citroen c4 coupe 3
citroen c4 sport 4
acura cdx cuv 5
... ... ... ...
table2与table1具有相同的字段,但是有更多字段(只有1个字段很有趣)
alltable
SAME AS TABLE1 zone
... EUR
... EUR
... USA
... RUS
... CHI
... ...
所以我只是简单地将table1和table2加入到他们的相似字段中以获得" zone"来自table2。
以下是查询:
SELECT C.model_marq AS model_marq, C.model_name AS model_name, C.silhouette AS silhouette, IIF(T.ZONE IS NULL, 'ROW', T.ZONE) AS zone
INTO zone_vehicule
FROM CARROSSERIE C
LEFT JOIN alltable T
ON C.model_marq= T.MARQUE
AND C.model_name = T.MODELE
AND C.silhouette = T.CARROS
GROUP BY model_marq, model_name, silhouette, zone
我获得了良好的结果,因为我得到了所有的"区域"来自table2,取决于table1的字段。 问题是我需要在这个查询中处理一些事情:
如果来自table1的汽车(字段model_marq,model_name,silhouette)与table2位于7个不同的区域(7个区域实际上是所有可能的区域),我会进入结果集:
MARQ MODEL SILHOUETTE zone
citroen c3 coupe EUR
citroen c3 coupe CHIN
citroen c3 coupe ASI
citroen c3 coupe RUS
citroen c3 coupe AML
... ... ... etc(all the 7 zones)
这是完全正常的查询我正在使用。 我想要的是,如果查询返回这种情况(汽车在所有7个区域中),而不是在结果集中有7行与所有不同的区域,我只想要1行' *&#39 ;在区域字段中(事实上,只有当它在所有区域中时,它才会合并1行中的7行)
我需要的结果示例:
MARQ MODEL SILHOUETTE zone
citroen c3 coupe * (because it was in all the 7 zones)
citroen c3 sport CHIN
citroen c3 sport RUS
citroen c3 sport EUR
citroen c4 cuv AML
acura cdx sport *
acura cdx coupe EUR
... ... ... ...
我不知道从哪里开始。 我希望能够明白这一点。随意提问。
答案 0 :(得分:1)
我的方法使用子查询来查找列出了7个区域的汽车,然后使用该子查询的结果通知主查询的IIF语句确定“区域”字段。根据您的数据,它会做出一些可能无法证明是正确的假设,例如alltable
有carId
类似于您需要查看的每辆汽车的ID,以及如果计算每个carId
的行数,它将返回不同区域的数量。您可以根据实际情况根据需要调整查询,但希望概念很明确:
SELECT C.model_marq AS model_marq, C.model_name AS model_name, C.silhouette AS silhouette, IIF(T.ZONE IS NULL, 'ROW', iff(allZones.carId is not null, "*", T.ZONE) AS zone
INTO zone_vehicule
FROM (CARROSSERIE C
LEFT JOIN alltable T
ON C.model_marq= T.MARQUE
AND C.model_name = T.MODELE
AND C.silhouette = T.CARROS) Left join
(select distinct carId, count(carId)
from alltable
group by carId
Having count(carId) = 7) as allZones on t.carId = allZones.carId
GROUP BY model_marq, model_name, silhouette, zone
这是子查询:
(select distinct carId, count(carId)
from alltable
group by carId
Having count(carId) = 7) as allZones
您可以使用并单独运行以评估结果的准确性。
答案 1 :(得分:0)
我在这台PC上没有SQL,但你需要在第一次查询时使用RANK和PARTITION。然后,您可以操纵以获取*记录。
使用RANK / PARTITION的示例:http://www.besttechtools.com/articles/article/sql-rank-functions
答案 2 :(得分:0)
从zone
删除group by
,并在选择列表中使用此表达式:
case count(T.ZONE) when 0 then 'ROW' when 1 then min(T.ZONE) when 7 then '*' end
如果这对Access不起作用,您可以将其转换为:
iif(count(T.ZONE) = 0, 'ROW',
iif(count(T.ZONE) = 1, min(T.ZONE),
iif(count(T.ZONE) = 7, '*', '')))
答案 3 :(得分:0)
这对我有用,但我觉得它有点复杂,而且我的需要很长。我会尝试使用@TPhe答案来改进它。
SELECT model_marq, model_name, silhouette, zone into ZONE_MODELE FROM
(SELECT C.marque AS model_marq, C.modele AS model_name, C.carros AS silhouette, IIF(T.ZONE IS NULL, IIF(model_name = 'DIVERS', '*', 'ROW'), IIF(C.countzones = 7, '*', T.ZONE)) AS zone, C.countzones as countzones FROM (SELECT marque, modele, carros, count(*) as countzones FROM (SELECT marque, modele, carros, zone from (SELECT CA.model_marq AS marque, CA.model_name AS modele, CA.silhouette AS carros, IIF(TMP.ZONE IS NULL, 'ROW', TMP.ZONE) AS zone FROM CARROSSERIE CA LEFT JOIN alltable TMP ON CA.model_marq = TMP.MARQUE AND CA.model_name = TMP.MODELE AND CA.silhouette = TMP.CARROS GROUP BY CA.model_marq, CA.model_name, CA.silhouette, TMP.ZONE ) group by marque, modele, carros, zone) a GROUP BY marque, modele, carros) C LEFT JOIN alltable T ON C.marque= T.MARQUE AND C.modele = T.MODELE AND C.carros = T.CARROS GROUP BY C.marque, C.modele, C.carros, zone, countzones) TMP2 GROUP BY model_marq, model_name, silhouette, zone
如果有人知道如何制作相同的简化查询,我将不胜感激