由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 求助,oracle/sql里怎么实现这个转化
相关主题
求助:一个SAS的小问题问个sql问题
请教proc sql给duplicate加flag
[Mysql] how to return NULL count in group by querySQL find distinct values in large table (转载)
请问SAS大牛一个关于proc sql joinSQL run a stored procedure by fetching from a cursor row by (转载)
SAS code求教问一个sas问题
SQL中啥时候用group by, 啥时候用self-join?问一个数据处理的R的问题
SAS Proc SQL count问题请教sas程序
SAS Code 求助,如何把在另一个dataset的id找出来请教这个do while 为什么只给出循环最里面的output(ndat=1,id=1, time= 1 to &niobs)
相关话题的讨论汇总
话题: cust话题: prod话题: max话题: 2015话题: select
进入Statistics版参与讨论
1 (共1页)
s***c
发帖数: 1664
1
比如一个表
cust date prod
1 1/1/2015 A
1 1/2/2015 B
1 1/3/2015 B
2 1/3/2015 C
2 1/4/2015 D
怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
记成1,否则0
cust A B C D
1 1 1 0 0
2 0 0 1 1
谢谢~
S******y
发帖数: 1123
2
you might want to use CASE statement
CASE is used to provide if-then-else type of logic to SQL
---
欢迎浏览Python/R/Hadoop实战速成课网页-
http://plus.google.com/+statsGuyMITBBS/about
t*****w
发帖数: 254
3
create table #cust(cust int, date datetime, prod char(1))
insert into #cust(cust , date , prod )
values( '1' , '1/1/2015' , 'A'), ('1' , '1/2/2015' , 'B'),
('1' , '1/3/2015' , 'B'),('2' , '1/3/2015' , 'C'),('2' , '1/4/
2015' , 'D')
drop table #cust2
select distinct cust, prod ,
A=case when prod ='A' then 1 else 0 end ,
B=case when prod ='B' then 1 else 0 end ,
C=case when prod ='C' then 1 else 0 end ,
D=case when prod ='D' then 1 else 0 end
into #cust2 from #cust
select cust, (cast(MAX(A) as CHAR(1))+ cast(MAX(B) as CHAR(1))+cast(MAX(C)
as CHAR(1))+cast(MAX(D) as CHAR(1)))ABCD
from #cust2 group by cust

【在 s***c 的大作中提到】
: 比如一个表
: cust date prod
: 1 1/1/2015 A
: 1 1/2/2015 B
: 1 1/3/2015 B
: 2 1/3/2015 C
: 2 1/4/2015 D
: 怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
: 记成1,否则0
: cust A B C D

s*****d
发帖数: 2667
4
MySql:
create table cust2 as
(select cust,
IF(prod='A',1,0) as A,
IF(prod='B',1,0) as B,
IF(prod ='C',1,0) as C,
IF(prod='D',1,0) as D
from cust);
select cust,max(A) as A,max(B) as B,max(C) as C,max(D) as D
from cust2
group by cust;

【在 s***c 的大作中提到】
: 比如一个表
: cust date prod
: 1 1/1/2015 A
: 1 1/2/2015 B
: 1 1/3/2015 B
: 2 1/3/2015 C
: 2 1/4/2015 D
: 怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
: 记成1,否则0
: cust A B C D

s***c
发帖数: 1664
5
谢了,我后来也这么写的,好在产品少。要是有一万个产品,这个code得多长啊

【在 s*****d 的大作中提到】
: MySql:
: create table cust2 as
: (select cust,
: IF(prod='A',1,0) as A,
: IF(prod='B',1,0) as B,
: IF(prod ='C',1,0) as C,
: IF(prod='D',1,0) as D
: from cust);
: select cust,max(A) as A,max(B) as B,max(C) as C,max(D) as D
: from cust2

s***c
发帖数: 1664
6
比如一个表
cust date prod
1 1/1/2015 A
1 1/2/2015 B
1 1/3/2015 B
2 1/3/2015 C
2 1/4/2015 D
怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
记成1,否则0
cust A B C D
1 1 1 0 0
2 0 0 1 1
谢谢~
S******y
发帖数: 1123
7
you might want to use CASE statement
CASE is used to provide if-then-else type of logic to SQL
---
欢迎浏览Python/R/Hadoop实战速成课网页-
http://plus.google.com/+statsGuyMITBBS/about
t*****w
发帖数: 254
8
create table #cust(cust int, date datetime, prod char(1))
insert into #cust(cust , date , prod )
values( '1' , '1/1/2015' , 'A'), ('1' , '1/2/2015' , 'B'),
('1' , '1/3/2015' , 'B'),('2' , '1/3/2015' , 'C'),('2' , '1/4/
2015' , 'D')
drop table #cust2
select distinct cust, prod ,
A=case when prod ='A' then 1 else 0 end ,
B=case when prod ='B' then 1 else 0 end ,
C=case when prod ='C' then 1 else 0 end ,
D=case when prod ='D' then 1 else 0 end
into #cust2 from #cust
select cust, (cast(MAX(A) as CHAR(1))+ cast(MAX(B) as CHAR(1))+cast(MAX(C)
as CHAR(1))+cast(MAX(D) as CHAR(1)))ABCD
from #cust2 group by cust

【在 s***c 的大作中提到】
: 比如一个表
: cust date prod
: 1 1/1/2015 A
: 1 1/2/2015 B
: 1 1/3/2015 B
: 2 1/3/2015 C
: 2 1/4/2015 D
: 怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
: 记成1,否则0
: cust A B C D

s*****d
发帖数: 2667
9
MySql:
create table cust2 as
(select cust,
IF(prod='A',1,0) as A,
IF(prod='B',1,0) as B,
IF(prod ='C',1,0) as C,
IF(prod='D',1,0) as D
from cust);
select cust,max(A) as A,max(B) as B,max(C) as C,max(D) as D
from cust2
group by cust;

【在 s***c 的大作中提到】
: 比如一个表
: cust date prod
: 1 1/1/2015 A
: 1 1/2/2015 B
: 1 1/3/2015 B
: 2 1/3/2015 C
: 2 1/4/2015 D
: 怎么变成下面这个表,不管customer什么时候买过某个产品,买过几次,只要买过,就
: 记成1,否则0
: cust A B C D

s***c
发帖数: 1664
10
谢了,我后来也这么写的,好在产品少。要是有一万个产品,这个code得多长啊

【在 s*****d 的大作中提到】
: MySql:
: create table cust2 as
: (select cust,
: IF(prod='A',1,0) as A,
: IF(prod='B',1,0) as B,
: IF(prod ='C',1,0) as C,
: IF(prod='D',1,0) as D
: from cust);
: select cust,max(A) as A,max(B) as B,max(C) as C,max(D) as D
: from cust2

g**********y
发帖数: 423
11
如果有一万个的话,可以这么写:
SELECT CONCAT(
'CREATE TABLE CUST3 AS',
'(SELECT CUST,',
GROUP_CONCAT(REPLACE(' IF(PROD="PROD_NAME",1,0) AS PROD_NAME', 'PROD_NAME',
T.PROD) SEPARATOR ','),
' FROM CUST);'
)
INTO @CREATE_SQL FROM (SELECT DISTINCT PROD FROM CUST) AS T;
PREPARE STMT FROM @CREATE_SQL;
EXECUTE STMT;
SELECT CONCAT(
'SELECT CUST,',
GROUP_CONCAT(REPLACE(' MAX(PROD_NAME) AS PROD_NAME', 'PROD_NAME', T.PROD)
SEPARATOR ','),
' FROM CUST3',
' GROUP BY CUST;'
)
INTO @SELECT_SQL FROM (SELECT DISTINCT PROD FROM CUST) AS T;
PREPARE STMT FROM @SELECT_SQL;
EXECUTE STMT;
s***c
发帖数: 1664
12
谢谢楼上,收藏了
1 (共1页)
进入Statistics版参与讨论
相关主题
请教这个do while 为什么只给出循环最里面的output(ndat=1,id=1, time= 1 to &niobs)SAS code求教
SAS model 一个简单的问题。SQL中啥时候用group by, 啥时候用self-join?
SAS问题请教。谢谢。SAS Proc SQL count问题
How can I do this in R?SAS Code 求助,如何把在另一个dataset的id找出来
求助:一个SAS的小问题问个sql问题
请教proc sql给duplicate加flag
[Mysql] how to return NULL count in group by querySQL find distinct values in large table (转载)
请问SAS大牛一个关于proc sql joinSQL run a stored procedure by fetching from a cursor row by (转载)
相关话题的讨论汇总
话题: cust话题: prod话题: max话题: 2015话题: select