求句SQL

hzybc 2009-10-27 11:54:03
表中的数据:

RQ H Je1 Je2
2009-1-1 A 200 0
2009-1-18 A-1 200 56
2009-3-6 B 500 0
2009-3-14 B-1 500 60

想要得到
RQ H Je1 Je2
2009-1-1 A 200 56
2009-3-6 B 500 60
...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2009-10-27
  • 打赏
  • 举报
回复
晕 搞错
--小F-- 2009-10-27
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-27 11:57:20
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([RQ] datetime,[H] varchar(3),[Je1] int,[Je2] int)
insert [tb]
select '2009-1-1','A',200,0 union all
select '2009-1-18','A-1',200,56 union all
select '2009-3-6','B',500,0 union all
select '2009-3-14','B-1',500,60
--------------开始查询--------------------------
select * from tb t where not exists(select 1 from tb where substring(h,1,1)=substring(t.h,1,1) and Je2>t.Je2)
----------------结果----------------------------
/* RQ H Je1 Je2
----------------------- ---- ----------- -----------
2009-01-18 00:00:00.000 A-1 200 56
2009-03-14 00:00:00.000 B-1 500 60

(2 行受影响)

*/
水族杰纶 2009-10-27
  • 打赏
  • 举报
回复
--> Title  : Generating test data #T
--> Author : wufeng4552
--> Date : 2009-10-27
if object_id('tempdb.dbo.#T') is not null drop table #T
go
create table #T (RQ datetime,H varchar(3),Je1 int,Je2 int)
insert into #T
select '2009-1-1','A',200,0 union all
select '2009-1-18','A-1',200,56 union all
select '2009-3-6','B',500,0 union all
select '2009-3-14','B-1',500,60
select min(RQ)RQ,
min(H)H,
min(Je1)Je1,
max(Je2)je2
from #t
group by convert(varchar(7),rq,120)
/*
RQ H Je1 je2
----------------------- ---- ----------- -----------
2009-01-01 00:00:00.000 A 200 56
2009-03-06 00:00:00.000 B 500 60

*/
SQL77 2009-10-27
  • 打赏
  • 举报
回复
SELECT MIN(RQ)RQ,LEFT(H,1)H,JE1,SUM(JE2)JE2 FROM TB GROUP BY LEFT(H,1)H,JE1

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧