| |

VerySource

 Forgot password?
 Register
Search
View: 966|Reply: 3

Ask a SQL statement?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-16 11:00:01
| Show all posts |Read mode
Table A:
id ratey
001 Y
001 N
002 Y
002 Y
003 N
003 N
Table B:
id ratey
001 N
002 Y
003 N
How to use a SQL statement to get table B (grouped by id, as long as a certain ratey is ‘N’, the corresponding ratey=’N’ for this id)? Urgent!
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-10-16 11:15:01
| Show all posts
select top 1 * from tablename group by id order by ratey
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-10-16 11:45:01
| Show all posts
declare @a table(id char(10),ratey char(10))
insert into @a select '001','Y' UNION ALL
SELECT '001','N' UNION ALL
SELECT '002','Y' UNION ALL
SELECT '002','Y' UNION ALL
SELECT '003','N' UNION ALL
SELECT '003','N'
select id,min(ratey) as ratey from @a group by id
result:
id ratey
---------- ----------
001 N
002 Y
003 N

(The number of rows affected is 3 rows)
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-10-16 12:00:02
| Show all posts
insert into @t
select '001','Y'
union all
select '001','N'
union all
select '002','Y'
union all
select '002','Y'
union all
select '003','N'
union all
select '003','N'
select * from @t

select distinct id,ratey=case when (select count(1) from @t where id=t.id and ratey='N')>0 then'N' else ratey end
from @t t
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list