我想在SQL里建一个视图,有三张表 ,需要从这三张表里取数据
表1:check(checkid,customer_id,checkdate)表2:checkgoods(checkid,quantity,discountprice)表...
表1:check (checkid,customer_id,checkdate) 表2:checkgoods (checkid,quantity,discountprice) 表3:customer(customer_id,abbrev) 要求 做个视图dpyxshou(customer_id1,abbrev1,checkdate1,xiaoshoue)。其中 条件1:表1 checkid=表2checkid,条件2:表1customer_id=表3customer_id, 条件3:xiaoshoue等于quantity*discountprice ,同时要按照customer_id(店铺编号)checkdate(日期)分组求和。也许每个店铺名称、日期都会有很多条记录,最终形成结果如222那样的 还有很多的店铺 按照每个店铺不同日期分组求和 xiaoshoue。拜大神
只求当月的数据 要加上条件
year(getdate())=year([Check].CheckDate) and month(getdate())=month([Check].CheckDate) 展开
只求当月的数据 要加上条件
year(getdate())=year([Check].CheckDate) and month(getdate())=month([Check].CheckDate) 展开
展开全部
SELECT
--quantity,discountprice,abbrev, --这三个不需要么?
customer_id,checkdate,SUM(xiaoshoue)
FROM (
SELECT
A.checkid,customer_id,checkdate,
quantity,discountprice,
abbrev,
quantity*discountprice AS xiaoshoue
FROM
CHECK A
FULL JOIN CHECKGOODS ON A.CHECKID = B.CHECKID
FULL JOIN customer C ON A.customer_id= C.customer_id
) TAL
GROUP BY
--quantity,discountprice,abbrev,
customer_id,checkdate
因为没有具体的表环境,无法实测。
希望你能看懂上面的关键的代码,然后自个去学会相关关键词的意义,理解后再去调试。
更多追问追答
追问
哎呀 需要啊 那个 xiaoshoue=quantity*discountprice啊。。abbrev这个也是需要的啊。。 建的视图 要四个属性啊 customer_id 后面不就是abbrev么。。而且建立视图怎么着也得create view 吧。。不用么?
追答
那就把前面两个减号去处即可。
展开全部
create dpyxshou
as (
select check . customer_id customer_id1 , customer.abbrev abbrev1,check .checkdate checkdate1,quantity*discountprice xiaoshoue from
check inner join checkgoods on check .checkid=checkgoods .checkid
inner join customer on check .checkid=checkgoods .checkid
)
as (
select check . customer_id customer_id1 , customer.abbrev abbrev1,check .checkdate checkdate1,quantity*discountprice xiaoshoue from
check inner join checkgoods on check .checkid=checkgoods .checkid
inner join customer on check .checkid=checkgoods .checkid
)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select a.customer_id,c.abbrev,b.checkdate,sum(b.quantity*b.discountprice) as xiaoshoue
from check a,checkgoods b,customer c
where a.checkid=b.checkid and a.customer_id=c.customer_id
group by a.customer_id,c.abbrev,b.checkdate
from check a,checkgoods b,customer c
where a.checkid=b.checkid and a.customer_id=c.customer_id
group by a.customer_id,c.abbrev,b.checkdate
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询