我需要在sql中编写一个查询

时间:2016-05-24 17:32:12

标签: sql sql-server

  

撰写查询以提供“获得的高级和获得的曝光”总数   按公司和S160覆盖代码。仅包括2010年的数据   (2010年1月1日 - 2010年12月31日),公司等于GE或GG,并且评级为州   等于CA.

以下是表格和字段:

Table 1a
Earned Premium = ERND_PRM_AM
Earned Exposures = ERND_EXPSR_DY_CN
S160 Coverage Code = S160_CVRG_CD
Policy Key = PLCY_KY

Table 2a
Rated State = RTD_ST_CD
Company = CMPNY_CD
Date = SNP_DT
Policy Key = PLCY_KY

这是我的答案 - 这是正确的吗?

Select 
    Table 1a.Earned Premium, Table 1a.Earned Exposures, Table 1a.S160 Coverage Code, 
    Table 2a.Company, Table 2a.Date, Table 2a.Rated State
From Table 1a
Inner Join Table 2a
On Table 1a.Policy Key=Table 2a.Policy Key
Where Date between 1/1/2010 and 12/31/2010, Company='GE' or 'GG', Rated State='CA';

2 个答案:

答案 0 :(得分:0)

如果您正在谈论SQL服务器并且您的表名实际上是1a和2a,那么:

Select 
   1a.ERND_PRM_AM as [Earned Premium], 1a.ERND_EXPSR_DY_CN as [Earned Exposures], 1a.S160_CVRG_CD as [S160 Coverage Code], 
   2a.CMPNY_CD as Company, 2a.SNP_DT as [Date], 2a.RTD_ST_CD as [Rated State]
From 1a
Inner Join 2a
On 1a.PLCY_KY = 2a.PLCY_KY
Where SNP_DT between '1/1/2010' and '12/31/2010' and CMPNY_CD in ('GE', 'GG') and RTD_ST_CD = 'CA';

答案 1 :(得分:0)

忽略格式错误的问题,这非常简单:

SELECT SUM(t1.ERND_PRM_AM) AS 'Total Earned Premium'
,SUM(t1.ERND_EXPSR_DY_CN) AS 'Total Earned Exposures'
,t1.S160_CVRG_CD AS 'S160 Coverage Code'
,t2.CMPNY_CD AS 'Company'
FROM Table_1a t1
INNER JOIN Table_2a t2
  ON t1.PLCY_KY = t2.PLCY_KY
WHERE SNP_DT BETWEEN '20100101' AND '20101231'
  AND CMPNY_CD IN('GE','GG')
  AND RTD_ST_CD='CA'
GROUP BY t2.CMPNY_CD, t1.S160_CVRG_CD

这听起来像家庭作业有关。仅供参考,SO上不允许这类问题。您也没有提供您期望/样本数据的示例。