avatar
n*g
1
there are records in Database such as
1 A
2 A
2 B
3 C
4 B
I want to return a recordset that combine the value of
the second column if the value of the first column is the same,
like below
1 A
2 AB
3 C
4 B
how to implement it using sql statement?
avatar
aw
2
use a self-join.
SELECT T1.lastname + isnull(T2.lastname,'')
FROM MYTABLE T1 LEFT JOIN MYTABLE T2
ON T1.firstname = T2.firstname AND T1.id < T2.id

【在 n*********g 的大作中提到】
: there are records in Database such as
: 1 A
: 2 A
: 2 B
: 3 C
: 4 B
: I want to return a recordset that combine the value of
: the second column if the value of the first column is the same,
: like below
: 1 A

avatar
y*i
3
I think this way the solution is not scalable. What if there are records like
You will get
Instead of
avatar
n*g
4
doesn't work. the sql script returns:
1 A
2 AB
2 B
3 C
4 B
I don't want the third record....
btw: both columns are char

【在 aw 的大作中提到】
: use a self-join.
: SELECT T1.lastname + isnull(T2.lastname,'')
: FROM MYTABLE T1 LEFT JOIN MYTABLE T2
: ON T1.firstname = T2.firstname AND T1.id < T2.id

avatar
n*f
5
建议您用程序去实现.
在ORACLE 您可以用PL/SQL 去LOOP 整个表.
您的问题主要是同表中行数据和行数据的关系处理.
总之,我认为写一段小程序比较能简单解决您的问题.

【在 n*********g 的大作中提到】
: doesn't work. the sql script returns:
: 1 A
: 2 AB
: 2 B
: 3 C
: 4 B
: I don't want the third record....
: btw: both columns are char

avatar
n*g
6
yeah, I agree it is easy to implement in program.
but it is not my decision, sigh.

【在 n****f 的大作中提到】
: 建议您用程序去实现.
: 在ORACLE 您可以用PL/SQL 去LOOP 整个表.
: 您的问题主要是同表中行数据和行数据的关系处理.
: 总之,我认为写一段小程序比较能简单解决您的问题.

avatar
n*f
7
OK, Please tell us more about your Table:
1. What DB you are using? Access, SQL or Oracle?
2. What is possible value for ID field? 1,2,3..... What is the MAX value?
3. What is possible value for DATA field? Just A,B and C or have more ?
4. How many record do you have?
5. Why you have to use One SQL statement to do that?
Is it possible let me CALL you?

【在 n*********g 的大作中提到】
: yeah, I agree it is easy to implement in program.
: but it is not my decision, sigh.

avatar
aw
8
还有个前面有人提到的问题,可不可能有:
2 A
2 B
2 C
。。。的情况?就说有未知个相同ID(不止2个)。

【在 n****f 的大作中提到】
: OK, Please tell us more about your Table:
: 1. What DB you are using? Access, SQL or Oracle?
: 2. What is possible value for ID field? 1,2,3..... What is the MAX value?
: 3. What is possible value for DATA field? Just A,B and C or have more ?
: 4. How many record do you have?
: 5. Why you have to use One SQL statement to do that?
: Is it possible let me CALL you?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。