如何select公司有两个组

How to select company which have two groups

我仍然尝试了 select 两组中的所有客户。来自客户的重复是正常的,因为select来自发票,但我需要知道上半年有一个组,下半年跳到另一个组的客户。

示例:

SELECT
  f.eankod as kod,                                              --(groups)
  ad.kod as firma,                                              --(markComp)
  f.nazfirmy as nazev,                                          --(nameComp)
  COUNT(ad.kod),
  sum(f.sumZklZakl + f.sumZklSniz + f.sumOsv) as cena_bez_dph   --(Price)
FROM
  ddoklfak as f
LEFT OUTER JOIN aadresar ad ON ad.idfirmy = f.idfirmy 
WHERE
  f.datvyst >= '2017-01-01'
  and f.datvyst <= '2017-12-31'
  and f.modul like 'FAV'
GROUP BY
  f.eankod,
  ad.kod,
  f.nazfirmy
HAVING COUNT (ad.kod) > 1
order by
  ad.kod

结果:

 GROUP   markcomp  nameComp    price
| D002 | B5846 | Cosmopolis | price ... |
| D003 | B6987 | Tismotis   | price ... |
| D009 | B8974 | Teramis    | price ... |
| D006 | B8876 | Kesmethis  | price ... | I need this, same company but diferent group, because this 
| D008 | B8876 | Kesmethis  | price ... | company jumped. I need know only jumped company. (last two rows from examples)

感谢帮助。

您可以使用 CTE 找出哪些 nameComp 出现了多次,并只保留那些。例如:

with
x as (
  -- your query
)
select * from x where nameComp in (
  select nameComp from x group by nameComp having count(*) > 1
)