抱歉,我是新手,所以我不确定我是否正确格式化了这一切。我有下表:
DROP TABLE mytable;
CREATE TABLE mytable
( employee_id INTEGER(3) NOT NULL PRIMARY KEY,
vendor VARCHAR(1) ,
lineofbiz VARCHAR(1),
state VARCHAR(2) ,
company_name VARCHAR(8),
company_id INTEGER(1)
);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (123,'A','D','CA','Peasants',1);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (123,'A','M','CA','Peasants',1);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (123,'B','V','CA','Peasants',1);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (234,'C','V','CA','Plebians',2);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (234,'C','M','CA','Plebians',2);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (234,'C','D','CA','Plebians',2);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (345,'C','M','CA','Plebians',2);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (345,'C','D','CA','Plebians',2);
INSERT INTO mytable(employee_id,vendor,lineofbiz,state,company_name,company_id)
VALUES (345,'C','V','CA','Plebians',2);
我觉得我有一点小小的把握,但我正在撞墙。
1)在上表中,我想插入一个新列,该列具有一个标志,其中employee_id被标记为在大公司或小公司中。我会在下面写下我的错误查询,所以也许它会帮助描述这个问题。
select case when (count (distinct employee_id) > 1)
then as 'Large' else 'Small' from mytable;
我想我想插入一个带有该验证的新列,但我不知道该怎么做。
如果我违反协议,请告诉我!
答案 0 :(得分:0)
如果你想确定公司是大还是小,取决于员工的数量,我认为这是你试图写的查询。
SELECT company_id,
CASE
WHEN COUNT(DISTINCT employee_id) > 1 THEN 'large'
ELSE 'small'
END as company_size
FROM mytable
GROUP BY company_id;