这样的表应该怎么设计?数据区间和具体数据的对应

qq_liang 2007-07-19 03:38:08
有一组数据规则,H=6或H=5.125对应的H1数据是H1=3.5,H>6对应的H1数据是H1=6,3.75<=H<6对应的H1数据是2.5,我想把这样的数据关系到表里,应该怎么设计表?
...全文
181 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ivan_ren 2007-07-20
  • 打赏
  • 举报
回复
test_id test_values
-------------------- --------------------
6.000 3.500
5.125 3.500
7.000 6.000
3.260 2.500
5.500 2.500

(所影响的行数为 5 行)


把你想实现的规则在插入前触发器中来实现
不知道是否是楼主想要的结果呢?
ivan_ren 2007-07-20
  • 打赏
  • 举报
回复
CREATE TABLE T_TEST(TEST_ID NUMERIC(18,3) NULL,TEST_VALUES NUMERIC(18,3) NULL)
GO
CREATE trigger itr_t_test on t_test
instead of insert
as
begin
select * into #temp_insert from inserted
if exists(select * from #temp_insert where test_id in(6,5.125))
begin
update #temp_insert set test_values=3.5 where test_id in(6,5.125)
end
if exists(select * from #temp_insert where test_id >6)
begin
update #temp_insert set test_values=6 where test_id >6
end
if exists(select * from #temp_insert where TEST_ID > 5.125 AND TEST_ID<6 )
begin
update #temp_insert set test_values=2.5 where TEST_ID > 5.125 AND TEST_ID<6
end
if exists(select * from #temp_insert where TEST_ID >= 3.25 AND TEST_ID<5.125)
begin
update #temp_insert set test_values=2.5 where TEST_ID >= 3.25 AND TEST_ID<5.125
end
INSERT INTO t_test(TEST_ID,TEST_VALUES) SELECT TEST_ID ,TEST_VALUES FROM #temp_insert
end
GO
--测试数据

INSERT INTO T_TEST(TEST_ID) VALUES(6)

INSERT INTO T_TEST(TEST_ID) VALUES(5.125)

INSERT INTO T_TEST(TEST_ID) VALUES(7)

INSERT INTO T_TEST(TEST_ID) VALUES(3.26)

INSERT INTO T_TEST(TEST_ID) VALUES(5.5)

SELECT * FROM T_TEST

drop table t_test
drop trigger itr_t_test
qq_liang 2007-07-20
  • 打赏
  • 举报
回复
郁闷啊怎么没人来呢
qq_liang 2007-07-20
  • 打赏
  • 举报
回复
自己顶下
qq_liang 2007-07-19
  • 打赏
  • 举报
回复
没高手在吗

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧