① sql怎么把多条数据合并成一条数据
把多条数据合并成一条数据的代码:
select sum(case when wgrp_id='2' then quota end) w2, sum(case when wgrp_id='3' ;then quota end) w3, mm;
from table;
group by mm。
SQL语言,是结构化查询语言(Structured Query Language)的简称。SQL语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
SQL语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统可以使用相同的结构化查询语言作为数据输入与管理的接口。SQL语言语句可以嵌套,这使他具有极大的灵活性和强大的功能。
应用信息:
结构化查询语言SQL(STRUCTURED QUERY LANGUAGE)是最重要的关系数据库操作语言,并且它的影响已经超出数据库领域,得到其他领域的重视和采用,如人工智能领域的数据检索,第四代软件开发工具中嵌入SQL的语言等。
支持标准:
SQL 是1986年10 月由美国国家标准局(ANSI)通过的数据库语言美国标准,接着,国际标准化组织(ISO)颁布了SQL正式国际标准。1989年4月,ISO提出了具有完整性特征的SQL89标准,1992年11月又公布了SQL92标准,在此标准中,把数据库分为三个级别:基本集、标准集和完全集。
② 1、sql查询语句时怎么把几个字段拼接成一个字段这几个字段是整型的。
SQL中字符型字段的拼接可以使用“+”或者“||”来实现,不过考虑到你问题中这几个字段属于整形字段,需要先将整形进行数据类型转换后,再使用“+”或者“||”拼接。
拼接的语法为:
SELECT CAST(COLUMN1 AS VARCHAR(10)) + '-' + CAST(COLUMN2 AS VARCHAR(10)+ '-' + ........) as P FROM TABLE ;
或者:
SELECT CAST(COLUMN1 AS VARCHAR(10)) || '-' + CAST(COLUMN2 AS VARCHAR(10) || '-' ||........) as P FROM TABLE ;
其中CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型。CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型。
语法:CAST (expression AS data_type)
举个例子:已知表 TableA,含ID、yy、mm、dd四个整数型字段。
(2)sql中的quota扩展阅读:
在SQL中,不同的数据库拼接字段的方法不尽相同,如
MySQL: CONCAT()
Oracle: CONCAT(), ||
SQL Server: +
CONCAT() 的语法如下:
CONCAT(字串1, 字串2, 字串3, ...): 将字串1、字串2、字串3,等字串连在一起。
请注意,Oracle的CONCAT()只允许两个参数;
换言之,一次只能将两个字串串连起来。不过,在Oracle中,我们可以用'||'来一次串连多个字串。
例子1:
MySQL/Oracle:
SELECT CONCAT(region_name,"-",store_name) name FROM Geography
或
SELECT CONCAT(region_name,store_name) FROM Geography
WHERE store_name = 'Boston';
结果:
'EastBoston
③ sql语句中通配符有几种
SQL语句中通配符有四种,分别是百分号%、下划线_、方括号[
]、方括号[!](或[^])。
在搜索数据库中的数据时,SQL
通配符可以替代一个或多个字符。SQL
通配符必须与
LIKE
运算符一起使用。
1、百分号%:可匹配任意类型和长度的字符,如果是中文,请使用两个百分号即%%。
2、下划线_:匹配单个任意字符,它常用来限制表达式的字符长度。
3、方括号[
]:指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
方括号[^]或者[!]:其取值也[
]相同,但它要求所匹配对象为指定字符以外的任一个字符。
(3)sql中的quota扩展阅读:
SQL通配符是使用用特殊字符或语法拼写,用来执行模糊搜索指令的语句功能。在搜索数据库中的数据时,SQL
通配符可以替代一个或多个字符。
SQL
通配符必须与
LIKE
运算符一起使用。如在数据中搜索以大写“A”字母开头的字符类型数据,可以使用like
"A%"。
下划线和百分号在sql模糊查询like语句中为特殊字符,分别可匹配1个字符和0到多个字符,如果需要真正查询特殊字符得转义,如like
'a\_b%'
escape
'\',将匹配前3个字符为a_b的所有记录。
参考资料来源:搜狗网络-SQL通配符
④ SQL清除语句
CREATE SNAPSHOT [schema.]snapshot
[ [PCTFREE integer] [PCTUSED integer]
[INITRANS integer] [MAXTRANS integer]
[TABLESPACE tablespace]
[STORAGE storage_clause]
[ USING INDEX [ PCTFREE integer | TABLESPACE tablespace
| INITTRANS integer | MAXTRANS integer
| STORAGE storage_clause ] ...
| [CLUSTER cluster (column [, column]...)] ]
[ REFRESH [FAST | COMPLETE | FORCE] [START WITH date] [NEXT date]]
AS subquery
schema
is the schema to contain the snapshot. If you omit schema, Oracle
creates the snapshot in your schema.
snapshot
is the name of the snapshot to be created.
Oracle chooses names for the table, views, and index used to
maintain the snapshot by prefixing the snapshot name. To limit
these names to 30 bytes and allow them to contain the entire
snapshot name, Oracle Corporation recommends that you limit your
snapshot names to 23 bytes.
PCTFREE
PCTUSED
INITRANS
MAXTRANS
establishes values for these parameters for the internal table
Oracle uses to maintain the snapshot's data.
TABLESPACE
specifies the tablespace in which the snapshot is to be created. If
you omit this option, Oracle creates the snapshot in the default
tablespace of the owner of the snapshot's schema.
STORAGE
establishes storage characteristics for the table Oracle uses to
maintain the snapshot's data.
USING INDEX
specifies the storage characteristics for the index on a simple
snapshot. If the USING INDEX clause not specified, the index is
create with the same tablespace and storage parameters as the
snapshot.
CLUSTER
creates the snapshot as part of the specified cluster. Since a
clustered snapshot uses the cluster's space allocation, do not use
the PCTFREE, PCTUSED, INITRANS, or MAXTRANS parameters, the
TABLESPACE option, or the STORAGE clause in conjunction with the
CLUSTER option.
REFRESH
specifies how and when Oracle automatically refreshes the snapshot:
FAST
specifies a fast refresh, or a refresh using only the
updated data stored in the snapshot log associated
with the master table.
COMPLETE
specifies a complete refresh, or a refresh that re-
executes the snapshot's query.
FORCE
specifies a fast refresh if one is possible or
complete refresh if a fast refresh is not possible.
Oracle decides whether a fast refresh is possible at
refresh time.
If you omit the FAST, COMPLETE, and FORCE options,
Oracle uses FORCE by default.
START WITH
specifies a date expression for the first automatic
refresh time.
NEXT
specifies a date expression for calculating the
interval between automatic refreshes.
Both the START WITH and NEXT values must evaluate to a time in the
future. If you omit the START WITH value, Oracle determines the
first automatic refresh time by evaluating the NEXT expression when
you create the snapshot. If you specify a START WITH value but omit
the NEXT value, Oracle refreshes the snapshot only once. If you
omit both the START WITH and NEXT values or if you omit the REFRESH
clause entirely, Oracle does not automatically refresh the snapshot.
AS subquery
specifies the snapshot query. When you create the snapshot, Oracle
executes this query and places the results in the snapshot. The
select list can contain up to 253 expressions. A snapshot query is
subject to the same restrictions as a view query.
PREREQUISITES:
To create a snapshot in your own schema, you must have CREATE
SNAPSHOT system privilege. To create a snapshot in another user's
schema, you must have CREATE ANY SNAPSHOT system privilege.
Before a snapshot can be created, the user SYS must run the SQL
script DBMSSNAP.SQL on both the database to contain the snapshot and
the database(s) containing the tables and views of the snapshot's
query. This script creates the package SNAPSHOT which contains both
public and private stored proceres used for refreshing the
snapshot and purging the snapshot log. The exact name and location
of this script may vary depending on your operating system.
When you create a snapshot, Oracle creates a table, two views, and
an index in the schema of the snapshot. Oracle uses these objects
to maintain the snapshot's data. You must have the privileges
necessary to create these objects. For information on these
privileges, see the CREATE TABLE, CREATE VIEW, and CREATE INDEX
commands.
The owner of the schema containing the snapshot must have either
space quota on the tablespace to contain the snapshot or UNLIMITED
TABLESPACE system privilege. Also, both you (the creator) and the
owner must also have the privileges necessary to issue the
snapshot's query.
To create a snapshot, you must be using Oracle with the proceral
option. To create a snapshot on a remote table or view, you must
also be using the distributed option.
⑤ SQL Server常用到的几个设置选项
1. SET DEADLOCK_PRIORITY
2. SET LOCK_TIMEOUT
3.@@LOCK_TIMEOUT
4.SET IDENTITY_INSERT
5.SET IMPLICIT_TRANSACTIONS
6.SET NOCOUNT
7.@@ROWCOUNT
8.SET ROWCOUNT
9.SET TRANSACTION ISOLATION LEVEL
10.SET XACT_ABORT
1. SET DEADLOCK_PRIORITY
说明:控制在发生死锁情况时会话的反应方式。如果两个进程都锁定数据,并且直到其它进程释放自己的锁时,每个进程才能释放自己的锁,即发生死锁情况。
语法:SET DEADLOCK_PRIORITY { LOW | NORMAL | @deadlock_var }
参数:LOW 指定当前会话为首选死锁牺牲品。Microsoft® SQL Server™ 自动回滚死锁牺牲品的事务,并给客户端应用程序返回 1205 号死锁错误信息。
NORMAL 指定会话返回到默认的死锁处理方法。
@deadlock_var 是指定死锁处理方法的字符变量。如果指定 LOW,则 @deadlock_var 为 3;如果指定 NORMAL,则 @deadlock_var 为 6。
注释:SET DEADLOCK_PRIORITY 的设置是在执行或运行时设置,而不是在分析时设置。
权限:SET DEADLOCK_PRIORITY 权限默认授予所有用户。
2. SET LOCK_TIMEOUT
说明:指定语句等待锁释放的毫秒数。
语法:SET LOCK_TIMEOUT timeout_period
参数:timeout_period 是在 Microsoft® SQL Server™ 返回锁定错误前经过的毫秒数。值为 -1(默认值)时表示没有超时期限(即无限期等待)。
当锁等待超过超时值时,将返回错误。值为 0 时表示根本不等待,并且一遇到锁就返回信息。
注释:在连接开始时,该设置的值为 -1。设置更改后,新设置在其余的连接时间里一直有效。
SET LOCK_TIMEOUT 的设置是在执行或运行时设置,而不是在分析时设置。
READPAST 锁定提示为该 SET 选项提供了另一种方式。
权限:SET LOCK_TIMEOUT 权限默认授予所有用户。
⑥ SQL 中的反斜杠有何意义
楼主的SQL语句原先是不是换行的。
一般编写SQL语句或PROC等嵌入式SQL时,考虑换行书写规范和可读性,人为用"\"告诉编译器换行,并无实际表达式意义。
这个方式很常见,比如:shell,linux/unix命令,c编程等。