广州某公司笔试题(英文)-SQL

这篇博客介绍了如何使用MySQL创建一个名为'inoutlist'的库存进出记录表,并展示了如何插入数据。同时给出了如何通过SQL查询得到每个产品的入库、出库总量以及库存余额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Please use MySQL to Create DataBase like the language below

 

/*TabelName:inoutlist, Record the pass in and out of the storage*/

CREATE TABLE `inoutlist` (

  /*Auto increment*/

  `AutoID` int(11) NOT NULL auto_increment, 

  /*Time of pass in or out*/

  `RecordTime` datetime NOT NULL default '0000-00-00 00:00:00',

  /*ProductID*/

  `ProductID` int(11) default NULL,

  /*tagin or out*/

  `InOut` enum('out','in') default NULL,

  /*the number of pass in or out*/

  `Num` int(11) default NULL,

  /*Primary Key*/

  PRIMARY KEY  (`AutoID`)

);

 

/*insert data to table inoutlist*/

insert into InOutList values (0,'2007-12-3 11:39:25',1,'out',3);

insert into InOutList values (0,'2007-12-4 10:39:25',2,'out',2);

insert into InOutList values (0,'2007-12-5 11:39:25',1,'out',5);

insert into InOutList values (0,'2007-12-5 11:39:26',2,'out',9);

insert into InOutList values (0,'2007-12-5 11:39:27',3,'out',3);

insert into InOutList values (0,'2007-12-5 09:39:25',2,'in',10);

insert into InOutList values (0,'2007-12-5 13:39:56',3,'in',5);

insert into InOutList values (0,'2007-12-6  09:39:30',2,'in',6);

insert into InOutList values (0,'2007-12-6  09:39:31',2,'in',8);

insert into InOutList values (0,'2007-12-6  09:39:32',1,'in',100);

insert into InOutList values (0,'2007-12-6  09:39:33',2,'in',50);

insert into InOutList values (0,'2007-12-6  09:39:34',3,'in',60);

insert into InOutList values (0,'2007-12-6  09:39:35',4,'in',10);

 

/*TableName:product, products in storage*/

CREATE TABLE `product` (

  `ID` int(11) NOT NULL default '0',

  `Name` varchar(255) default NULL,

  PRIMARY KEY  (`ID`)

) ;

 

/*insert data to table product*/

insert into product values (1,'Product1');

insert into product values (2,'Product2');

insert into product values (3,'Product3');

insert into product values (4,'Product4');

insert into product values (5,'Product5');

 

 

 Application running as the form below

 

  Table:InOutList

┌───┬─────────┬─────┬───┬───┐

AutoIDRecordTime        ProductID InOut Num 

├───┼─────────┼─────┼───┼───┤

  1   2007-12-3 11:39:25    1       out   3  

├───┼─────────┼─────┼───┼───┤

  2   2007-12-4 10:39:25    2       out   2  

├───┼─────────┼─────┼───┼───┤

  3   2007-12-5 11:39:25    1       out   5  

├───┼─────────┼─────┼───┼───┤

  4   2007-12-5 11:39:26    2       out   9  

├───┼─────────┼─────┼───┼───┤

  5   2007-12-5 11:39:27    3       out   3  

├───┼─────────┼─────┼───┼───┤

  6   2007-12-5 09:39:25    2       in    10 

├───┼─────────┼─────┼───┼───┤

  7   2007-12-5 13:39:56    3       in    5  

├───┼─────────┼─────┼───┼───┤

  8   2007-12-6 09:39:30    2       in    6  

├───┼─────────┼─────┼───┼───┤

  9   2007-12-6 09:39:31    2       in    8  

├───┼─────────┼─────┼───┼───┤

  10  2007-12-6 09:39:32    1       in    100

├───┼─────────┼─────┼───┼───┤

  11  2007-12-6 09:39:33    2       in    50 

├───┼─────────┼─────┼───┼───┤

  12  2007-12-6 09:39:34    3       in    60 

├───┼─────────┼─────┼───┼───┤

  13  2007-12-6 09:39:35    4       in    10 

└───┴─────────┴─────┴───┴───┘

 

  Table:Product

┌───┬─────┐

ID   Name     

├───┼─────┤

  1   Product1 

├───┼─────┤

  2   Product2    

├───┼─────┤

  3   Product3   

├───┼─────┤

  4   │Product4    

├───┼─────┤

  5   │Product5   

└───┴─────┘

 

 Demand: write out the SQL sentence which will run as the form below without changing of the original table and data

┌───┬────┬───┬───┬───┐

  ID  │Name      In    Out │ Save │

├───┼────┼───┼───┼───┤

  1   │Product1│  100 │   8    92 

├───┼────┼───┼───┼───┤

  2   │Product2│  74    11    63 

├───┼────┼───┼───┼───┤

  3   │Product3│  65     3    62 

├───┼────┼───┼───┼───┤

  4   │Product4│  10     0    10 

├───┼────┼───┼───┼───┤

  5   │Product5│  0      0    0  

└───┴────┴───┴───┴───┘

 explanation:  ID   - ProductID

               Name - ProductName

               In   - accumulative total of the Product pass in storage

               Out  - accumulative total of the Product pass out storage

               Save - stocks of the product

 

answer:

select p.Id,

p.Name,

sum(case when io.inout='in' then io.num else 0 end) 'In',

sum(case when io.inout='out' then io.num else 0 end) 'Out',

case when sum(case when io.inout='in' then io.num else -io.num end)  is null then 0 else sum(case when io.inout='in' then io.num else -io.num end)  end 'Save'

from product p left join inoutlist io on p.id=io.productID group by p.id;

 

//出处:  http://blog.youkuaiyun.com/lenotang/archive/2008/08/26/2835270.aspx
 
case的用法:

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
END

SQL CASE 是一种通用的条件表达式,类似于其他语言里的 if/else 语句. CASE 子句可以用于任何表达式可以有效存在的地方. condition 是一个返回boolean 的表达式. 如果结果为真,那么 CASE 表达式的结果就是 result. 如果结果为假,那么以相同方式搜寻任何随后的 WHEN 子句. 如果没有 WHEN condition 为真,那么 case 表达式的结果就是在 ELSE 子句里的值. 如果省略了 ELSE 子句而且没有匹配的条件, 结果为 NULL.

例子:

=> SELECT * FROM test;
 a
---
 1
 2
 3

=> SELECT a,
		CASE 	WHEN a=1 THEN 'one'
			WHEN a=2 THEN 'two'
			ELSE 'other'
		END
	FROM test;
 a | case
---+-------
 1 | one
 2 | two
 3 | other

 

所有 result 表达式的数据的类型 都必须可以转换成单一的输出类型. 参阅 Section 7.5 获取细节.

CASE expression
    WHEN value THEN result
    [WHEN ...]
    [ELSE result]
END

这个"简单的" CASE 表达式是上面 的通用形式的一个特殊的变种. 先计算 expression 的值, 然后与所有在WHEN 子句里的 value 对比,直到找到一个相等的. 如果没有找到匹配的,则返回在 ELSE 子句里的 result (或者 NULL). 这个类似于 C 里的 switch 语句.

上面的例子可以用简单 CASE 语法来写:

=> SELECT a,
	CASE a	WHEN 1 THEN 'one'
		WHEN 2 THEN 'two'
		ELSE 'other'
	END
	FROM test;
 a | case
---+-------
 1 | one
 2 | two
 3 | other


//from http://docs.huihoo.com/postgresql/pgsql-doc-7.3/functions-conditional.html


答案:


select p.ID,p.Name,

sum(case when io.inout='in' then io.num else 0 end) 'In',

sum(case when io.inout='out' then io.num else 0 end) 'Out',

sum(case when io.inout='in' then io.num when io.inout='out' then -io.num else 0 end)  'Save'

from product p left join inoutlist io

on p.id=io.productID group by p.id;


 SAVE中有错:

select p.Id,
p.Name,
case when
(select sum(case when io1.inout='in' then io1.num else 0 end)
from inoutlist io1 where p.id=io1.productID ) is null then 0
else (select sum(case when io1.inout='in' then io1.num else 0 end)
from inoutlist io1 where p.id=io1.productID )
end
as 'In',
case when
(select sum(case when io2.inout='out' then io2.num else 0 end)
from inoutlist io2 where p.id=io2.productID )
is null then 0
else
(select sum(case when io2.inout='out' then io2.num else 0 end)
from inoutlist io2 where p.id=io2.productID )
end
as 'Out',
('In' - 'Out') as 'Save'
from product p ;

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值