oracle sql选择脚本帮助

时间:2010-09-20 12:17:11

标签: sql oracle

任何人都可以帮助选择脚本吗?

期望的结果

 TABLENAME, ATTRIBUTE, NULLABLE, DATATYPE, LENGTH, DESC, HELP_TEXT having count(datadic.ATTRIBUTE)>1  (because these can only be unique) 

并且对于具有相同值的任何其他属性(不包括DDKEY和SerNo)并不是唯一的。

 TABLE2 ADDR4 NULL VARCHAR2 15 Unit Address Line 4 Unit address line 4 
 TABLE1 ADDR4 NULL VARCHAR2 30 Unit Address Line 4 Unit address line 4 

我试图了解嵌套查询

现实生活中的senerio我想看看我的数据库是否在不同的表格中具有不同值的任何属性,这是一种家务保持练习。

我有154个表700属性

到目前为止

 select attribute,count(DATADIC.ATTRIBUTE) as "ATTRIBUTE" 
 from "DATADIC" "DATADIC"
 group by DATADIC.ATTRIBUTE 
 having count(datadic.ATTRIBUTE)>1)
 order by count(DATADIC.ATTRIBUTE)

然后

 select TABLENAME,ATTRIBUTE,NULLABLE, DATATYPE, LENGTH, DESC, HELP_TEXT 
 from datadic 
 where      
 attribute in
 (select attribute,count(DATADIC.ATTRIBUTE) as "ATTRIBUTE" 
   from "DATADIC" "DATADIC"
   group by DATADIC.ATTRIBUTE
   having count(datadic.ATTRIBUTE)>1)
   order by count(DATADIC.ATTRIBUTE)

的值太多

 desc
 DATADIC.
  DDKEY Number 
  SER Number 
  TABLENAME Varchar2 15 
  ATTRIBUTE Varchar2 25
  NULLABLE Varchar2 10 
  DATATYPE Varchar2 15  
  LENGTH number(7,3) 
  DESCRIPTION Varchar2 30
  HELP_TEXT Varchar2 1000

样本数据

556 5 TABLE2 ADDR1 NULL VARCHAR2 30 Unit Address Line 1 Unit address line 1 
545 5 TABLE1 ADDR1 NULL VARCHAR2 30 Unit Address Line 1 Unit address line 1 
546 6 TABLE1 ADDR2 NULL VARCHAR2 30 Unit Address Line 2 Unit address line 2 
557 6 TABLE2 ADDR2 NULL VARCHAR2 30 Unit Address Line 2 Unit address line 2 
547 7 TABLE1 ADDR3 NULL VARCHAR2 30 Unit Address Line 3 Unit address line 3 
558 7 TABLE2 ADDR3 NULL VARCHAR2 30 Unit Address Line 3 Unit address line 3 
559 8 TABLE2 ADDR4 NULL VARCHAR2 15 Unit Address Line 4 Unit address line 4 
548 8 TABLE1 ADDR4 NULL VARCHAR2 30 Unit Address Line 4 Unit address line 4 

有用的脚本

CREATE TABLE  "DATADIC" 
(   "DDKEY" NUMBER, 
"SER" NUMBER, 
"TABLENAME" VARCHAR2(15), 
"ATTRIBUTE" VARCHAR2(25), 
"NULLABLE" VARCHAR2(10), 
"DATATYPE" VARCHAR2(15), 
"LENGTH" NUMBER(7,3), 
"DESCRIPTION" VARCHAR2(30), 
"HELP_TEXT" VARCHAR2(1000), 
 CONSTRAINT "DATADIC_PK" PRIMARY KEY ("DDKEY") ENABLE
)

/

CREATE OR REPLACE TRIGGER  "bi_DATADIC" 
before insert on "DATADIC"              
for each row 
begin  
  for c1 in ( 
   select "DATADIC_SEQ".nextval next_val 
    from dual 
  ) loop 
  :new."DDKEY" :=  c1.next_val; 
 end loop; 
end;

/
ALTER TRIGGER  "bi_DATADIC" ENABLE
/

data csv

 556,5,TABLE2,ADDR1,NULL,VARCHAR2,30,Unit Address Line 1,Unit address line 1
 557,6,TABLE2,ADDR2,NULL,VARCHAR2,30,Unit Address Line 2,Unit address line 2
 558,7,TABLE2,ADDR3,NULL,VARCHAR2,30,Unit Address Line 3,Unit address line 3
 559,8,TABLE2,ADDR4,NULL,VARCHAR2,15,Unit Address Line 4,Unit address line 4
 545,5,TABLE1,ADDR1,NULL,VARCHAR2,30,Unit Address Line 1,Unit address line 1
 546,6,TABLE1,ADDR2,NULL,VARCHAR2,30,Unit Address Line 2,Unit address line 2
 547,7,TABLE1,ADDR3,NULL,VARCHAR2,30,Unit Address Line 3,Unit address line 3
 548,8,TABLE1,ADDR4,NULL,VARCHAR2,30,Unit Address Line 4,Unit address line 4

更新的数据现在正在使用此查询,但我不需要使其不唯一

select * from ( select TABLENAME, ATTRIBUTE, NULLABLE,
DATATYPE,
LENGTH,
DESCRIPTION,
HELP_TEXT, count(*) over (partition by attribute) attr_count
from datadic ) where attr_count > 1

TABLENAME ATTRIBUTE NULLABLE DATATYPE LENGTH DESCRIPTION HELP_TEXT ATTR_COUNT 表2 ADDR1 NULL VARCHAR2 30单元地址行1单元地址行1 2 TABLE1 ADDR1 NULL VARCHAR2 30单元地址行1单元地址行1 2 表1 ADDR2 NULL VARCHAR2 30单元地址行2单元地址行2 2 表2 ADDR2 NULL VARCHAR2 30单元地址行2单元地址行2 2 表1 ADDR3 NULL VARCHAR2 30单元地址行3单元地址行3 2 表2 ADDR3 NULL VARCHAR2 30单元地址行3单元地址行3 2 表2 ADDR4 NULL VARCHAR2 30单元地址行4单元地址行4 2 表1 ADDR4 NULL VARCHAR2 30单元地址行4单元地址行4 2

1 个答案:

答案 0 :(得分:1)

  

选择TABLENAME,ATTRIBUTE,NULLABLE,   DATATYPE,LENGTH,DESC,HELP_TEXT来自   属性中的datadic(选择   attribute,count(DATADIC.ATTRIBUTE)as   来自“DATADIC”“DATADIC”的“ATTRIBUTE”   由DATADIC.ATTRIBUTE组成   count(datadic.ATTRIBUTE)> 1)order by   计数(DATADIC.ATTRIBUTE)

您获取的值太多,因为查询期望在“IN”子句之后有一列。尝试以下查询以使用多次出现的属性获取表中的所有行。

select TABLENAME,
       ATTRIBUTE,
       NULLABLE, 
       DATATYPE, 
       LENGTH, 
       DESC, 
       HELP_TEXT 
  from datadic 
  where attribute in 
  (
  select attribute from (
  select attribute,
         count(DATADIC.ATTRIBUTE) as "ATTRIBUTE" 
   from "DATADIC" "DATADIC" 
   group by DATADIC.ATTRIBUTE 
   having count(datadic.ATTRIBUTE)>1) 
   order by count(DATADIC.ATTRIBUTE)
   )
  )

您可以充分利用分析函数以更简单的方式回答此查询。

select * from (
select TABLENAME,
       ATTRIBUTE,
       NULLABLE, 
       DATATYPE, 
       LENGTH, 
       DESC, 
       HELP_TEXT,
       count(*) over (partition by attribute) attr_count 
  from datadic
) where attr_count > 1