将表创建为...通过联接选择

时间:2018-07-17 13:04:00

标签: postgresql ddl postgresql-9.5

我必须使用两个查询,还是可以在此处写“ as select”?

我像同事写的那样输入查询,目前该查询在SQL中有效。

struct Mystruct
     a::Array{Int64,1}
     Mystruct(a::Array{Int64,1}) = new(copy(a))
end

我显然试图这样做,但是出现了错误:

CREATE TABLE planification_client_biens_test
(nom VARCHAR(50),
serveur VARCHAR(24),
scheduled_backup VARCHAR(8),
schedule VARCHAR(31),
retention VARCHAR(6),
groupe VARCHAR(200),
remote_access VARCHAR(433),
alias VARCHAR(332),
parallelisme VARCHAR(3),
storage_nodes VARCHAR(69),
client_OS_type VARCHAR(31),
version_networker VARCHAR(19),
savesets VARCHAR(4200),
ALIAS_1 VARCHAR(50),
ALIAS_2 VARCHAR(50),
APPLICATION VARCHAR(255),
ENVIRONNEMENT VARCHAR(50),
IMPACT VARCHAR(20),
CLIENT_OPERATIONNEL VARCHAR(50),
TYPE VARCHAR(13),
SITE VARCHAR(30),
COMPOSANT_DE VARCHAR(23),
COMPOSE_DE VARCHAR(17),
ETAT VARCHAR(30),
POIDS DECIMAL(11,2),
EQUIPE_EXPLOITATION VARCHAR(100))
SELECT t1.*, t2.ALIAS_1, t2.ALIAS_2,t2.APPLICATION,t2.ENVIRONNEMENT, t2.IMPACT, t2.CLIENT_OPERATIONNEL, t2.TYPE, t2.SITE, t2.COMPOSANT_DE, t2.COMPOSE_DE, t2.ETAT, t2.POIDS, t2.EQUIPE_EXPLOITATION FROM planification_client_test t1 LEFT OUTER JOIN Biens_test t2 ON t1.nom_court IN (t2.NOM,t2.ALIAS_1,t2.ALIAS_2);

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用create table as时不能为列指定数据类型。

CREATE TABLE planification_client_biens_test
(
  nom,
  serveur,
  scheduled_backup,
  schedule,
  retention,
  groupe,
  remote_access,
  alias,
  parallelisme,
  storage_nodes,
  client_OS_type,
  version_networker,
  savesets,
  ALIAS_1,
  ALIAS_2,
  APPLICATION,
  ENVIRONNEMENT,
  IMPACT,
  CLIENT_OPERATIONNEL,
  TYPE,
  SITE,
  COMPOSANT_DE,
  COMPOSE_DE,
  ETAT,
  POIDS,
  EQUIPE_EXPLOITATION
)
SELECT t1.*, 
       t2.ALIAS_1, 
       t2.ALIAS_2,
       t2.APPLICATION,
       t2.ENVIRONNEMENT, 
       t2.IMPACT, 
       t2.CLIENT_OPERATIONNEL, 
       t2.TYPE, 
       t2.SITE, 
       t2.COMPOSANT_DE, 
       t2.COMPOSE_DE, 
       t2.ETAT, 
       t2.POIDS, 
       t2.EQUIPE_EXPLOITATION 
FROM planification_client_test t1 
  LEFT OUTER JOIN Biens_test t2 ON t1.nom_court IN (t2.NOM,t2.ALIAS_1,t2.ALIAS_2);

从查询的源列中推断出数据类型。

如果您需要在源表和创建的表之间更改类型,则需要在查询中强制转换列,例如:

CREATE TABLE planification_client_biens_test
(
 ....
)
SELECT t1.*, 
       t2.ALIAS_1, 
       t2.ALIAS_2,
       cast(t2.APPLICATION as  VARCHAR(255)),
       t2.ENVIRONNEMENT, 
       t2.IMPACT, 
       t2.CLIENT_OPERATIONNEL, 
       cast(t2.TYPE as varchar(13)), 
       t2.SITE, 
       cast(t2.COMPOSANT_DE as VARCHAR(23)), 
       cast(t2.COMPOSE_DE as VARCHAR(17)), 
       t2.ETAT, 
       t2.POIDS, 
       t2.EQUIPE_EXPLOITATION 
FROM planification_client_test t1 
  LEFT OUTER JOIN Biens_test t2 ON t1.nom_court IN (t2.NOM,t2.ALIAS_1,t2.ALIAS_2);