Sql server 在这种情况下使用什么查询-sqlServer
我有两张桌子: 信息: 分数: 我可以使用什么查询来获得此结果? 月和年列显示了2019年6月至2019年6月的男性得分Sql server 在这种情况下使用什么查询-sqlServer,sql-server,Sql Server,我有两张桌子: 信息: 分数: 我可以使用什么查询来获得此结果? 月和年列显示了2019年6月至2019年6月的男性得分 你可以这样做 SELECT Info.id , Info.Name , Info.age , DATEDIFF(MONTH, CAST(score.date AS datetime), GETDATE()) AS Moths , DATEDIFF(YEAR, CAST(score.date AS datetime), GETDATE(
你可以这样做
SELECT
Info.id
, Info.Name
, Info.age
, DATEDIFF(MONTH, CAST(score.date AS datetime), GETDATE()) AS Moths
, DATEDIFF(YEAR, CAST(score.date AS datetime), GETDATE()) AS Years
FROM
Info
INNER JOIN Score
ON Info.id = Score.ID
请尝试以下操作:
select info.id, info.name,info.age, sum(case when MONTH(convert(datetime,score.date,5)) = 6 and Year(convert(datetime,score.date,5)) = 2019 then score else 0 end) as monthS,sum(case when MONTH(convert(datetime,score.date,5)) = 6 and Year(convert(datetime,score.date,5)) = 2019 then score else 0 end) as yearS
from info
inner join score
on info.id = score.id
group by info.id, info.name,info.age
或:
示例工作。您已经尝试了什么?如果您还没有尝试任何东西,为什么不呢?你做了什么研究?如果您不知道从哪里开始,我建议您查找连接语法;这是使用任何RDBMS的基本部分之一。我想我必须按id分组,然后计算2019年的值,但我不知道怎么做,我是新手
select info.id, info.name,info.age, sum(isnull(score,0)) as monthS,sum(isnull(score,0)) as yearS
from info
left join
(
select * from score
where MONTH(convert(datetime,score.date,5)) = 6 and Year(convert(datetime,score.date,5)) = 2019
) as s
on info.id = s.id
group by info.id, info.name,info.age