下面一条语句能实现吗?谢谢storeid,plu,qty
店1,A001,10
店1,A002,20
店1,A003,30
店2,A001,11
店2,A002,21
店2,A003,31
店3,A001,3
店3,A002,4
店3,A003,5
.......要得到这样的数据storeid,plu,本店数量qty,其他店数量
店1,A001,10,(11+3=14)
店1,A002,20,(21+5=26)
店1,A001,10
店1,A002,20
店1,A003,30
店2,A001,11
店2,A002,21
店2,A003,31
店3,A001,3
店3,A002,4
店3,A003,5
.......要得到这样的数据storeid,plu,本店数量qty,其他店数量
店1,A001,10,(11+3=14)
店1,A002,20,(21+5=26)
解决方案 »
- 请问一个SQL语句统计对应多个记录,只选择其中一条记录的办法
- 个人认为极复杂的数据库交叉表,烦请各位帮忙解决下!急!
- IF 语句?
- 怎样同时update5个表中的相同字段?
- 一篇文章,分开主表,附加表效果好吗?
- 如何监视、查找用户对数据库操作日志(具体到表的数据操作)?
- [散分100]求一简单SQL语句
- 今天早上连接别人的SQL到本地上好好的!现在却说连接不上了!why
- 请问这种情况可以使用一条语句解决吗?
- 这个里面的connect都删不掉,现在我不知道怎么装mysql了,不知道该怎么办,求解
- 上千个照片按id号命了名,用什么tsql语句把它们一次性导入sql server?
- IN在这里的意思以及这样的JOIN链接为什么会错误呢?请指导...
from 表 t
group by storeid,plu
INSERT @t SELECT N'店1',N'A001',10
UNION ALL SELECT N'店1',N'A002',20
UNION ALL SELECT N'店1',N'A003',30
UNION ALL SELECT N'店2',N'A001',11
UNION ALL SELECT N'店2',N'A002',21
UNION ALL SELECT N'店2',N'A003',31
UNION ALL SELECT N'店3',N'A001',3
UNION ALL SELECT N'店3',N'A002',4
UNION ALL SELECT N'店3',N'A003',5
/************/
/*Test Data*/
/***fcuandy**/
/*2008-11-28*/
/************/
--sql2005
SELECT * FROM @t a
CROSS APPLY
(
SELECT sm = SUM(qty) FROM @t WHERE storeid!=a.storeid AND plu=a.plu
) b/*
店1 A001 10 14
店1 A002 20 25
店1 A003 30 36
店2 A001 11 13
店2 A002 21 24
店2 A003 31 35
店3 A001 3 21
店3 A002 4 41
店3 A003 5 61
*/
--sql2000
SELECT *,sm=(SELECT SUM(qty) FROM @t WHERE storeid!=a.storeid AND plu=a.plu)FROM @t a/*
店1 A001 10 14
店1 A002 20 25
店1 A003 30 36
店2 A001 11 13
店2 A002 21 24
店2 A003 31 35
店3 A001 3 21
店3 A002 4 41
店3 A003 5 61
*/
Set Nocount On
declare @1 table([storeid] nvarchar(2),[plu] nvarchar(4),[qty] int)
Insert @1
select N'店1',N'A001',10 union all
select N'店1',N'A002',20 union all
select N'店1',N'A003',30 union all
select N'店2',N'A001',11 union all
select N'店2',N'A002',21 union all
select N'店2',N'A003',31 union all
select N'店3',N'A001',3 union all
select N'店3',N'A002',4 union all
select N'店3',N'A003',5
Select a.storeid,a.plu ,a.qty As [本店数量],b.qty-a.qty As [其他店数量 ]
From @1 As a
Inner Join (Select plu,Sum(qty) As qty From @1 Group By plu) As b
On a.plu=b.plu
Order By a.storeid/*
storeid plu 本店数量 其他店数量
------- ---- ----------- -----------
店1 A001 10 14
店1 A002 20 25
店1 A003 30 36
店2 A003 31 35
店2 A001 11 13
店2 A002 21 24
店3 A001 3 21
店3 A002 4 41
店3 A003 5 61
*/
use practice
if Object_ID('dbo.product')is not null
drop table product;
go
create table product (
storeID char(3),
productID char(4),
numbers int
)
go
insert into product(storeID,productID,numbers)
select '店1','A001',10 union all
select '店1','A002',20 union all
select '店1','A003',30 union all
select '店2','A001',11 union all
select '店2','A002',21 union all
select '店2','A003',31 union all
select '店3','A001',3 union all
select '店3','A002',4 UNION ALL
select '店3','A003',5
go
select storeID, numbers as 本店数量,
其他店数量=(select sum(numbers) from product tt where tt.productID=t.productID and
tt.storeID<>t.storeID
group by productID)
from product t
order by storeID;
go
学习了一下