【发布时间】:2017-09-06 07:31:21
【问题描述】:
我有两张桌子
表1:
Id | product | price
---+---------+------
1 | A | 5
1 | B | 3
1 | C | 6
表2:
Id | prod | subprod
---+------+--------
1 | A | xxxx
1 | A | yyyy
1 | A | zzzz
我的结果表应该包含 table2 中的所有 3 行以及一个名为 price 的新列(将从 table1 计算值)
结果表应该是这样的
Id|prod|subprod|price
--+----+-------+-----
1 | A | xxxx |(if subprod = xxxx in table 2 then this should have price of A from table 1)
1 | A | yyyy |(if subprod = yyyy in table 2, then if price of B is greater than price of C then the value should be price of B else 0)
1 | A | zzzz |(if subprod = zzzz in table 2, then if price of B is less than price of C then the value should be price of C-B else 0)
【问题讨论】:
-
你想要整个 if 语句在你的输出中,还是你想要 if 语句的解决方案?到目前为止,您还尝试过什么?发布您的查询
-
看起来像家庭作业
-
@RealCheeseLord 我创建了一个函数,所有这些都是硬编码的,并且每次在查询中调用它
-
@RuslanK。这是真正的工作问题……不是在家工作。
标签: sql-server sql-server-2008 sql-server-2014 sql-server-2016