表CUSTOM
CustomNO CustomName PRICE
1111111 张三 李四 3000表MONEY
CustomNO Money
1111111 1000
1111111 2000
1111111 200表CustomDetials
CustomNO Sort Price
1111111 物品1 200
1111111 物品2 400如何得出结果为400
CustomNO CustomName PRICE
1111111 张三 李四 3000表MONEY
CustomNO Money
1111111 1000
1111111 2000
1111111 200表CustomDetials
CustomNO Sort Price
1111111 物品1 200
1111111 物品2 400如何得出结果为400
解决方案 »
- 如何更新重复的数据?
- sql while语句 嵌套 遍历 的问题
- 请问SQL中有没有函数可以判断某一天是第几周?
- 有多个sql数据库实例,那么连接串如何设呢?
- sp_KillSpid数据库后数据重连
- 如何在数据库中删除某一时间点之前的数据?
- 关于group by的sql语句,菜鸟问???
- 附加数据库的问题,变成E:\ASPNET\YOUPF\APP_DATA\MEDICINEDATA_DATA.MDF
- 这句复杂的SQL语句该怎么写?请SQL 高手帮忙,顺便讲一下JION的几个用法
- 求一句SQL
- sql 2008查版本号居然是05的
- [求助]如何利用CRecordSet类删除、修改excel里面的记录?
--表CUSTOM
/*CustomNO CustomName PRICE1111111 张三 李四 3000*/
Create Table CUSTOM
(
CustomNO nvarchar(10),
CustomName nvarchar(15),
PRICE decimal
)
insert into CUSTOM
select '1111111','张三 李四',3000
union all
select '1111112',' 王五',2000
/*
表MONEY
CustomNO Money
1111111 1000
1111111 2000
1111111 200*/
Create Table [MONEY]
(
CustomNO nvarchar(10),
[Money] decimal
)
insert into [MONEY]
select '1111111',1000
union all
select '1111111',2000
union all
select '1111111',200
union all
select '1111112',300
union all
select '1111112',400
/*表CustomDetials
CustomNO Sort Price
1111111 物品1 200
1111111 物品2 400 */
Create Table CustomDetials
(
CustomNO nvarchar(10),
Sort nvarchar(10),
Price decimal
)
insert into CustomDetials
select '1111111','物品1',200
union all
select '1111111','物品2',400
union all
select '1111112','物品1',100
union all
select '1111112','物品2',300
select (c.PRICE-m.sumMoney+cd.sumPrice) as Total from CUSTOM c,
(select m.CustomNO,sum(m.[Money]) sumMoney from [MONEY] m group by m.CustomNO) as m,
(select cd.CustomNO,sum(cd.Price) sumPrice from CustomDetials cd group by cd.CustomNO) as cd
where c.CustomNO=m.CustomNO
and c.CustomNO=cd.CustomNO
/*
Total
---------------------------------------
400
1700(2 行受影响)*/
create table [CUSTOM]
(CustomNO varchar(10), CustomName varchar(10), PRICE int)insert into CUSTOM
select '1111111', '张三 李四', 3000create table [MONEY]
(CustomNO varchar(10), [Money] int)insert into [MONEY]
select '1111111', 1000 union all
select '1111111', 2000 union all
select '1111111', 200create table [CustomDetials]
(CustomNO varchar(10), Sort varchar(10), Price int)insert into [CustomDetials]
select '1111111', '物品1', 200 union all
select '1111111', '物品2', 400
select a.CustomNO,
a.PRICE+b.Price-c.[Money] 'Price'
from
(select CustomNO,sum(PRICE) 'PRICE'
from [CUSTOM] group by CustomNO) a
left join
(select CustomNO,sum(Price) 'Price'
from [CustomDetials] group by CustomNO) b on a.CustomNO=b.CustomNO
left join
(select CustomNO,sum(Money) 'Money'
from [MONEY] group by CustomNO) c on a.CustomNO=c.CustomNO
where a.PRICE+b.Price-c.[Money]<>0 and a.PRICE+b.Price-c.[Money] is not null
/*
CustomNO Price
---------- -----------
1111111 400(1 row(s) affected)
*/
create table [CUSTOM]
(CustomNO varchar(10), CustomName varchar(10), PRICE int)insert into CUSTOM
select '1111111', '张三 李四', 3000create table [MONEY]
(CustomNO varchar(10), [Money] int)insert into [MONEY]
select '1111111', 1000 union all
select '1111111', 2000 union all
select '1111111', 200create table [CustomDetials]
(CustomNO varchar(10), Sort varchar(10), Price int)insert into [CustomDetials]
select '1111111', '物品1', 200 union all
select '1111111', '物品2', 400
select a.CustomNO,
a.PRICE+b.Price-c.[Money] 'Price'
from
(select CustomNO,sum(PRICE) 'PRICE'
from [CUSTOM] group by CustomNO) a
left join
(select CustomNO,sum(Price) 'Price'
from [CustomDetials] group by CustomNO) b on a.CustomNO=b.CustomNO
left join
(select CustomNO,sum(Money) 'Money'
from [MONEY] group by CustomNO) c on a.CustomNO=c.CustomNO
where a.PRICE+b.Price-c.[Money]<>0 and a.PRICE+b.Price-c.[Money] is not null
/*
CustomNO Price
---------- -----------
1111111 400(1 row(s) affected)
*/
这个也是正确的,要是再加一个列,怎么加呢。我怎么加不上?custom表中再加CustomName,Phone...怎么加到SQL里?
create table [CUSTOM]
(CustomNO varchar(10), CustomName varchar(10), PRICE int, Phone varchar(10))
insert into CUSTOM
select '1111111', '张三 李四', 3000, '123456789'create table [MONEY]
(CustomNO varchar(10), [Money] int)
insert into [MONEY]
select '1111111', 1000 union all
select '1111111', 2000 union all
select '1111111', 200
create table [CustomDetials]
(CustomNO varchar(10), Sort varchar(10), Price int)
insert into [CustomDetials]
select '1111111', '物品1', 200 union all
select '1111111', '物品2', 400
select a.CustomNO,
a.Phone,
a.PRICE+b.Price-c.[Money] 'Price'
from
(select d.CustomNO,
(select top 1 Phone
from CUSTOM e where e.CustomNO=d.CustomNO) 'Phone',
sum(d.PRICE) 'PRICE'
from [CUSTOM] d group by d.CustomNO) a
left join
(select CustomNO,sum(Price) 'Price'
from [CustomDetials] group by CustomNO) b on a.CustomNO=b.CustomNO
left join
(select CustomNO,sum(Money) 'Money'
from [MONEY] group by CustomNO) c on a.CustomNO=c.CustomNO
where a.PRICE+b.Price-c.[Money]<>0 and a.PRICE+b.Price-c.[Money] is not null/*
CustomNO Phone Price
---------- ---------- -----------
1111111 123456789 400(1 row(s) affected)
*/
怎么加到这个里面呢?
(c.PRICE-m.sumMoney+cd.sumPrice) as Total
select a.customno,baseprice+ISNULL(againprice,0)-ISNULL([money],0) as 欠款 from (select customno,SUM(price) as baseprice from custom group by customno) as a
left join (select customno,SUM(price) as againprice from customdetails group by customno) as b
on a.customno=b.customno
left join (select customno,SUM(money)as [money] from [money] group by customno) as c
on a.customno=c.customno这个只适合你的有了基本消费才能有附加消费的情况,如果不是这种的custom与custondetail要先full join才行