有点难度的SQL查询语句,望版主也来看看,解决立马给分!

Jamesczh 2004-08-07 04:14:05
现有两个表product和sales:
product表中的字段如下:
product_style (款式)
product_barcode (产品条码)
status (产品状态:已售或未售)
sales表中的字段如下:
product_barcode (产品条码)
sales_time (销售时间)
sales_price (销售价格)

最终需要得到的结果集是这样的:
款式
合计件数
合计金额

说明:
1、两个表中用product_barcode作关联,在product表中product_barcode是唯一的,一个product_barcode表示一件衣服,合计件数由它来计算。而在sales表中,product_barcode不是唯一的,一个product_barcode可能存在多条记录,碰到多条的时侯,就取销售时间最晚的那条记录,来作为该件衣服的价格。
2、查询条件包含:
status='已售' and from_time<sales_time<to_time
3、一个款式包含多个barcode
...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zicxc 2004-08-07
  • 打赏
  • 举报
回复
说明呀?

select
a.product_style as 款式,
sum(1) as 合计件数, --这里可以写成count(*),应该一样的
sum(b.sales_price) as 合计金额
from product a,sales b
where a.status='已售'
and b.sales_time>@from_time and b.sales_time<@to_time
and a.product_barcode=b.product_barcode --到这里都应该不必说明的吧
and b.sales_time=(select max(sales_time) from sales where product_barcode=a.product_barcode) --这一句就是实现“碰到多条的时侯,就取销售时间最晚的那条记录,来作为该件衣服的价格。”
group by a.product_style --按款式汇总

Jamesczh 2004-08-07
  • 打赏
  • 举报
回复
非常感谢!!
忘了说明了from_time和to_time是参数。
TO:zicxc能否给你的SQL语句说明一下。再次谢谢,马上结贴!
zicxc 2004-08-07
  • 打赏
  • 举报
回复
更正:
select
a.product_style as 款式,
sum(1) as 合计件数,
sum(b.sales_price) as 合计金额
from product a,sales b
where a.status='已售'
--and from_time<sales_time<to_time --两个字段不明
--如果是参数,这么写 : and b.sales_time>@from_time and b.sales_time<@to_time
and a.product_barcode=b.product_barcode
and b.sales_time=(select max(sales_time) from sales where product_barcode=a.product_barcode)
group by a.product_style

zicxc 2004-08-07
  • 打赏
  • 举报
回复
大概吧:

select
a.product_style as 款式,
sum(1) as 合计件数,
sum(b.sales_price) as 合计金额
from product a,sales b
where a.status='已售'
--and from_time<sales_time<to_time --两个字段不明
--如果是参数,这么写 : and b.sales_time>@from_time and b.sales_time<@to_time
and a.product_barcode=b.product_barcode
and b.sales_time=(select max(sales_time) from sales where product_barcode=a.product_barcode)

zicxc 2004-08-07
  • 打赏
  • 举报
回复
from_time
to_time
???
没有这字段

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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