简体   繁体   English

SQLAlchemy-选择具有相同列值的行,但不包括来自不同列的值的行

[英]SQLAlchemy - Select rows with the same column values, excluding those with values from different column

I am inexperienced in SQL / SQLAlchemy, and am trying to write a SQLAlchemy query from a table called Event with data such as this: 我对SQL / SQLAlchemy缺乏经验,并试图从名为Event的表中编写一个SQLAlchemy查询,其中包含以下数据:

I would like to select the rows with Type equal to "Viewed Tab", but only if there does not exists a row with the same Session value that has a Type equal to "Mobile View". 我想选择Type等于“ Viewed Tab”的行,但前提是不存在具有相同Session值且Type等于“ Mobile View”的行。 So in the sample data above I would want the query to return the rows with EventId equal to 150 and 154, but not 147. 因此,在上面的示例数据中,我希望查询返回EventId等于150和154而不是147的行。

Thank you. 谢谢。

Assuming the table is defined as per below: 假设表定义如下:

class Event(Base):
    __tablename__ = 'events'
    EventId = Column(Integer, primary_key=True)
    Session = Column(Integer)
    Type = Column(String)

the query to produce the desired result can be written as: 产生期望结果的查询可以写成:

viewed = aliased(Event, name='viewed')
mobile = aliased(Event, name='mobile')
qry = (session.query(viewed)
       .filter(viewed.Type == 'Viewed Tab')
       .outerjoin(mobile, and_(
           viewed.Session == mobile.Session,
           mobile.Type == 'Mobile View')
       )
       .filter(mobile.Session == None)
       )

This will produce a query without any aggregations: 这将产生没有任何聚合的查询:

SELECT  viewed."EventId" AS "viewed_EventId",
        viewed."Session" AS "viewed_Session",
        viewed."Type" AS "viewed_Type"

FROM    events AS viewed

LEFT OUTER JOIN
        events AS mobile
    ON  viewed."Session" = mobile."Session"
    AND mobile."Type" = 'Mobile View'

WHERE   viewed."Type" = 'Viewed Tab'
    AND mobile."Session" IS NULL

Try: 尝试:

select e.*
  from event e
  join (select eventid, session
          from event
         group by eventid, session
        having sum(case when type = 'Viewed Tab' then 1 else 0 end) > 0
           and sum(case when type = 'Mobile View' then 1 else 0 end) = 0) v
    on e.eventid = v.eventid

I'm not sure what syntax limitations there are with sql alchemy, however. 我不确定sql alchemy有什么语法限制。

暂无
暂无

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

相关问题 熊猫:基于列值合并2个数据框; 对于包含相同列值的多个行,请将其附加到不同的列 - Pandas: Merge 2 dataframes based on a column values; for mulitple rows containing same column value, append those to different columns 从 Pandas DataFrame 中选择一列中具有相同值但另一列中具有不同值的行 - Select rows from a Pandas DataFrame with same values in one column but different value in the other column 如何 select 数据框中的行在列值的基础上是相似的 - how to select rows in a data frame those are similar on basis of column values select 行来自 pandas dataframe 在另一列不同的列中具有相同值并找到平均值并使其成为字典 - select rows from pandas dataframe with same values in one column different on the other &find the average&make it a dictionary Select 行,如果多个列值相同(或为空) - Select rows if multiple column values are same (or null) 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column SQLAlchemy sqlite3从具有不同JSON值的多行的JSON列中删除值 - SQLAlchemy sqlite3 remove value from JSON column on multiple rows with different JSON values 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 根据不同列中的多行选择列中的值 - Select values in a column based on multiple rows in a different column 基于列值的 DataFrame 中的 select 行? - select rows from a DataFrame based on column values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM