| |

VerySource

 Forgot password?
 Register
Search
View: 912|Reply: 8

SQL query table, low-level questions?

[Copy link]

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-12 00:00:01
| Show all posts |Read mode
I'm a novice and write out sql statements, remember to give points if I can achieve them. The following are three tables, table 1: student information table, table 2: schedule, table 3: transcript, found out to be like excel:
Student ID, Name, Language, Mathematics, English
201601 Zhangsan 114 120 124
Table 1:
sno sname
201601 Zhang San
Table 2: cno cname
      1 language
      2 Mathematics

Table 3 sno cno grade
    201601 1 114
    201601 2 120
Requires detection of the following:
sno sname 1 2
201601 Zhangsan 114 120
Reply

Use magic Report

0

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-15 19:15:01
| Show all posts
Row-column conversion problem; static can be done with a case statement;
Dynamic (such as yours) may need a string together ^_^
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-15 23:30:01
| Show all posts
This is best achieved using a stored procedure, written in SQL is very readable
Reply

Use magic Report

0

Threads

22

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-6-16 09:45:01
| Show all posts
Row to column
Reply

Use magic Report

0

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-17 09:30:01
| Show all posts
if exists(select 1 from sysobjects where id = object_id(N't1'))
drop table t1
go

create table t1
(sno varchar(10),
sname varchar(10)
)

if exists(select 1 from sysobjects where id = object_id(N't2'))
drop table t2
go
create table t2
(cno varchar(10),
cname varchar(10)
)


if exists(select 1 from sysobjects where id = object_id(N't3'))
drop table t3
go
create table t3
(sno varchar(10),
cno varchar(10),
grade int
)

insert into t1 values('201601','Zhang San')
insert into t2 values('1','Chinese')
insert into t2 values('2','Math')
insert into t3 values('201601','1',100)
insert into t3 values('201601','2',80)
go

--Execute query
declare @sql varchar(2000)

set @sql ='select t1.sno,t1.sname '
select @SQl = @sql +',sum(case t3.cno when'+ t2.cno + 'then t3.grade else 0 end)' + t2.cname
from t2

set @Sql = @sql + 'from t1, t3 where t1.sno = t3.sno group by t1.sno,t1.sname'
exec( @sql)
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-6 09:00:01
| Show all posts
endmancan be achieved, but I am a novice, can you explain why you want to use select 1 from sysobjects where id = object_id(N't1'), and sum(case t3.cno when'+ t2.cno + ' then t3.grade else 0 end) Can I implement it in dephi+access
Reply

Use magic Report

0

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-6 10:15:01
| Show all posts
....
It seems that access does not work
I have implemented access, but I can't write it.
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-6 16:15:01
| Show all posts
endmancan achieve all the results,
if exists(select 1 from sysobjects where id = object_id(N't1'))
drop table t1
go

create table t1
(sno varchar(10),
sname varchar(10)
)

if exists(select 1 from sysobjects where id = object_id(N't2'))
drop table t2
go
create table t2
(cno varchar(10),
cname varchar(10)
)


if exists(select 1 from sysobjects where id = object_id(N't3'))
drop table t3
go
create table t3
(sno varchar(10),
cno varchar(10),
grade int
)

insert into t1 values('201601','Zhang San')
insert into t1 values('201602','Li Si')
insert into t2 values('1','Chinese')
insert into t2 values('2','Math')
insert into t2 values('3','English')
insert into t3 values('201601','1',100)
insert into t3 values('201601','2',80)
insert into t3 values('201601','3',120)
insert into t3 values('201601','1',111)
insert into t3 values('201601','2',90)
insert into t3 values('201601','3',124)
go

--Execute query
declare @sql varchar(2000)

set @sql ='select t1.sno,t1.sname '
select @SQl = @sql +',sum(case t3.cno when'+ t2.cno + 'then t3.grade else 0 end)' + t2.cname
from t2

set @Sql = @sql + 'from t1, t3 where t1.sno = t3.sno group by t1.sno,t1.sname'
exec( @sql)
Got as follows

sno sname language mathematics english
201601 Zhang San 211 170 244
But what I want is:
sno sname language mathematics english
201601 Zhang San 100 80 120
201602 Li Si 111 90 124
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-6 22:30:01
| Show all posts
endmancan be achieved: I am sorry that I inserted it wrong when you inserted it. You will explain the question I asked
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