오류: "array_agg" 칼럼을 하나 이상 지정했음

 

에러만 봐서는.. array_agg 컬럼을 여러개 써서 그런거 같은데

(한 쿼리에 array_agg를 여러번 쓰면 안되는 것으로 이해함)

정작 에러코드를 찾아가보면 컬럼명이 동일한게 존재하기 때문에 발생하는 에러라고 해석된다.

 

array_agg라는 이름으로 컬럼명이 생성되다 보니 그런건데.. 해석이 아무리봐도 이상한 듯..

결론은.. as로 다른 이름을 지정해주면 끝.

 

DB2 SQL-Error: -121
SQLState: 42701
Short Description: THE COLUMN IS IDENTIFIED MORE THAN ONCE IN THE INSERT OR UPDATE OR SET TRANSITION VARIABLE STATEMENT
The same column 'name' is specified more than once, either in the list of object columns of an INSERT statement, in the SET clause of an UPDATE statement, or in a SET transition variable statement. System action: The statement cannot be executed. No data was inserted or updated in the object table. Programmer response: Correct the syntax of the statement so that each column name is specified only once.

[링크 : http://www.sqlerror.de/db2_sql_error_-121_sqlstate_42701.html]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

postgresql loop 반복문  (0) 2019.12.20
sql array any all  (0) 2019.12.20
sql scan과 index  (0) 2019.12.19
postgresql 에서 ,로 구분되는 문자열로 내보내기  (0) 2019.12.18
postgres create view / materialized view  (0) 2019.12.15
Posted by 구차니

좀 봐야 할듯..

join으로 테이블들 큰 것 합치다 보면 오래 걸리는데 그것에 대한 설명.

 

[링크 : https://baseofmint.tistory.com/5]

[링크 : https://jojoldu.tistory.com/162]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

sql array any all  (0) 2019.12.20
postgres 동일 컬럼명 에러  (0) 2019.12.20
postgresql 에서 ,로 구분되는 문자열로 내보내기  (0) 2019.12.18
postgres create view / materialized view  (0) 2019.12.15
sql not exists  (0) 2019.12.09
Posted by 구차니

말이 어렵다 -ㅁ-

array_agg()를 통해서 합치게 되면

{1,2,3} 이런식으로 중괄호가 붙게 되는데

이걸 없애고 순수하게(?)

1,2,3 으로만 출력하게 하는 방법

 

아래와 같이 하면 array_agg에 의해 배열로 {}로 붙게 되고 array_to_string()에 의해

{}가 제거된 채 원하는 의도대로 1,2,3 으로 출력되게 된다.

array_to_string(array_agg(DISTINCT datas),',')

[링크 : https://stackoverflow.com/questions/11899024/postgresql-query-to-return-results-as-a-comma-separated-list]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

postgres 동일 컬럼명 에러  (0) 2019.12.20
sql scan과 index  (0) 2019.12.19
postgres create view / materialized view  (0) 2019.12.15
sql not exists  (0) 2019.12.09
sql filter() over()  (0) 2019.12.09
Posted by 구차니

create view는 먼가.. 막 복잡할줄 알았는데 별 차이가 없네?

query 부분을 ' '로 감싸면서 안에 있는 '를 막 '''로 치환해줘야 할 줄 알았는데..

[링크 : https://www.postgresql.org/docs/9.2/sql-createview.html]

[링크 : https://blog.naver.com/jdub7138/220863929987]

 

materialzied view 라는게 pgadmin에서 본게 기억나서 찾아보니

단순하게 메모리에 생겨나는게 아닌 물리적으로 공간을 차지하는 뷰라고 한다.

오라클에서 처음 도입되었다고 하는데. 그건 나에게 중요한건 아니니..

[링크 :https://ko.wikipedia.org/wiki/구체화_뷰]

[링크 : http://www.gurubee.net/lecture/1857]

 

postgres 9.3 이후버전 부터 사용이 가능한 것 같고

(다만 9.3 버전은 unsupportted 버전으로 지정되었지만.. 여기서 unsupportted는 LTS등의 기간이 끝난거겠지?)

view랑은 아무튼 구분되어서 저장되는 것으로 보인다.

[링크 : https://www.postgresql.org/docs/9.3/rules-materializedviews.html]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

sql scan과 index  (0) 2019.12.19
postgresql 에서 ,로 구분되는 문자열로 내보내기  (0) 2019.12.18
sql not exists  (0) 2019.12.09
sql filter() over()  (0) 2019.12.09
sql 표준함수 종류  (0) 2019.12.09
Posted by 구차니

단순하게 하면 되는줄 알았는데 단순한 애가 아니었네...

 

suppliers를 SUPER SET 이라고 정의하고

subquery의 orders를 SUB SET이라고 정의하면

 

suppliers를 A orders 를 B라고 정의하면

NOT EXISTS는 A-B 차집합 결과를

EXISTS는 A∩B교집합 결과를 돌려준다.

조금은.. WHERE IN과 다르게 비교 조건이 subquery에 들어가는 느낌이라고 해야하려나?

대신.. NOT IN으로 하면 결과가 30분 넘게 걸리는게 몇초도 안되서 바로 결과가 나오니 좋다.

SELECT *

  FROM suppliers

 WHERE NOT EXISTS (SELECT * FROM orders

                            WHERE suplliers. supplier_id = orders.supplier_id);

[링크 : https://20140501.tistory.com/57]

Posted by 구차니

집계 함수를 창 함수로 사용하기 위해서는 filter() over()가 끼면 되는 듯

 

The filter clause works for any aggregate function: besides the well-known functions such as sum and count, it also works for array_agg and ordered set functions (e.g., listagg).

>> filter 절은 어떠한 aggregate 함수와도 사용이 가능함: ...

If an aggregate function is used as a window function (over clause), the syntactic order is: aggregate function, filter clause, over clause:

>> 만약 aggregate 함수가 window 함수로 사용이 되어진다면  (over 절), 문법적 순서는 aggregate, filter, over 순으로 된다.

SUM(<EXPRESSION>) FILTER(WHERE <condition>)

SUM(...) FILTER(WHERE ...) OVER (...)

[링크 : https://modern-sql.com/feature/filter]

 

order by, group by 서브쿼리를 개선하기 위해 나온 함수

[링크 : https://javaexpert.tistory.com/503]

Posted by 구차니

aggregate - count(), avg(), sum(), min(), max()

group - 소계, 중계 합계

 > rollup() 그룹핑된 컬럼의 소계를 생성

 > grouping() , group by rollup()

 > group by  cube()

window - rank(), row_number()

[링크 : https://jeong-pro.tistory.com/41]

 

윈도우 함수

분석함수(analytic function) 혹은 순위함수(rank function)

데이터 웨어하우스에서 발전한 기능

[링크 : https://eehoeskrap.tistory.com/79]

[링크 : https://gaiag.tistory.com/27]

Posted by 구차니

예전에 적어둔게 없었나..

아무튼.. ALTER SEQUENCE를 통해 RESTART WITH 뒤에 원하는 숫자를 넣어주면 해당 숫자로 시작을 다시 한다.

(예전 기억으로는 0은 안되고 1부터 시작했던 걸로 기억함.)

 

ALTER SEQUENCE "시퀀스이름" RESTART WITH 1

[링크 : https://gs.saro.me/dev?page=2&tn=5]

Posted by 구차니

SQL 문을 통해 이것저것 조인을 하다 보면

예상하지 못하게 이상하게 갯수가 많이 늘어가는 경우가 발생한다.

대개(?)는 1:1 조건인줄 알았던 테이블이 예상외로 1:N 조건이기 때문에 발생을 하는데

 

ROW_NUMBER() OVER(PARITION BY)를 이용하여 left join 결과를 정렬후

상위 쿼리에서(즉 LEFT_JOIN은 서버 쿼리에서 수행) ROW_NUMBER = 1인 조건으로 걸러내면 될 듯 하다.

 

일단 해보진 않았으니 패스~ 이론적으로만 알아둬야지.. 끄응

 

SELECT
    A.PersonName, A.Email
FROM
        (
        Select Person.PersonName, Email.Email
            ,ROW_NUMBER() OVER(PARTITION BY Person.ID ORDER BY Email.Email) AS RN
        From person 
        left join Email on Person.ID=Email.PersonId
        ) A
WHERE A.RN = 1

[링크 : https://dba.stackexchange.com/questions/103815...]

[링크 : https://stackoverflow.com/questions/3375436/how-do-i-limit-a-left-join-to-the-1st-result-in-sql-server]

 

+

UPDATE 쿼리에서 join을 통해 할 경우에도

아마도.. 물리적으로 배열된 컬럼중에 가장 위에것이 되지 않겠냐 라는 답변이 있는데

반대로 생각하면 할때 마다 다른 결과가 나올수도 있다.. 라는 의미가 된다.

[링크 : https://stackoverflow.com/questions/9502449/updating-and-join-on-multiple-rows-which-rows-value-is-used]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

sql 표준함수 종류  (0) 2019.12.09
postgresql 시퀀스 값 초기화 하기  (0) 2019.11.29
array_agg 빈 배열을 누적할 수 없습니다.  (0) 2019.11.28
sql over() partition by  (0) 2019.11.28
pg_tables, pg_sequences  (0) 2019.11.28
Posted by 구차니

하나는 들어있고 다른 하나는 비어있는 걸 합치려고 하니 에러가 나서

찾아보니 그냥 함수를 만들어 버린듯 

CREATE AGGREGATE array_accum (anyarray)
(
    sfunc = array_cat,
    stype = anyarray,
    initcond = '{}'
);  


[링크 :
https://gist.github.com/ryandotsmith/4602274]]

[링크 : https://codeday.me/ko/qa/20190625/887424.html]

Posted by 구차니