how to write this SQL statement

York_Kwan 2004-01-18 06:45:48
the dataSet as this:
//---------------------------------------------
NAME SUBJECT TEAM SCORE  //DataField
sinmax math first 60
myname math second 70
myname math third 80      
myname english first 60      
myname english second 70      
myname english third 80    
myname chinese first 60    
myname chinese second 70    
myname chinese third 80  
//---------------------------------------------


after Excute the SQL sentence,
it return:
//------------------------------------------
NAME math english chinese //DataField
myname 210 210 210
...全文
34 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
LGQDUCKY 2004-01-19
  • 打赏
  • 举报
回复
declare
v_sql varchar(2000):='select name';
begin
select v_sql||',sum(case subject when '''||subject||'''then score end )'||subject into v_sql from (select distinct subject from test) a;
v_sql:=v_sql||'from test group by name';
--execute immediate( v_sql);
open c for v_sql;
end;
/

这个只能返回单行,对于多行只能是使用游标。
beckhambobo 2004-01-19
  • 打赏
  • 举报
回复

select name,sum(decode(SUBJECT,'math',SCORE)) math,
sum(decode(subject,'english',score)) english,
sum(decode(subject,'chinese',score)) chinese
from tabname group by name
welyngj 2004-01-18
  • 打赏
  • 举报
回复
我写了个,但是出现问题
declare
v_sql varchar(2000):='select name';
begin
select v_sql||',sum(case subject when '''||subject||'''then score end )'||subject into v_sql from (select distinct subject from test) a;
v_sql:=v_sql||'from test group by name';
execute immediate( v_sql);
end;
/

*
ERROR 位于第 1 行:
ORA-01422: 实际返回的行数超出请求的行数
ORA-06512: 在line 4
怎么办?
在sqlserver中这种问题太容易实现了。
oracle的语法不熟


What this book covers Chapter 1, Microsoft SQL Server Database Design Principles, explains the database design process and the architecture and working of the SQL Server 2014 Storage Engine. This chapter covers the database development life cycle in detail, including the normalization and denormalization process, beneits of choosing appropriate data types, and the functioning of the SQL Server 2014 Storage Engine. Chapter 2, Understanding DDL and DCL Statements in SQL Server, introduces the reader to the SQL Server 2014 Transact-SQL language elements and SQL Server 2014 Management Studio (SSMS 2014). This chapter explains Transact-SQL DDL, DCL, and DML language elements in detail, and how you can use them to create, manage, and secure SQL Server 2014 databases, schemas, and tables. This chapter also shows you how you can use SQL Server Management Studio to create and manage SQL Server 2014 databases, schemas, and tables. Finally, this chapter covers the purpose of SQL Server 2014 system databases and highlights the advantages and disadvantages of database recovery models. Chapter 3, Data Retrieval Using Transact-SQL Statements, demonstrates how to query data from tables, how to write multiple table queries, and how to group, organize, and pivot result set data. This chapter explores the basic form of the SELECT statement and how it can be used to query data from tables. This chapter also highlights the different categories of built-in T-SQL functions and how you can use them in your SELECT statements. This chapter also explains different techniques that you can use to combine data from multiple tables, how to organize the data, and how to generate the summary data by grouping or pivoting it. Finally, this chapter covers the purpose of the CTE and SQL Server 2014 windowing functions and how to use them to quickly solve complex analytical tasks. Chapter 4, Data Modiication with SQL Server Transact-SQL Statements, illustrates how to add, modify, and delete data in tables using Transact-SQL DML statements. This chapter covers how to add data to a table using the INSERT statement, how to delete the data using the DELETE statement, and how to update existing data using the UPDATE statement. This chapter also covers the SELECT…INTO, MERGE, and TRUNCATE TABLE statements, and it highlights the key new enhancements of these statements in SQL Server 2014. Chapter 5, Understanding Advanced Database Programming Objects and Error Handling, covers reusable programming objects that includes views, stored procedures (normal and natively compiled), functions and triggers (based on either DDL or DML), SQL Server 2014 control-of-low statements, and structured error handling blocks. This chapter shows you how you can declare and use variables and how you can use control-of-low statements to control your program execution. Next, it explains the purpose of views, stored procedures, user-deined functions, and triggers, and highlights the guidelines and restrictions to design each of these programmable objects. This chapter also illustrates how to handle errors that occur within Transact-SQL batches and programmable objects using the TRY...CATCH construct. Chapter 6, Performance Basics, explains performance-related features of SQL Server 2014. This chapter irst explains the architecture of the SQL Server Relational Engine. Then, it introduces the architecture of the SQL Server 2014 in-memory technology. Next, it covers all SQL Server 2014 index types and how they can be used to achieve optimal query performance while reducing the overall response time. Then, it explores the architectural differences of B-tree, Bw-tree, and xVelocity columnstore indexes. Finally, it explains core performance topics such as SQL Server query optimization statistics, SQL Server transactions and locks, and tools that come with SQL Server 2014 Database Engine, which you can use to monitor and troubleshoot its Database Engine performance.
Transact-SQL In Chapter 20, you builtSQL statements to retrieve and update rows in a database. You also learned all the variations of the SELECT statement. Some restrictions, however, can’t be expressed with a WHERE clause, no matter how complicated. To perform complicated searches, many programmers would rather write an application to retrieve the desired rows, process them, and then display some results. The processing could include running totals, formatting of the query’s output, calculations involving multiple rows of the same table, and so on. Most database management systems include extensions, which enhance SQL and make it behave more like a programming language. SQL Server provides a set of statements known as Transact-SQL (T-SQL). T-SQL recognizes statements that fetch rows from one or more tables, flow-control statements like IF…ELSE and WHILE, and numerous functions to manipulate strings, numeric values, and dates, similar to Visual Basic’s functions. With T-SQL you can do everything that can be done with SQL, as well as program these operations. At the very least, you can attach lengthy SQL statements to a database as stored procedures, so that you can call them by name. In addition, you can use parameters, so that the same procedure can act on different data. This chapter is an overview of T-SQL and demonstrates the basic topic with stored proce- dures that perform complicated tasks on the server. We’ll start with the COMPUTE BY state- ment, which allows you to calculate totals on groups of the rows retrieved from the database. This statement looks and feels very much like the straight SQL statements, yet it’s not part of standard SQL. Then we’ll look at T-SQL in depth. If you need to look up a function or how to declare a stored procedure’s arguments, you can use this chapter as reference material. If you’re new to T-SQL, you should read this material, because T-SQL is a powerful language, and if you’re working with or plan to switch to SQL Server, you’ll need it sooner or later. In addition, you’ll improve your Visual Basic programming. T-SQL is the native language of SQL Server. By seeing how the basic operations can be implemented in T-SQL and VB, you’ll gain a deeper under- standing of database programming.

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧