我的目标是,在2013年查看所有房产查看者和计划的观看次数。 我相信我有90%的解决方案,但目前,它不起作用。
正在使用的表格
* yr_viewer *
Clientnum, CHAR(5), NOT nullable (PRIMARY KEY 1)
Branchnum, CHAR(3), NOT nullable (PRIMARY KEY 2)
Prefferedtype, VARCHAR2(15), nullable
MAXIMUMRENT, NUMBER (17,2), nullable
Finished, NUMBER(1,0), nullable
yr_viewing
propertynum, CHAR(5), NOT nullable (PRIMARY KEY 1)
dateviewed, Date nullable (format - 1-jan-2013)
Clientnum, CHAR(5), NOT nullable (PRIMARY KEY 2)
Staffnum, CHAR(5), nullable
Comments, VARCHAR2 (300), nullable
yr_Client
Clientnum, CHAR(5), NOT nullable (PRIMARY KEY 1)
Firstname Varchar2(20), nullable
Lastname Varchar2(20), nullable
Address Varchar2(50), nullable
Telephonenum Char (13), nullable
我的查询
select distinct c.Firstname, c.Lastname, v.PropertyNum, v.DateViewed
from yr_viewing, yr_viewer i
inner join YR_VIEWING v
on i.ClientNum = v.ClientNum
inner join YR_CLIENT c
on i.ClientNum = c.ClientNum
where dateviewed between '01-jan-2013' and '31-dec-2013'
答案 0 :(得分:1)
答案:
select distinct c.Firstname, c.Lastname, v.PropertyNum, v.DateViewed
from yr_viewer VV
inner join YR_VIEWING v
on VV.ClientNum = v.ClientNum
inner join YR_CLIENT c
on V.ClientNum = c.ClientNum
where TO_CHAR(dateviewed, 'yyyy') = '2013'
编辑:双引号(Alex建议)
答案 1 :(得分:1)
你的联盟有些麻烦。您与yr_viewing
交叉加入yr_viewer
,然后再次加入yr_viewing
。
您不会从yr_viewer
中提取任何列,因此请将其完全排除在查询之外。而且您无需两次包含yr_viewing
。尝试这样的事情:
select distinct c.Firstname, c.Lastname, v.PropertyNum, v.DateViewed
from yr_viewing v
inner join YR_CLIENT c on i.ClientNum = c.ClientNum
where dateviewed between '01-jan-2013' and '31-dec-2013'
还有一件事:只有当Oracle NLS_DATE_FORMAT
设置为DD-MON-YYYY(通常不是)时,您的日期才有效。即使它是你不应该相信它。最好使用ANSI日期文字并更改WHERE
子句,如下所示:
where dateviewed between DATE '2013-01-01' and DATE '2013-12-31'
答案 2 :(得分:0)
您确定应该使用内连接而不是左连接吗?使用内部联接仅显示显示客户和查看人员的结果。
你的陈述似乎有点时髦......你正在进行隐式连接,然后通过同一个表的显式内连接跟随它......
也许这样的事情会更好:
select distinct c.Firstname, c.Lastname, v.PropertyNum, v.DateViewed
from yr_viewing v
LEFT JOIN yr_viewer i
on i.ClientNum = v.ClientNum
LEFT JOIN YR_CLIENT c
on i.ClientNum = c.ClientNum
where dateviewed
between '01-jan-2013' and '31-dec-2013'
;