Count(*)自动舍入

时间:2018-04-07 04:31:08

标签: sql-server

我有一个查询,我正在尝试确定在某些日子发生的事件的百分比,我什么都没有,只有零回复。我认为(但不确定)某些事情导致我的查询回合。这在我的SQL Server中发生,但不是MySQL。

/* create the event table */
create table event (id int
                  , dayOf datetime
                  , description varchar(32)
);

/* add some events */
insert into event( id, dayOf, description ) values
( 1, '2018-01-01', 'Thing 1'),
( 2, '2018-01-01', 'Thing 2'),
( 3, '2018-01-02', 'Thing 3'),
( 4, '2018-01-02', 'Thing 4'),
( 5, '2018-01-03', 'Thing 5');

/* try to get % of events by day, but actually get zeroes */
select event_daily.dayOf, event_daily.cnt, event_total.cnt, 
       event_daily.cnt / event_total.cnt as pct_daily /* this is the zero */
from ( select dayOf, count(*) as cnt from event group by dayOf ) event_daily
   , ( select count(*) as cnt from event ) event_total;

预期结果:

DateOf    cnt  cnt  pct_daily
1/1/2018   2    5    0.40
1/2/2018   2    5    0.40
1/3/2018   1    5    0.20

实际结果:

DateOf    cnt  cnt  pct_daily
1/1/2018   2    5     0
1/2/2018   2    5     0
1/3/2018   1    5     0

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

这是因为SQL Server执行整数除法,您可以先使用select event_daily.dayOf, event_daily.cnt, event_total.cnt, CAST(event_daily.cnt AS float) / CAST(event_total.cnt AS float) as pct_daily from ( select dayOf, count(*) as cnt from event group by dayOf ) event_daily , ( select count(*) as cnt from event ) event_total;

将其转换为float
process(Input_Clk,Reset_Control)
begin
    if (Reset_Control = '1') then
        Output_Data     <= (others => '0');
    elsif  rising_edge(Input_Clk) then
        Output_Data     <= Input_Data1;
    elsif falling_edge(Input_Clk) then
        Output_Data     <= Input_Data2;
    end if;
end process ;

答案 1 :(得分:0)

尝试以下方法

public enum VisibilitySpecifier
{
    A,
    B,
}

public class AndVisibleToAttribute : Attribute
{
    public VisibilitySpecifier[] Visibility { get; set; }

    public AndVisibleToAttribute(params VisibilitySpecifier[] visibility)
    {
        Visibility = visibility;
    }
}

public class OrVisibleToAttribute : Attribute
{
    public VisibilitySpecifier[] Visibility { get; set; }

    public OrVisibleToAttribute(params VisibilitySpecifier[] visibility)
    {
        Visibility = visibility;
    }
}