由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 请教2个sql query 问题
相关主题
urgent help! insert value into table多对多relation?
About INSERT IGNOREquery estimation shows cost 900%?
[转载] Can anyone interpret this simple SQL?parameterized queries with no inputs
誰來幫我開來開悄? Interesting SQL queryint or varchar?
问一个SQL Server的问题请教一个在stored procedure 里用bcp的问题.
error of executing SQL query of string concatenation (转载再问not exist和not in
Merge table with one single query?请问这两个SQL QUERY有什么错?
A sql questionanyone can help on this query, thanks!
相关话题的讨论汇总
话题: attribute话题: k2话题: k1话题: k3话题: sql
进入Database版参与讨论
1 (共1页)
c***n
发帖数: 921
1
请教2个sql query 问题:
1. 假设有table A, and A has attribute k1, k2, k3, k4, k5
怎样判断{k1, k2, k3} 是否是一个candidate key? 也就是说是否{k1, k2, k3}
uniquely identify each row.
2. 假设有table A, and A has a numeric attribute "date". It stores
information like "200604", which means April in year 2006. Now I want to
derive a new table B, which contains two numerical attribute "Month" and "
year" from A. How to write the query?
也就是说从原来的 200604 变成 2006 和 4, 都是numeric attribute.
谢谢看完我的问题.
B*****g
发帖数: 34098
2
1. http://www.w3schools.com/sql/sql_groupby.asp
2. substr/substring

【在 c***n 的大作中提到】
: 请教2个sql query 问题:
: 1. 假设有table A, and A has attribute k1, k2, k3, k4, k5
: 怎样判断{k1, k2, k3} 是否是一个candidate key? 也就是说是否{k1, k2, k3}
: uniquely identify each row.
: 2. 假设有table A, and A has a numeric attribute "date". It stores
: information like "200604", which means April in year 2006. Now I want to
: derive a new table B, which contains two numerical attribute "Month" and "
: year" from A. How to write the query?
: 也就是说从原来的 200604 变成 2006 和 4, 都是numeric attribute.
: 谢谢看完我的问题.

j*****n
发帖数: 1781
3
T-SQL:

IF EXISTS(
SELECT k1, k2, k3 FROM A
GROUP BY k1,k2,k3
Having count(1) > 1
)
PRINT 'Not a candidate key'
ELSE
PRINT 'Yes, it is'
INSERT INTO B (Month, Year)
SELECT RIGHT(Convert(varchar(6), date), 2) Month,
LEFT(Convert(varchar(6), date), 4) Year
FROM A

【在 c***n 的大作中提到】
: 请教2个sql query 问题:
: 1. 假设有table A, and A has attribute k1, k2, k3, k4, k5
: 怎样判断{k1, k2, k3} 是否是一个candidate key? 也就是说是否{k1, k2, k3}
: uniquely identify each row.
: 2. 假设有table A, and A has a numeric attribute "date". It stores
: information like "200604", which means April in year 2006. Now I want to
: derive a new table B, which contains two numerical attribute "Month" and "
: year" from A. How to write the query?
: 也就是说从原来的 200604 变成 2006 和 4, 都是numeric attribute.
: 谢谢看完我的问题.

c***n
发帖数: 921
4
T-SQL 应该和 PL/SQL 一回事吧. 我用的是db2, 八成语法差不多. 让我查查看.

【在 j*****n 的大作中提到】
: T-SQL:
:
: IF EXISTS(
: SELECT k1, k2, k3 FROM A
: GROUP BY k1,k2,k3
: Having count(1) > 1
: )
: PRINT 'Not a candidate key'
: ELSE
: PRINT 'Yes, it is'

j*****n
发帖数: 1781
5
嗯,差不多,尤其 2005 搞过 Oracle 的就能搞。

【在 c***n 的大作中提到】
: T-SQL 应该和 PL/SQL 一回事吧. 我用的是db2, 八成语法差不多. 让我查查看.
c***n
发帖数: 921
6
I found another method. It works well.
insert INTO B (MONTH, YEAR)
select mod(date,100),date/100
FROM A

【在 j*****n 的大作中提到】
: T-SQL:
:
: IF EXISTS(
: SELECT k1, k2, k3 FROM A
: GROUP BY k1,k2,k3
: Having count(1) > 1
: )
: PRINT 'Not a candidate key'
: ELSE
: PRINT 'Yes, it is'

1 (共1页)
进入Database版参与讨论
相关主题
anyone can help on this query, thanks!问一个SQL Server的问题
请教一个求职面试题:如何写一个SQL query求表中对角线的和error of executing SQL query of string concatenation (转载
这个query怎么写?Merge table with one single query?
sql的2个问题 (转载)A sql question
urgent help! insert value into table多对多relation?
About INSERT IGNOREquery estimation shows cost 900%?
[转载] Can anyone interpret this simple SQL?parameterized queries with no inputs
誰來幫我開來開悄? Interesting SQL queryint or varchar?
相关话题的讨论汇总
话题: attribute话题: k2话题: k1话题: k3话题: sql