一个sku可能有几行具有不同的partner_id。如何选择只有一条记录的skus?

时间:2017-07-23 09:39:23

标签: postgresql

我有这样的partner_stockrecord表:

"id"|"partner_sku"|"partner_id"
1      sku1          1
2      sku1          2

3      sku2          1
4      sku2          2
5      sku2          3

6      sku3          1

7      sku4          1

SELECT sku只有partner_id=1 AND partner_id=2 SELECTsku3如何sku4只有class BarThread extends Thread { ClassificationV4 classObj = new ClassificationV4(); JProgressBar progressBar; public BarThread(JProgressBar bar) { progressBar = bar; } public void run() { int minimum = 0; int maximum = classObj.getMaximumLength(); for (int i = minimum; i < maximum; i++) { try { int value = progressBar.getValue(); progressBar.setValue(value + 1); Thread.sleep(classObj.getSleepTime()); } catch (InterruptedException ignoredException) {} } } } //create anonymous thread new Thread() { public void run() { Thread stepper = new BarThread(jProgressBar1); stepper.start(); } }; 在这种情况下?

P.S。我确信这是一个重复,但我无法制定正确的搜索查询。

1 个答案:

答案 0 :(得分:1)

select partner_sku from partner_stockrecord as x
where not exists (
  select 1 from partner_stockrecord as y
  where x.partner_sku = y.partner_sku and y.partner_id = 1)
union
select partner_sku from partner_stockrecord as x
where not exists (
  select 1 from partner_stockrecord as y
  where x.partner_sku = y.partner_sku and y.partner_id = 2);

select partner_sku from partner_stockrecord
group by partner_sku
having not array_agg(partner_id) @> array[1,2];

select partner_sku from partner_stockrecord
group by partner_sku
having count(distinct partner_id) filter (where partner_id in (1,2)) < 2;

只需使用一个对您的数据更有效的方法。