复杂的SQL查询----一道面试题

Sunnydavid1 2005-10-19 11:31:54
表结构如下:
item(itemno(varchar(20)),in_price(float),out_price(float))----itemno:产品编号,in_price:采购价,out_price:销售价.
INOut_list(itemno (varchar(20)),flag(char(1),qty(int))---itemno:产品编号,flag:标志,其中flag=1表示入仓,flag=2表示出仓.qty:入仓或出仓数量.

1.用一条SQL列出 仓存;
2.用一条SQL列出 每个产品的库存金额;
3.用一条SQL列出 所有产品的库存金额.
...全文
452 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunnydavid1 2005-10-20
  • 打赏
  • 举报
回复
谢谢各位大侠的解答,这两个问题圆满解决,也得到了同一问题的不同方法和不同思维。。
谢谢!!!
YOHOYOHO 2005-10-20
  • 打赏
  • 举报
回复
好题...
Alang_79 2005-10-20
  • 打赏
  • 举报
回复
2 另一种方法
select a.itemno , sum(case a.flag when 1 then a.qty * b.in_price else -qty * b.in_price end)
from INOut_list a inner join item b on a.itemno = b.itemno
group by a.itemno

改正3
select sum(case a.flag when 1 then a.qty * b.in_price else -qty * b.in_price end)
from INOut_list a inner join item b on a.itemno = b.itemno
ShinnyZhang 2005-10-20
  • 打赏
  • 举报
回复
select a.itemno,(sum(a.qty) * (select in_price from item as c where a.itemno=c.itemno))
-(select (sum(b.qty )* (select out_price from item as d where a.itemno=d.itemno))
from inout_list as b where b.flag=2 and a.itemno=b.itemno
group by itemno) from inout_list a where a.flag='1' group by itemno第2个问题,第2个会,第一个就不用说了。
zxbyhcsdn 2005-10-20
  • 打赏
  • 举报
回复
热闹呀!!
bugchen888 2005-10-20
  • 打赏
  • 举报
回复
1.
SELECT itemno, 仓存 = SUM(CASE flag WHEN 1 THEN qty WHEN 2 THEN -qty END)
FROM [INOut_list]
GROUP BY itemno

2.
SELECT A.itemno, A.in_price * B.仓存
FROM item A,(SELECT itemno, 仓存 = SUM(CASE flag WHEN 1 THEN qty WHEN 2 THEN -qty END)
FROM [INOut_list]
GROUP BY itemno) B
WHERE A.itemno = B.itemno

3.
SELECT SUM(A.in_price * B.仓存)
FROM item A,(SELECT itemno, 仓存 = SUM(CASE flag WHEN 1 THEN qty WHEN 2 THEN -qty END)
FROM [INOut_list]
GROUP BY itemno) B
WHERE A.itemno = B.itemno
bugchen888 2005-10-20
  • 打赏
  • 举报
回复
SELECT A.订单编号 ,欠收款 = A.采购总金额 - ISNULL(B.收款总金额,0)
FROM 采购订单表 A LEFT OUTER JOIN
(SELECT 订单编号,SUM(收款金额) AS 收款总金额
FROM 收款记录表
GROUP BY 订单编号) B
ON A.订单编号=B.订单编号
Sunnydavid1 2005-10-20
  • 打赏
  • 举报
回复
还有一道SQL题,请大家解答:
有一张采购订单表(orders),表的结构如下:(订单编号,采购总金额)
一张收款记录表(money),表的结构如下:(订单编号,收款日期,收款金额);
用一条SQL语句求欠收款记录表。

Sunnydavid1 2005-10-20
  • 打赏
  • 举报
回复
TO:Alang_79(欲寄相思千点泪,流不到,楚江东。)
第三问还是不行,系统提示:
服务器: 消息 130,级别 15,状态 1,行 1
不能对包含聚合或子查询的表达式执行聚合函数。
skywdq 2005-10-20
  • 打赏
  • 举报
回复
哎,都是sql高手呀,有空到我贴子上面指点一下呀
http://community.csdn.net/Expert/topic/4337/4337895.xml?temp=.4944116
我要急死了,帮帮忙呀大家
Sunnydavid1 2005-10-19
  • 打赏
  • 举报
回复
TO:samfeng_2003(风云)
呵呵,风云兄,你谦虚了,现在我把第三题在你的基础上修改了一下,你看行不行?
Sunnydavid1 2005-10-19
  • 打赏
  • 举报
回复
3.用一条SQL列出 所有产品的库存金额
select sum(库存) as 总库存 from(
select 库存=(sum(qty)*(select in_price from item where itemno=b.itemno))
-((select sum(qty) from inout_list a where a.itemno=b.itemno and flag=2 group by itemno)*
(select in_price from item where itemno=b.itemno))
from inout_list b where flag=1 group by itemno)
as c
samfeng_2003 2005-10-19
  • 打赏
  • 举报
回复
哦!我其实对供产销这块不熟悉的,从来没有碰过这方面的数据库设计,哎!碰到会计类的问题就头大!:( 对了,别叫我大侠吧!这里的老大如同天上的浮云一样。他们听见了会扁我的哈!
Sunnydavid1 2005-10-19
  • 打赏
  • 举报
回复
TO:samfeng_2003(风云)
风云大侠,你的思路正确,但语法有误,改正语法:

1.用一条SQL列出 仓存;
select itemno,sum(qty)-(select sum(qty) from inout_list a where a.itemno=b.itemno and flag=2 group by itemno)
from inout_list b where flag=1 group by itemno

2.用一条SQL列出 每个产品的库存金额;
select itemno,(sum(qty)*(select in_price from item where itemno=b.itemno))
-((select sum(qty) from inout_list a where a.itemno=b.itemno and flag=0 group by itemno)*
(select in_price from item where itemno=b.itemno))
from inout_list b where flag=1 group by itemno

3.用一条SQL列出 所有产品的库存金额.执行不出来,应该是对第二问的再次求和就行了
samfeng_2003 2005-10-19
  • 打赏
  • 举报
回复
1.用一条SQL列出 仓存;
select itemno,sum(qty)-(select sum(qty) from inout_list a where a.itemno=b.itemno and flag=0 group by itemno)
from inout_list b where flag=1 group by itemno

2.用一条SQL列出 每个产品的库存金额;
select itemno,sum(qty)*(select in_price from item where itemno=b.itemno)
-(select sum(qty) from inout_list a where a.itemno=b.itemno and flag=0 group by itemno)*
(select out_price from item where itemno=b.itemno) )
from inout_list b where flag=1 group by itemno

3.用一条SQL列出 所有产品的库存金额.
select itemno,sum(库存) as 总库存 from
(
select itemno,库存=sum(qty)*(select in_price from item where itemno=b.itemno)
-(select sum(qty) from inout_list a where a.itemno=b.itemno and flag=0 group by itemno)*
(select out_price from item where itemno=b.itemno) )
from inout_list b where flag=1 group by itemno
) a
group by itemno
adandelion 2005-10-19
  • 打赏
  • 举报
回复
sc
Alang_79 2005-10-19
  • 打赏
  • 举报
回复
呵呵,3 错了。

这样
select sum((case flag when 1 then qty else -qty end) * (select in_price from item where itemno = INOut_list.itemno )
from INOut_list
Alang_79 2005-10-19
  • 打赏
  • 举报
回复
1 用一条SQL列出 仓存;
select itemno,sum(case flag when 1 then qty else -qty end)
from INOut_list
group by itemno

2 用一条SQL列出 每个产品的库存金额;
select itemno,sum(case flag when 1 then qty else -qty end) * (select in_price from item where itemno = INOut_list.itemno )
from INOut_list
group by itemno

3
select sum(sum(case flag when 1 then qty else -qty end) * (select in_price from item where itemno = INOut_list.itemno ))
from INOut_list

$扫地僧$ 2005-10-19
  • 打赏
  • 举报
回复
select itemno,sum(qty)-(select sum(qty) from inout_list a where a.itemno=b.itemno and flag=2 group by itemno)
from inout_list b where flag=1 group by itemno

''的确是会不一样

它要是有入没出呢??
select 1-NUll 等于 NUll 的情况
Sunnydavid1 2005-10-19
  • 打赏
  • 举报
回复
To:scmail81(freedom)
这样执行出来达不到预期结果,不过你的另一种方法值得探讨.
加载更多回复(2)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧