简体   繁体   English

SQL枢轴或自我联接为此?

[英]SQL pivot or self-join for this?

I've a table, Events, which has 2 columns matter) - entityId : string - eventId : int 我有一个表,事件,有2列很重要)-实体ID:字符串-事件ID:整数

a presence of a row ("foo", 42) means that an event with id 42 happened on to the entity "foo". 行(“ foo”,42)的存在意味着ID为42的事件发生在实体“ foo”上。

what I want to do is figure out, from the table, which event id were NOT registered for which entities. 我想做的是从表中找出未为哪个实体注册的事件ID。 Eg the data: 例如数据:

"foo", 1 “ foo”,1

"foo", 2 “ foo”,2

"foo", 3 “ foo”,3

"bar", 1 “ bar”,1

"bar", 2 “ bar”,2

"baz", 3 “巴兹”,3

should produce an answer of this kind: 应该产生这样的答案:

.... | .... | 1 | 1 | 2 | 2 | 3 3

foo| foo | y | y | y |y y | y

bar| 酒吧| y | y | y |n y | n

baz| baz | n | n | n |y n | y

I've looked around and saw references to pivots and joins floating around. 我环顾四周,看到了对枢轴和联接的引用。 Any suggestions on the best course of actions? 关于最佳行动方案有什么建议吗? Thanks! 谢谢!

ok I think this is what I need: 好的,我认为这是我需要的:

 SELECT
        entityId,
        COUNTIF(eventId == 1) AS event1_count,
        COUNTIF(event_id == 2) AS event2_count,
        COUNTIF(event_id == 3) AS event3_count,
        COUNTIF(event_id == 4) AS event4_count
    FROM data
    GROUP BY entityId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM