在SQL中返回计数由两个其他分区分组,并在id上使用in子句

时间:2016-02-15 19:03:33

标签: sql sql-server

我有这个查询,它在会员资格表中找到与(memberid)具有相同电子邮件的所有用户,并带回任何其他具有匹配电子邮件的用户

select tenant,first,last,memberid,email,ipaddress,password
from membership
where email IN (
  SELECT email
  FROM membership where memberid = <Parameters.Member ID>
  GROUP BY email)

我还需要能够恢复与当前行​​的用户具有相同密码或ipaddress的所有用户的计数。我尝试过使用case语句但无法获得正确的结果。

3 个答案:

答案 0 :(得分:2)

在许多平台(包括sql server)中,您可以使用窗口函数。窗口函数允许您计算与当前行具有相同密码或ipaddress的用户的计数。如果这是你想要的

select tenant,first,last,memberid,email,ipaddress,password,
       count(memberid) OVER (partition by ipaddress) as ipaddress_count,
       count(memberid) OVER (partition by password) as password_count
from membership
where email IN (
  SELECT email
  FROM membership where memberid = <Parameters.Member ID>
  GROUP BY email)

答案 1 :(得分:0)

在我完成+1之前,Hogan得到了正确答案 也可以使用连接

select match.email, match.tenant, match.first, match.last
     , match.memberid, match.ipaddress, match.password  
     , count(match.memberid) OVER (partition by match.ipaddress) as ipCount 
     , count(match.memberid) OVER (partition by match.password) as passwordCount
     , member.memberid as [member.memberid], member.ipaddress as [member.ipaddress], member.password as [member.password]
  from membership as match
  join membership as member
        on member.email = match.email 
       and member.memberid = <Parameters.Member ID>

答案 2 :(得分:-1)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

         <FrameLayout 
                android:layout_width="match_parent"
                android:layout_height="50dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:text="some text" 
                    android:paddingRight="28dp"          
                   />
                <View
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:background="@drawable/bg.png"
                    android:layout_gravity="right|center_vertical"  
                    android:marginRight="8dp"                      
                   />
         </FrameLayout>
</LinearLayout>