質問するログイン新規登録

回答編集履歴

6

修正版

2017/09/04 11:24

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -76,4 +76,41 @@
76
76
  left join t_zokusei on coalesce(zokusei2,999)=zid
77
77
  order by kaisha,zokusei2;
78
78
 
79
+ ```
80
+
81
+
82
+ # 修正版?
83
+ ```
84
+ create table tbl(kaisha int,zokusei int,hiduke date,suuchi int);
85
+ insert into tbl values
86
+ (1,1,'2017-06-01',100),
87
+ (1,2,'2017-06-01',100),
88
+ (1,3,'2017-06-01',100),
89
+ (1,4,'2017-06-01',100),
90
+ (2,1,'2017-06-01',100),
91
+ (2,2,'2017-06-01',100),
92
+ (2,3,'2017-06-01',100),
93
+ (2,4,'2017-06-01',100),
94
+ (3,1,'2017-06-01',100),
95
+ (3,2,'2017-06-01',100),
96
+ (3,3,'2017-06-01',100),
97
+ (3,4,'2017-06-01',100);
98
+ create table t_kaisha(kid int unique key,kname varchar(20));
99
+ insert into t_kaisha values(1,'F社'),(2,'R社'),(3,'C社'),(999,'合計');
100
+ create table t_zokusei(zid int unique key,zname varchar(20));
101
+ insert into t_zokusei values(1,'店舗'),(2,'免税'),(3,'国内通販'),(4,'海外通販'),(999,'合計');
102
+ select concat(kname,' - ',zname) as kz
103
+ ,coalesce(kaisha,999) as kaisha
104
+ ,coalesce(zokusei2,999) as zokusei2
105
+ ,t1.m6
106
+ from (
107
+ select kaisha,case when zokusei in (1,2) then 1 else zokusei end as zokusei2
108
+ ,sum((date_format(hiduke,'%m')='06')*suuchi) as m6
109
+ from tbl
110
+ where date_format(hiduke,'%Y')='2017'
111
+ group by kaisha asc ,zokusei2 asc with rollup
112
+ ) as t1
113
+ left join t_kaisha on coalesce(kaisha,999)=kid
114
+ left join t_zokusei on coalesce(zokusei2,999)=zid
115
+ order by kaisha,zokusei2;
79
116
  ```

5

小計

2017/09/04 11:24

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -51,13 +51,18 @@
51
51
  会社名と属性を別テーブルで用意してやるとこう
52
52
  ```SQL
53
53
  create table t_kaisha(kid int unique key,kname varchar(20));
54
- insert into t_kaisha values(1,'F社'),(2,'R社');
54
+ insert into t_kaisha values(1,'F社'),(2,'R社'),(999,'全社');
55
55
  create table t_zokusei(zid int unique key,zname varchar(20));
56
- insert into t_zokusei values(1,'店舗'),(2,'免税'),(3,'通販'),(99,'その他');
56
+ insert into t_zokusei values(1,'店舗'),(2,'免税'),(3,'通販'),(99,'その他'),(999,'合計');
57
57
  ```
58
- リレーションしてこう
58
+ リレーションしてこう(小計欄集計をつけときました)
59
59
  ```SQL
60
- select concat(kname,' - ',zname) as kz,t1.*
60
+ select concat(kname,' - ',zname) as kz
61
+ ,coalesce(kaisha,999) as kaisha
62
+ ,coalesce(zokusei2,999) as zokusei2
63
+ ,t1.m5
64
+ ,t1.m6
65
+ ,t1.m7
61
66
  from (
62
67
  select kaisha,case when zokusei in (2,3) then 99 else zokusei end as zokusei2
63
68
  ,sum((date_format(hiduke,'%m')='05')*suuchi) as m5
@@ -65,9 +70,10 @@
65
70
  ,sum((date_format(hiduke,'%m')='07')*suuchi) as m7
66
71
  from tbl,t_zokusei
67
72
  where date_format(hiduke,'%Y')='2017'
68
- group by kaisha,zokusei2
73
+ group by kaisha asc ,zokusei2 asc with rollup
69
74
  ) as t1
70
- left join t_kaisha on kaisha=kid
75
+ left join t_kaisha on coalesce(kaisha,999)=kid
71
- left join t_zokusei on zokusei2=zid
76
+ left join t_zokusei on coalesce(zokusei2,999)=zid
72
- order by kaisha,zokusei2
77
+ order by kaisha,zokusei2;
78
+
73
79
  ```

4

ついでに

2017/09/01 11:54

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -45,4 +45,29 @@
45
45
  from tbl
46
46
  where date_format(hiduke,'%Y')='2017'
47
47
  group by kaisha,zokusei2
48
+ ```
49
+
50
+ # ついでに
51
+ 会社名と属性を別テーブルで用意してやるとこう
52
+ ```SQL
53
+ create table t_kaisha(kid int unique key,kname varchar(20));
54
+ insert into t_kaisha values(1,'F社'),(2,'R社');
55
+ create table t_zokusei(zid int unique key,zname varchar(20));
56
+ insert into t_zokusei values(1,'店舗'),(2,'免税'),(3,'通販'),(99,'その他');
57
+ ```
58
+ リレーションしてこう
59
+ ```SQL
60
+ select concat(kname,' - ',zname) as kz,t1.*
61
+ from (
62
+ select kaisha,case when zokusei in (2,3) then 99 else zokusei end as zokusei2
63
+ ,sum((date_format(hiduke,'%m')='05')*suuchi) as m5
64
+ ,sum((date_format(hiduke,'%m')='06')*suuchi) as m6
65
+ ,sum((date_format(hiduke,'%m')='07')*suuchi) as m7
66
+ from tbl,t_zokusei
67
+ where date_format(hiduke,'%Y')='2017'
68
+ group by kaisha,zokusei2
69
+ ) as t1
70
+ left join t_kaisha on kaisha=kid
71
+ left join t_zokusei on zokusei2=zid
72
+ order by kaisha,zokusei2
48
73
  ```

3

zokuseiまとめ

2017/09/01 11:27

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -35,4 +35,14 @@
35
35
  from tbl
36
36
  where date_format(hiduke,'%Y')='2017'
37
37
  group by kaisha,zokusei
38
+ ```
39
+ 上記サンプルの2と3をまとめて別のコード=99として集計します
40
+ ```SQL
41
+ select kaisha,case when zokusei in (2,3) then 99 else zokusei end as zokusei2
42
+ ,sum((date_format(hiduke,'%m')='05')*suuchi) as m5
43
+ ,sum((date_format(hiduke,'%m')='06')*suuchi) as m6
44
+ ,sum((date_format(hiduke,'%m')='07')*suuchi) as m7
45
+ from tbl
46
+ where date_format(hiduke,'%Y')='2017'
47
+ group by kaisha,zokusei2
38
48
  ```

2

追加情報

2017/09/01 11:07

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -33,5 +33,6 @@
33
33
  ,sum((date_format(hiduke,'%m')='06')*suuchi) as m6
34
34
  ,sum((date_format(hiduke,'%m')='07')*suuchi) as m7
35
35
  from tbl
36
+ where date_format(hiduke,'%Y')='2017'
36
37
  group by kaisha,zokusei
37
38
  ```

1

sample

2017/09/01 10:58

投稿

yambejp
yambejp

スコア118110

answer CHANGED
@@ -1,4 +1,37 @@
1
1
  全体的に無駄が多いように見えます
2
2
 
3
3
  GROUP BY orderer_id,point_of_sale_id
4
- で集計すればよいのでは?
4
+ で集計すればよいのでは?
5
+
6
+ # sample
7
+
8
+ まずデータの持ち方は、会社id、売上属性、計上日、数量の4つが必要です
9
+ それを踏まえてサンプルデータを以下とします
10
+ ```SQL
11
+ create table tbl(kaisha int,zokusei int,hiduke date,suuchi int);
12
+ insert into tbl values
13
+ (1,1,'2017-06-01',100),
14
+ (1,1,'2017-06-01',200),
15
+ (1,1,'2017-06-02',300),
16
+ (1,2,'2017-06-01',150),
17
+ (1,2,'2017-06-01',120),
18
+ (1,2,'2017-06-01',300),
19
+ (1,3,'2017-06-01',200),
20
+ (1,3,'2017-06-01',100),
21
+ (1,3,'2017-06-01',100),
22
+ (1,3,'2017-06-01',300),
23
+ (1,3,'2017-07-01',1000),
24
+ (2,1,'2017-06-01',100),
25
+ (2,1,'2017-06-02',300),
26
+ (2,2,'2017-06-01',100),
27
+ (2,3,'2017-06-01',100);
28
+ ```
29
+ 集計します
30
+ ```
31
+ select kaisha,zokusei
32
+ ,sum((date_format(hiduke,'%m')='05')*suuchi) as m5
33
+ ,sum((date_format(hiduke,'%m')='06')*suuchi) as m6
34
+ ,sum((date_format(hiduke,'%m')='07')*suuchi) as m7
35
+ from tbl
36
+ group by kaisha,zokusei
37
+ ```