如何在SQL Server中添加自定义计算列

时间:2016-04-13 06:46:07

标签: sql-server calculated-columns

我有一个SQL Server查询:

SELECT MD.Author_ID, MD.CoAuthor_ID, ACP.Probability [AC_Probability], 
       MD.Venue_ID, AVP.Probability [AV_Probability], A2P.pid [Paper_ID],
       P.abstract_research_area_category_id [Paper_Category_ID], 
       MD.Paper_Count, MD.Year
FROM   Merged_Data MD
JOIN   sub_aminer_author2paper A2P    ON MD.Author_ID = A2P.aid AND
                                      MD.Year      = A2P.p_year AND
                                      MD.Venue_ID  = A2P.p_venue_vid
JOIN   sub_aminer_paper P             ON MD.Venue_ID  = P.p_venue_vid AND
                                      MD.Year      = P.p_year
JOIN   AuthorCoAuthorProbability ACP  ON MD.Author_ID = ACP.Author_ID AND
                                      MD.CoAuthor_ID = ACP.CoAuthor_ID AND
                                      MD.Year      = ACP.Year
JOIN   AuthorVenueProbability    AVP  ON MD.Venue_ID = AVP.Venue_ID AND
                                      MD.Author_ID = AVP.Author_ID AND
                                      MD.Year      = AVP.Year
WHERE  MD.Author_ID = 677 AND 
       P.abstract_research_area_category_id IS NOT NULL AND 
       A2P.pid = P.pid

给我结果如下:

enter image description here

我想在P_Probability列之前的末尾添加另一列Year,例如:

对于Year = 2005Author_ID = 6778列中有1112Paper_Category_ID,而3中有Paper_Count 1}}列,每个Year每个Author_ID最多有3行(如果存在)。

因此P_Probability列中的值应为:

8677 / 2005 = Paper_Count = 1/3的{​​3}行的{3}行中存在0.333333333333333的次数
11677 / 2005 = Paper_Count = 1/3的{​​3}行的0.333333333333333次存在的次数
12677 / 2005 = Paper_Count = 1/3

的{3}行的{3}行中存在0.333333333333333次的次数

换句话说,P_Probability计算概念是:

DISTINCT Paper_Category_IDAuthor_ID的{​​{1}}除以Year中的Paper_Count除以Year

的次数 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${host}"/> <property name="port" value="${port}"/> <property name="username" value="${mail.username}"/> <property name="password" value="${mail.password}"/> <property name="javaMailProperties"> <props> <!-- Use SMTP transport protocol --> <prop key="mail.transport.protocol" >${mail.transport.protocol}</prop> <!-- Use SMTP-AUTH to authenticate to SMTP server --> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <!-- Use TLS to encrypt communication with SMTP server --> <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop> <prop key="mail.debug">false</prop> <prop key="mail.smtp.ssl.enable">true</prop> </props> </property> </bean>

0 个答案:

没有答案