求助SQL高手
id pid gid money time
1 1 1 30.00 2012-04-07 21:25:06.810
2 2 1 80.00 2012-04-07 00:00:00.000
3 3 1 60.00 2012-04-07 00:00:00.000
4 4 1 120.00 2012-04-07 00:00:00.000
5 5 5 110.00 2012-04-07 00:00:00.000
6 6 6 200.00 2012-04-07 00:00:00.000
如何得到如下结果
id pid gid money time
1 1 1 290.00 2012-04-07 21:25:06.810
2 2 1 80.00 2012-04-07 00:00:00.000
3 3 1 60.00 2012-04-07 00:00:00.000
4 4 1 120.00 2012-04-07 00:00:00.000
5 5 5 110.00 2012-04-07 00:00:00.000
6 6 6 200.00 2012-04-07 00:00:00.000
------解决方案-------------------- 什么规则?gid求和家到底一条数据上?
------解决方案-------------------- 引用: 什么规则?gid求和家到底一条数据上?
gid等于pid的话,就把money相加放到pid的这一行上
------解决方案-------------------- 看不懂啊
------解决方案-------------------- 楼主试试这个,其中dbo.Table_1
建表语句
CREATE TABLE [dbo].[Table_1](
[id] [nchar](10) NULL,
[pid] [nchar](10) NULL,
[gid] [nchar](10) NULL,
[money] [int] NULL,
[sdatetime] [datetime] NULL
) ON [PRIMARY]
GO
查询语句:
SELECT MQ.ID
,MQ.PID
,MQ.GID
,SQ.NEWMONOEY
FROM dbo.Table_1 MQ
LEFT JOIN(
SELECT A.PID,Sum(CASE WHEN B.MONEY IS NULL THEN A.MONEY ELSE B.MONEY END) AS NEWMONOEY FROM dbo.Table_1 A
LEFT JOIN dbo.Table_1 B
ON A.PID = B.GID
GROUP BY A.PID) SQ
ON MQ.PID = SQ.PID
------解决方案--------------------
INSERT [dbo].[Table_1] ([id], [pid], [gid], [money], [sdatetime]) VALUES (N'1 ', N'1 ', N'1 ', 30, CAST(0x0000A032011CBBCC AS DateTime))
INSERT [dbo].[Table_1] ([id], [pid], [gid], [money], [sdatetime]) VALUES (N'2 ', N'2 ', N'1 ', 80, CAST(0x0000A032011CBBCC AS DateTime))
INSERT [dbo].[Table_1] ([id], [pid], [gid], [money], [sdatetime]) VALUES (N'3 ', N'3 ', N'1 ', 60, CAST(0x0000A032011CBBCC AS DateTime))
INSERT [dbo].[Table_1] ([id], [pid], [gid], [money], [sdatetime]) VALUES (N'4 ', N'4 ', N'1&nb