请教一个求和的问题

[复制链接]
查看11 | 回复9 | 2007-6-12 14:21:04 | 显示全部楼层 |阅读模式
现在用
select C_name,G_name,sum(C_money) from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' group by C_name,G_name
查询出来消费和是40
可是实际上用select C_name,G_name,C_money from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' order by G_name
查询出来实际的消费和是75
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
select C_name,G_name,sum(C_money)
from sales,customer

where C_name='飞碟' and

G_name='网易'and

C_time>= '2007-01-01 00:00:00' and

C_time <='2007-01-0123:59:59'
group by C_name,G_name
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
LZ把你想要的结果跟数据贴出来
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
我现在想要的效果是:
C_name G_nameS_money
客户名游戏名 消费金额的和
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
select a.C_name,a.G_name,sum(a.C_money) from(select * fromsales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' )a where C_name=a.C_name and G_name=a.G_name
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
select a.C_name,a.G_name,sum(a.C_money) from(select * from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' )a where C_name=a.C_name and G_name=a.G_name
不太明白 这里的a代表的意思 第一个from 前面的a.C_name,a.G_name这里的C_name是customer表中的G_name是sales表中的
给你看我两个表的表结构吧
create table customer
(C_name nvarchar(50),
C_num nvarchar(11))
create table sales
(ID nvarchar(20),
S_num nvarchar(11),
col01 nvarchar(50),
G_name nvarchar(20),
G_id nvarchar(50),
C_money int,
C_ip nvarchar(20),
C_time datetime)
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
select C_name,G_name,sum(C_money) from(select * from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' )a where C_name=a.C_name and G_name=a.G_name
a 是表别名就是select * from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' 这个结果表的别名..
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
可怜的人那...还没得到答案吗?
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
服务器: 消息 8118,级别 16,状态 1,行 1
列 'a.C_name' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
服务器: 消息 8118,级别 16,状态 1,行 1
列 'a.G_name' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
可是我如果加上grou by
select C_name,G_name,sum(C_money) from(select * from sales,customer where S_num=substring(C_num,1,11) and C_name='大森林' and G_name='金山' )a where C_name=a.C_name and G_name=a.G_name group by C_name,G_name
查询的结果 : 大森林
金山
40
回复

使用道具 举报

千问 | 2007-6-12 14:21:04 | 显示全部楼层
这样就与实际不符了
实际上消费金额综合应该为75
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行