【发布时间】:2021-05-29 01:32:34
【问题描述】:
我使用的是 SQL Server 2014 标准版。
我有以下疑问...
SELECT ach.amt, ades.dsline, ades.des
FROM ##ACHTrans ach
LEFT OUTER JOIN apvodes ades on 1=1 and ades.vo_id = ach.vo_id
WHERE ades.voline = '100'
ORDER by ach.apnum, ach.cknum, ach.vo_id, ach.amt desc
这给了我结果...
+------------+---------------+------------------------------+
| ach.amt | ades.dsline | ades.des |
+------------+---------------+------------------------------+
| 1232.50 | 1 | This is the description for |
| 1232.50 | 2 | The $1,232.50 ACH Amount |
| 245.18 | 1 | This one is for the $245.18 |
| 245.18 | 2 | transactions details |
| 245.18 | 3 | that has four lines of info |
| 245.18 | 4 | in the description. |
| 79.25 | 1 | This $79.25 item has 1 line. |
| 15.00 | 1 | So does this $15.00 one. |
+------------+---------------+------------------------------+
我需要一种方法来通过 ach.amt 行获取此信息,并连接 ades.des 信息以获得类似于以下内容的结果:
+------------+--------------------------------------------------------------------------------------------------+
| Amount | Description |
+------------+--------------------------------------------------------------------------------------------------+
| 1232.50 | This is the description for The $1,232.50 ACH Amount |
| 245.18 | This one is for the $245.18 transactions details that has four lines of info in the description. |
| 79.25 | This $79.25 item has 1 line. |
| 15.00 | So does this $15.00 one. |
+------------+--------------------------------------------------------------------------------------------------+
【问题讨论】:
-
on 1=1?1不何时等于1? -
amt可能是唯一的吗?如果不是,会准确地定义您的不同组吗? -
@Larnu 当您在客户端代码中构建查询并且 where 子句由用户选择确定时,这很常见。以
WHERE 1=1开头允许每个用户选择相同的AND {cirteria}表达式。 -
我会在
WHERE中理解这一点(尽管我不是他们的粉丝),但在ON子句中,@JoelCoehoorn ?用户何时定义JOIN条件? -
@Larnu 我在一些报告工具中看到过。
标签: sql sql-server tsql sql-server-2014 string-concatenation