如何在sql server中使用存储过程

如何在sql server中使用存储过程,sql,sql-server,stored-procedures,sql-server-2014,Sql,Sql Server,Stored Procedures,Sql Server 2014,我有以下五个表格: 我需要实现一个存储过程,以便execute sp_getStandingUpToDate aDate计算并显示standings表,如下图所示: 直到现在 我需要使用任何无效的日期进行调用,导致错误消息“无效的日期!”所有已完成的事务都将回滚并停止执行进一步的操作 我如何创建这个 select distinct t.name as TeamName,count(*) GP from team t,match m,match m2 where t.teamID=m.homeTe

我有以下五个表格:

我需要实现一个存储过程,以便execute sp_getStandingUpToDate aDate计算并显示standings表,如下图所示:

直到现在

我需要使用任何无效的日期进行调用,导致错误消息“无效的日期!”所有已完成的事务都将回滚并停止执行进一步的操作

我如何创建这个

select distinct t.name as TeamName,count(*) GP
from team t,match m,match m2
where t.teamID=m.homeTeamID and t.teamID=m2.visitingTeamID 
group by m.matchID,t.name
这个代码给了我一个队的比赛号码,
虽然我得到的是17分而不是34分。

我可能不应该鼓励你发表这种问题。有假定的缺乏尝试和所有已经提到的。然后你没有完全描述这个问题:不是每个人都知道什么是GF,GA,GD以及如何计算点

我相信这是一个有效的答案。如果你照原样交,你的老师可能会知道你没有写。但我怀疑,如果你设法把它拆开并重写,那么你会在这个过程中学到一些东西。因此,冒着被否决的风险:

with matchSummary as (
    select
        t.teamID,
        min(t.name) as teamName,
        sum(case when pt.teamID = t.teamID then 1 else 0 end) as GF,
        sum(case when pt.teamID = t.teamID then 0 else 1 end) as GA
    from
        team t
        inner join match m on t.teamID in (m.homeTeamID, m.visitingTeamID)
        inner join goals g on g.matchID = m.matchID
        inner join player_team pt on pt.playerID = g.playerID
    where m.dateOfMatch < @aDate
    group by m.matchID, t.teamID
)
select
    /* GD is first tie-breaker. Are there others? */
    row_number() over (
        order by
            sum(case when GF > GA then 3 when GF = GA then 1 end) desc,
            sum(GF) - sum(GA) desc
    ) as Pos,
    min(teamName) as "Team Name",
    count(*) as GP,
    count(case when GF > GA then 1 end) as W,
    count(case when GF = GA then 1 end) as T,
    count(case when GF < GA then 1 end) as L,
    sum(GF) as GF,
    sum(GA) as GA,
    sum(GF) - sum(GA) as GD,
    sum(case when GF > GA then 3 when GF = GA then 1 end) as Pts
from matchSummary
group by teamID;

因此,它不是一个代码编写服务。向我们展示你的代码尝试,也许你会得到一些建议!首先,您应该避免使用sp_uu前缀。第二,这是家庭作业的尖叫声,表明你没有努力。我们不喜欢做家庭作业,我们真的很喜欢看到人们发布他们尝试过的东西。最后但并非最不重要的一点是,即使我们确实想提供帮助,我们也需要一些细节,而不仅仅是图片。我已经知道了。我想得到一些关于开始的建议,看起来你只需要在目标表中加入一个简单的分组,通过比赛进行聚合,这样你就可以按日期进行筛选。您将使用case来确定赢/平/亏等,然后将该查询与外部查询打包,以按团队汇总所有内容。看起来这里不需要两张桌子。事实上,我想你确实需要这两张桌子来决定哪一方进球。这可能是一个愚蠢的设计,其全部目的是让问题变得更难。千万不要建议使用SQL反模式,比如隐式连接。这些代码在20多年前就被替换了,现在是时候开始使用最适合本世纪的代码了!