| |

VerySource

 Forgot password?
 Register
Search
View: 1271|Reply: 11

I'm anxious for a question !!

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-7 10:00:01
| Show all posts |Read mode
There is a table A
ID GX NUM NUM1
1 A 100 50
2 A 200 30
3 A 300 40
3 B 250 20
4 B 100 30
5 B 200 20
5 A 150 25
6 B 200 30
Now we need to sum the values ​​of NUM and NUM1 by ID. If the ID is the same, GX has two records of A and B.
The value of NUM is taken as the value of A, and the value of NUM1 is taken as the sum of A and B. They are grouped by ID.
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-24 17:30:01
| Show all posts
select ID, sum (case GX when 'A' then NUM else 0 end) as NUM, sum (NUM1) as NUM1 from A group by ID
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-5-24 17:45:02
| Show all posts
select ID, GX, sum (NUM) num, sum (NUM1) num1
from tablename
group by ID, GX
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-24 21:45:01
| Show all posts
select ID, sum (case GX when 'A' then NUM else 0 end) as NUM, sum (NUM1) as NUM1 from A where exists (select 1 from A t where t.ID = ID and t.GX = 'A' ) group by ID
union
select ID, sum (NUM) as NUM, sum (NUM1) as NUM1 from A where not exists (select 1 from A t where t.ID = ID and t.GX = 'A') group by ID
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-5-24 23:15:01
| Show all posts
select id, sum (case when gx = 'A' then num else 0 end), sum (num + num1)
from (
    select id, gx, sum (num), sum (num1)
    from t
    group by id, gx) _x
group by id
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-5-25 00:00:01
| Show all posts
create table a (id int, GX char (1), NUM int, NUM1 int)
insert a
select 1, 'A', 100, 50
union all
select 2, 'A', 200, 30
union all
select 3, 'A', 300, 40
union all
select 3, 'B', 250, 20
union all
select 4, 'B', 100, 30
union all
select 5, 'B', 200, 20
union all
select 5, 'A', 150, 25
union all
select 6, 'B', 200, 30
union all
select 6, 'B', 200, 30
union all
select 6, 'B', 200, 30



select ID, GX, sum (NUM) num, sum (NUM1) num1
from a
group by ID, GX
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-5-25 14:00:02
| Show all posts
CREATE TABLE A
(ID INT,
GX CHAR (1),
NUM INT,
NUM1 INT
)
GO

INSERT INTO A (ID, GX, NUM, NUM1)
    SELECT 1, 'A', 100,50
UNION ALL SELECT 2, 'A', 200,30
UNION ALL SELECT 3, 'A', 300,40
UNION ALL SELECT 3, 'B', 250,20
UNION ALL SELECT 4, 'B', 100,30
UNION ALL SELECT 5, 'B', 200,20
UNION ALL SELECT 5, 'A', 150,25
UNION ALL SELECT 6, 'B', 200,30
GO

SELECT ID, MAX (ISNULL (NNUM, NUM)), SUM (NUM1) FROM
(SELECT M.ID, M.GX, M.NUM, M.NUM1, N.GX AS NGX, N.NUM AS NNUM FROM AM LEFT JOIN AN ON M.ID = N.ID AND N.GX = 'A' ) T
GROUP BY ID
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-25 14:45:01
| Show all posts
It seems that the records with only A or B have not been calculated yet.
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-5-25 19:30:02
| Show all posts
SELECT ID, ISNULL (NNUM, NUM), SUM (NUM1) FROM
(SELECT M.ID, M.GX, M.NUM, M.NUM1, N.GX AS NGX, N.NUM AS NNUM FROM AM LEFT JOIN AN ON M.ID = N.ID AND N.GX = 'A' ) T
GROUP BY ID, ISNULL (NNUM, NUM)
ORDER BY ID

This can also
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-5-25 20:45:01
| Show all posts
You try my two
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