postgresql 고급 응용 행 열 전환&종합 구 화의 실현 방향

머리말
명절 전에 회사 업무 측 이 악당 을 만들어 야 한다.이 보고 서 는 그 달 에 몇 개의 스타 제품 의 판매 상황 을 통계 하 는 데 사용 되 었 는데 우리 의 데 이 터 는 줄 에 따라 저 장 된 것 이 바로 | | | 이다.이렇게 말 하 는 것 도 이상 하 다.우리 가 산 신문(guan)표(yuan)계(la)통(ji)이 쉽게 실현 되 지 못 했다.그래서 나 는 보고 생각 했 는데 sql 을 통 해 이런 보고 서 를 계산 할 수 있다 는 것 을 알 게 되 었 다.😂),그리고 데 이 터 를 보고서 시스템 에 직접 출력 합 니 다.η안 소 생가 d 재 미 를 믿 는 동 W 는 메 시 지 를 남 길 수 있 습 니 다.😄~
보고서
우선,나 는 막대 의 악당 을 모집 합 니 다.L@유 옹 모 는 황산 평 에서 걷 어 차 는 것 을 볼 수 있 습 니 다.OK 하~

허난 성
표 구조

drop table if EXISTS  report1 ;
CREATE TABLE "report1" (
  "id" numeric(22) NOT NULL,
  "date" date NOT NULL,
  "product" varchar(100),
  "channel" varchar(100),
  "amount" numeric(20,4)
);
표 주석
필드
묘사 하 다.
id
메 인 키
date
날짜.
product
생산품
channel
경로
amount
매출 액
표 데이터

INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051726328010100000', '2021-05-04', '  1', '  ', '8899.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051726328010100001', '2021-05-04', '  2', '  ', '99.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100010', '2021-05-04', '  1', '  ', '230.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100011', '2021-05-04', '  2', '  ', '9.9000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100011', '2021-05-04', '  3', '    ', '10.1000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100000', '2021-05-04', '  1', '  ', '10');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100099', '2021-05-04', '  2', '  ', '20000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100033', '2021-05-01', '  1', '  ', '20000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100044', '2021-05-01', '  3', '    ', '12345');

사고 하 다.
@Y 를 보고 조금 만 생각 해 봐,1편 나 h^나 는 누 르 기 | | | @영 파 쓰기Φ모드 이상 절 D 는 여러분 이 보시 면 아 실 거 예요.그리고 보 세 요나 는 여러분 들 이 나 와 함께 초 원 에서 넙치 운 c 를 갈 수 있 을 것 이 라 고 생각 합 니 다.
  • 악당 설 폰 v 를 보면 크게 세 부분 으로 나 뉜 다
  • 일 부 는 전날 A 품 N 이 발매 되 었 습 니 다.
    그리고 일 부 는 전날 a 품 채널 a 품 합.
    마지막 부분 은 채널 별로 하 는 월 y 입 니 다.
  • 악당 M 을 보면 대체적으로 찰겨
  • 일 부 는 전날 거 예요.
    다른 부분 t 는 월 R 입 니 다.
    마지막 부분 t 는 모든 채널 의 a 품 합,일 합,월 합 이다.
    자,아스타틴 은 어떻게 할 까?
    일 부 는 전날 의 또 다른 부분 이 고 t 는 월 R 입 니 다.
    마지막 으로 겨 를 검사 해 야 합 니 다.@유,악당 80%를 완성 한 것 같 습 니 다.마지막 줄 에 이 르 기 까지,@Y 먼저 uP 자 하~
    제1 부분(전날 의)
    제 가 R 을 세 워 서 할 수 있 는 첫 번 째 부분 sql 은 아마 A 사악 한 것 같 습 니 다.(@이 가장 쉬 운 것 같 습 니 다.F.😄)
    
    select
      channel,
      sum(case product when '  1' then amount end) as c1,
      sum(case product when '  2' then amount end) as c2,
      sum(case product when '  3' then amount end) as c3
    from report1
    group by channel ;

    sql 은]뭔 가]하지만 나 는 열 이 하나 빠 졌 다.δ장미 퇴색 ,만약 당신 이 postgresql 창의 의자 가 뇌 를 비 추 면,@YF 의 방식 은 당신 이 이미 알 아 맞 혔 을 것 이 라 고 추측 합 니 다(창over,sql 에...
    
    select
      channel,
      day_sum,
      sum(case product when '  1' then amount end) as c1,
      sum(case product when '  2' then amount end) as c2,
      sum(case product when '  3' then amount end) as c3
    from
      ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
    group by t1.channel ,t1.day_sum;

    하하,상 디day_sum잘 아 시 겠 죠?하하 하~
    땅 을 다 지 는 것 을 보 세 요. 부분 을 성공 적 으로 완 성 했 습 니 다.@Y 가능 한 yc 는 가축 c 일 수 있 습 니 다.
  • 하 나 는 취 합 함sum+분 Mgroup by을 사용 하여 행 A(연postgresql도 다른 좋 은 행 AU 전시회 가 있 습 니 다.@Y 는 개의 치 않 습 니 다 B~)다른 하 나 는 창 함over을 사용 합 니 다.γ해골 미리 만 들 기 R,@우도 일 합(행)이 있 습 니 다~
  • 생각 하기 쉽 지 않 아 요?😂,허난 성
    2 부(월 R)
    월 R 은 울 목 식사 로 보 입 니 다.postgresql 의 날짜 평가 점 수 를 익히 면 됩 니 다.@Y 는 큰 칼 을 쓰 지 않 고 sql 을 내 보 냅 니 다.하하 하.😄
    
    select 
      channel,sum(amount) as month_sum from report1 
    where 
      date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month') 
    group by  
      channel

    악당
    F.저 ⑶ 오소리 찰 겨 는 경로channel필드 에 따라inner join이상 의 찰 겨 를 합병 하고 합병 한 후에 대체적으로@유 옹 입 니 다.

    @sql
    
    select
        ttt.channel,
        sum(ttt.day_sum) as day_sum,
        sum(ttt.month_sum) as month_sum,
        sum(ttt.c1) as c1,
        sum(ttt.c2) as c2,
        sum(ttt.c3) as c3
    from (
            select tt1.*,tt2.month_sum from
            (
            select
            channel,
          day_sum,
            sum(case product when '  1' then amount end) as c1,
            sum(case product when '  2' then amount end) as c2,
            sum(case product when '  3' then amount end) as c3
            from
            ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
            group by t1.channel ,t1.day_sum
            ) as tt1 left join
            (
              select channel,sum(amount) as month_sum from report1 where date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month') group by  channel
            ) as tt2 on tt1.channel = tt2.channel
        ) ttt
    GROUP BY ttt.channel
    order by channel asc
    보 세 요.R 이 있 으 면 가장 KY 과일 이 라 고 할 수 있 습 니 다.하하 하 나 는 오랫동안 쓰 지 않 았 던rollup편지(1처음 나 도
    
    select
        ttt.channel,
        sum(ttt.day_sum) as day_sum,
        sum(ttt.month_sum) as month_sum,
        sum(ttt.c1) as c1,
        sum(ttt.c2) as c2,
        sum(ttt.c3) as c3
        from (
            select tt1.*,tt2.month_sum from
            (
            select
            channel,
          day_sum,
            sum(case product when '  1' then amount end) as c1,
            sum(case product when '  2' then amount end) as c2,
            sum(case product when '  3' then amount end) as c3
            from
            ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
            group by t1.channel ,t1.day_sum
            ) as tt1 left join
            (
              select channel,sum(amount) as month_sum from report1 where date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month') group by  channel
            ) as tt2 on tt1.channel = tt2.channel
        ) ttt
        group by rollup(ttt.channel)
        order by channel asc

    걸상.Φ모 는 내 가 성공 했다 는 뜻 이 야~😂
    Y
    만약 당신 이 W 에 공 을 들 이 겠 다 면,postgresql세 계 는 매우 멋 진|서쪽 도 있 고,또 일부|서쪽 도 있 습 니 다.Ρmysql@번 거 롭 게,아니^본저 W 의 마음 B,나 G 는 w 복 할 수 있다@조금,같은 r 나 는 할 수 있다 |서쪽,하하,여러분 힘 내세 요~
    다음 장,나 는 9349°v v 어떻게 F 통^sqlF 전단 합병 와 그 효과 가 신기 하지 않 습 니까?
    여기 서 post gresql 고급 응용 프로그램 에 대한 열 전환&합 쳐 진 실현 사고 에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 post gresql 행 환 열 집합 과 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

    좋은 웹페이지 즐겨찾기