Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL SELECT在多个国家/地区提供的产品_Sql_Count_Distinct_Having - Fatal编程技术网

SQL SELECT在多个国家/地区提供的产品

SQL SELECT在多个国家/地区提供的产品,sql,count,distinct,having,Sql,Count,Distinct,Having,我有这张有市场和价格的水果表(这张表实际上只是摘录) 现在,我只想选择在德国、英国、IT和美国四个市场都有售的(或集团)产品。这将导致下表: Product Market Price Apple UK 4 Apple DE 5 Apple US 4 Apple IT 3 Peach IT 12 Peach DE 10 Peach UK 10 Peach US 11 我已经尝试过使用group by+have+Count Distinct,但它不

我有这张有市场和价格的水果表(这张表实际上只是摘录)

现在,我只想选择在德国、英国、IT和美国四个市场都有售的(或集团)产品。这将导致下表:

Product Market  Price
Apple   UK  4
Apple   DE  5
Apple   US  4
Apple   IT  3
Peach   IT  12
Peach   DE  10
Peach   UK  10
Peach   US  11
我已经尝试过使用group by+have+Count Distinct,但它不起作用。见下文

SELECT
market, product, AVG(price) as pr
FROM
    fruits
WHERE       
    market IN (DE, IT, UK, US)
GROUP BY
    market, product
HAVING Count (DISTINCT market=4)
我猜我使用计数的方式不对。
请帮忙。谢谢

拥有-条件
计数(不同市场=4)
应该是:

SELECT
market, product, AVG(price) as pr
FROM
    fruits
WHERE       
    market IN (DE, IT, UK, US)
GROUP BY
    market, product
HAVING Count (DISTINCT market) = 4

产品没有重复的国家/地区。我建议使用简单的窗口功能:

select f.*
from (select f.*, count(*) over (partition by product) as cnt
      from fruits f
      where market in ('DE', 'IT', 'UK', 'US')
     ) f
where cnt = 4;

我理解答案,但不理解投票结果<代码>计数(不同市场)在此查询中总是
1
select f.*
from (select f.*, count(*) over (partition by product) as cnt
      from fruits f
      where market in ('DE', 'IT', 'UK', 'US')
     ) f
where cnt = 4;