【问题标题】:XML won't output Divide by zero encountered in SQL QueryXML 不会输出 SQL 查询中遇到的除以零
【发布时间】:2014-03-13 04:12:36
【问题描述】:

我有一个选择选举数据的查询。当然,当您开始选举时,所有候选人都有 0 票输入。但逐渐地,投票进入。为了从结果中获得输出百分比,我将选票数乘以 100,然后除以总票数,得到我的百分比。

问题- 一旦我的查询针对 xml 路径运行,xml 文档的第一行中就会出现一个错误,“遇到除零错误”。为了绕过这个,我只是注释掉了分界线,但我不想这样做。我想,如果我等到有一场比赛在每个领域都有投票,然后我会取消注释那行代码并让它输出只有那场比赛的百分比......直到所有比赛都至少有一些投票在他们里面。

这是我的sql代码:

:XML ON
select 
    max(case when seqnum = 1 then title1 end) as title1,
    max(case when seqnum = 1 then [precinct percent] end) as PrecinctPercent,
    max(case when seqnum = 1 then [candidate num] end) as WinnerNum,
    max(case when seqnum = 1 then Votes end) as WinningVotes,
    max(case when seqnum = 1 then party end) as WinningParty,
    max(case when seqnum = 1 then leader end) as Winner,
    max(case when seqnum = 1 then CAST(winner AS tinyint) end) as WinnerSelected,
    max(case when seqnum = 1 then ((Votes * 100) / ([total vote])) end) as WinnerPercent,
    max(case when seqnum = 2 then [candidate num] end) as LoserNum,
    max(case when seqnum = 2 then Votes end) as LosingVotes,
    max(case when seqnum = 2 then party end) as LosingParty,
    max(case when seqnum = 2 then leader2 end) as Loser,
    max(case when seqnum = 2 then ((Votes * 100) / ([total vote])) end) as LoserPercent,
    max(case when seqnum = 2 then CAST(winner AS tinyint) end) as LoserSelected,
    max(case when seqnum = 3 then title1 end) as title1,
    max(case when seqnum = 3 then [precinct percent] end) as PrecinctPercent,
    max(case when seqnum = 3 then [candidate num] end) as WinnerNum,
    max(case when seqnum = 3 then Votes end) as WinningVotes,
    max(case when seqnum = 3 then ((Votes * 100) / ([total vote])) end) as WinnerPercent,
    max(case when seqnum = 3 then party end) as WinningParty,
    max(case when seqnum = 3 then [first name]+ ' ' + [last name] end) as Winner,
    max(case when seqnum = 3 then CAST(winner AS tinyint) end) as WinnerSelected,
    max(case when seqnum = 4 then [precinct percent] end) as PrecinctPercent,
    max(case when seqnum = 4 then [candidate num] end) as LoserNum,
    max(case when seqnum = 4 then Votes end) as LosingVotes,
    max(case when seqnum = 4 then ((Votes * 100) / ([total vote])) end) as LoserPercent,
    max(case when seqnum = 4 then party end) as LosingParty,
    max(case when seqnum = 4 then [first name]+ ' ' + [last name] end) as Loser,
    max(case when seqnum = 4 then CAST(winner AS tinyint) end) as LoserSelected




from 
(
select  
        r.title1,
        r.[precinct percent],
        r.[total vote],
        rc.[race number],
        rc.[candidate num],
        rc.[Votes],
        rc.[winner],
        c.[party],
        r.[leader],
        r.[leader percent],
        r.[leader2],
        r.[leader2 percent],
        c.[first name],
        c.[last name],


            row_number() over (partition by rc.[race number] order by votes desc) as seqnum
    from    dbo.[RACE CANDIDATES] rc
    inner join dbo.[CANDIDATE] c    on  rc.[candidate num]  = c.[candidate number]
    inner join dbo.[RACE] r
     on rc.[race number] = r.[race number] 

) rc
group by rc.[race number]
FOR XML PATH ('ELECTION'), ROOT('root')

有没有一种方法可以让选择语句“忽略”任何包含 0 的字段。或者选择忽略那些确实包含 0 票的字段。

如果有帮助,将从表“[RACE CANDIDATES]”中选择总票数。希望一切都有意义。

任何意见将不胜感激。

谢谢大家!

【问题讨论】:

    标签: sql-server xml


    【解决方案1】:

    您可以在子选择中放入 where

    WHERE r.[total vote] != 0
    

    或者你可以使用案例陈述

    CASE WHEN r.[total vote] =  0 THEN 0 ELSE Votes / r.[total vote] END
    

    【讨论】:

    • 如果你不想每次都写出 case 语句,你可以将它传递给一个用户定义的函数来检查 0。
    • @astander:我应该把这些信息放在我的声明中的什么地方?
    • 您需要检查零的任何地方。所以让我们举第一个例子。 case when seqnum = 1 and [total vote] != 0 then ((Votes * 100) / ([total vote])) end
    • @astander:我可以让它检查零票而不是总票数吗?我可能需要更改我的百分比公式,对吧?
    • 我看错了你的答案。我明天去办公室试试。它不起作用,但妻子回家后想用笔记本电脑制作 DVD。感谢您的输入!!!
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多