从不同的表中计算2个相同的属性

时间:2016-07-26 16:40:13

标签: sql oracle

我收到了模糊定义的列。输入此SQL语句后。 我想要做的是计算BusinessEventInvite和EventInvite之间参加每个事件的人数。

>>> [str(i) for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
>>> ' + '.join(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'])
'1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10'

BEI表

ALUMNIID EVENTID RSVP出席

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended
FROM EVENT E
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID
WHERE EI.ATTENDED = Attended and BEI.ATTENDED = Attended
GROUP BY E.EVENTID
ORDER BY E.EVENTID ASC;

选择了6行

EI表 ALUMNIID EVENTID RSVP出席

 1          6 YES        Attended  
 2          6 YES        Attended  
 3          6 YES        Attended  
 4          6 YES        Attended  
 5          6 YES        Attended  
 6          6 NO                   

选择了10行

活动表

EVENTID EVENTNAME

 7          6 YES        Attended  
 8          6 YES        Attended  
 9          6 No                   
10          7 YES        Attended  
11          8 YES        Attended  
12          7 YES        Attended  
13          7 YES        Attended  
14          8 YES        Attended  
15          7 YES        Attended  
16          8 YES        Attended  

5 个答案:

答案 0 :(得分:1)

如果这些列存在于这些表中,则需要显示表的模式。但一般来说,当SQL Server在您在查询中使用的2个或更多表中找到相同的列名时,您会收到该错误,因此要求您通过使用别名限定它们来解决该歧义。

我的猜测是你需要改变这一行

WHERE EI.ATTENDED = BEI.Attended 

    @charset "utf-8";
   

    #menu2,
    #menu2 ul,
    #menu2 li,
    #menu2 a {
    	margin: 0;
    	padding: 0;
    	border: none;
    	outline: none;
    }

    /* menu2 */
    #menu2 {	
    		width:850px;
    		height:33px;
    		margin:0px;
    		padding:0px;
    		font-size:13px;
    		vertical-align:top;
    		float:left;
    		border-top-right-radius:10px;
    	
    	/*background: -webkit-linear-gradient(top, #7b0100 0%,#7b0100 100%);
    	background: -moz-linear-gradient(top, #7b0100 0%,#7b0100 100%);
    	background: -o-linear-gradient(top, #7b0100 0%,#7b0100 100%);
    	background: -ms-linear-gradient(top, #7b0100 0%,#7b0100 100%);
    	background: linear-gradient(top, #7b0100 0%,#7b0100 100%);
    	*/
    	-webkit-border-radius: 5px;
    	-moz-border-radius: 5px;
    	border-radius: 5px;
    }
    #menu2 li:hover{
    	background:#000066;
    	color:#FF0;
    	
    }

    #li_menu{
    		width:80px;
    		text-align:center;
    		padding:0px;
    		margin:0px;
    	}
    	
    #li_submenu{
    		text-align:left;
    	}
    	
    #menu2 li {
    	position: relative;
    	list-style: none;
    	float: left;
    	display: block;
    	height: 34px;
    }

    /* Links */

    #menu2 li a {
    	display: block;
    	padding: 0 14px;
    	margin: 6px 0;
    	line-height: 28px;
    	text-decoration: none;
    	
    	border-left: 1px solid #CCC;
    	border-right: 1px solid #9000;

    	font-family: Helvetica, Arial, sans-serif;
    	font-weight: bold;
    	font-size: 13px;

    	color: #FFFFFF;
    	text-shadow: 1px 1px 1px rgba(0,0,0,.6);

    	-webkit-transition: color .2s ease-in-out;
    	-moz-transition: color .2s ease-in-out;
    	-o-transition: color .2s ease-in-out;
    	-ms-transition: color .2s ease-in-out;
    	transition: color .2s ease-in-out;
    }

    #menu2 li:first-child a { border-left: none; }
    #menu2 li:last-child a{ border-right: none; }

    #menu2 li:hover > a { color: #FFFF00; }

    /* Sub menu2 */

    #menu2 ul {
    	position: absolute;
    	top: 40px;
    	left: 0;

    	opacity: 0;
    	
    	background: #0000FF;

    	-webkit-border-radius: 0 0 5px 5px;
    	-moz-border-radius: 0 0 5px 5px;
    	border-radius: 0 0 5px 5px;

    	-webkit-transition: opacity .25s ease .1s;
    	-moz-transition: opacity .25s ease .1s;
    	-o-transition: opacity .25s ease .1s;
    	-ms-transition: opacity .25s ease .1s;
    	transition: opacity .25s ease .1s;
    }


    #menu2 li:hover > ul {
    	 opacity: 1; 
    }

    #menu2 ul li {
    	height: 0;
    	overflow: hidden;
    	padding: 0;

    	-webkit-transition: height .25s ease .1s;
    	-moz-transition: height .25s ease .1s;
    	-o-transition: height .25s ease .1s;
    	-ms-transition: height .25s ease .1s;
    	transition: height .25s ease .1s;
    }

    #menu2 li:hover > ul li {
    	height: 34px;
    	overflow: visible;
    	padding: 0;
    }

    #menu2 ul li a {
    	width: 120px;
    	padding: 4px 0 4px 30px;
    	margin: 0;

    	border: none;
    	border-bottom: 1px solid #4F0002;
    }



    +++++++++++++++

    #menu, #menu ul {

    	list-style:none;
    	padding:0;
    	margin:0;
    	
    }

    #menu li {
    	float:center;
    	position:relative;
    	line-height: 4.5em;
    	width: 10em;
    }
    #menu li ul {
    	position:absolute;
    	margin-top:-1em;
    	margin-left:.5em;
    	display:none;
    }
    #menu ul li ul {
    	margin-top:-3em;
    	margin-left:7em;
    }
    #menu a {
    	display:block;
    	border-right:0px solid #fff;
    	color:#FFFFFF;
    	text-decoration:none;
    	padding:0 10px;
    }
    #menu a:hover {
    	background-color:#0000FF;
    	color:#fff;
    }
    #menu ul {
    	border-top:1px solid #fff;
    }
    #menu ul a {
    	border-right:none;
    	border-right:1px solid #fff;
    	border-bottom:1px solid #fff;
    	border-left:1px solid #fff;
    	background:#0000FF;
    }
    /* SHOW SUBMENU 1 */
    #menu li:hover ul, #menu li.over ul {
    	display:block;
    	position: absolute;
    }
    #menu li:hover ul ul, #menu li.over ul ul {
    	display:none;
    }
    /* SHOW SUBMENU 2 */
    #menu ul li:hover ul, #menu ul li.over ul {
    	display:block;
    }

    +++++++++++++++++++++++++++++++++

答案 1 :(得分:0)

这可能是您WHERE声明的原因,如下所示

WHERE EI.ATTENDED = Attended and BEI.ATTENDED = Attended

可能应该

WHERE EI.ATTENDED = 'Attended' AND BEI.ATTENDED = 'Attended';

考虑更改您的查询并将条件从WHERE移至JOIN ON,如

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended
FROM EVENT E
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid 
AND BEI.ATTENDED = 'Attended'
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID 
AND EI.ATTENDED = 'Attended' 
GROUP BY E.EVENTID
ORDER BY E.EVENTID ASC;

答案 2 :(得分:0)

ALUMNIID EVENTID RSVP出席

     1          6 YES        Attended  
     2          6 YES        Attended  
     3          6 YES        Attended  
     4          6 YES        Attended  
     5          6 YES        Attended  
     6          6 NO                   

选择了6行

ALUMNIID EVENTID RSVP出席

     7          6 YES        Attended  
     8          6 YES        Attended  
     9          6 No                   
    10          7 YES        Attended  
    11          8 YES        Attended  
    12          7 YES        Attended  
    13          7 YES        Attended  
    14          8 YES        Attended  
    15          7 YES        Attended  
    16          8 YES        Attended  

选择了10行

第一个表是BEI,第二个表是EI

答案 3 :(得分:0)

试试这个,因为你还没有参加哪个表?因为现在有3张桌子。

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended
FROM EVENT E
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID
WHERE EI.ATTENDED = BEI.Attended and BEI.ATTENDED = EI.Attended
GROUP BY E.EVENTID
ORDER BY E.EVENTID ASC;

答案 4 :(得分:0)

模糊列错误是由于您没有为where语句标识别名,因为此查询中的多个表中存在属性 Attended 。你可能意味着在你的平等陈述中使用字符串'Attended'。

WHERE EI.ATTENDED = 'Attended' and BEI.ATTENDED = 'Attended'

这将返回参与这两个活动和被邀请的用户

WHERE EI.ATTENDED = 'Attended' or BEI.ATTENDED = 'Attended'

您可能希望查看结果产生的结果和'或'声明

很难确定您拥有或正在尝试使用有限的表/数据结构