zoukankan      html  css  js  c++  java
  • [SQL]一组数据中Name列相同值的最大Je与最小je的差

    declare @t table(name varchar(20),qy varchar(20),je int)
    insert into @t 
    select '产品一','北京',500
    union all select '产品一','上海',300
    union all select '产品二','北京',600
    union all select '产品三','上海',1000
    union all select '产品三','北京',8008
    union all select '产品四','上海',400
    --select * from @t a where not exists  --这是取表中的NAME相同的最大值
    --(
    --    select 1 from @t where name=a.name and je>a.je
    --)
    --第一个答案:
    SELECT NAME,QY,JE-ISNULL((SELECT JE FROM @T WHERE NAME=A.NAME AND QY<>A.QY),0)
    FROM @T A
    WHERE JE=(SELECT MAX(JE) FROM @T WHERE NAME=A.NAME)
    ORDER BY NAME
    --第二个答案
    Select 
          Name,
          qy,
          je-(Select 
                    Case When Min(je)=A.je Then 0 Else Min(je) End 
              From 
                    @t 
              Where Name=A.Name Group By Name) As je 
    From 
          @t A 
    Where Not Exists
         (Select 1 From @t Where Name=A.Name And je>A.je)
  • 相关阅读:
    SQL 拾遗
    PowerDesigner技巧
    进步
    'data.csv'
    System.Web”中不存在类型或命名空间名称script /找不到System.Web.Extensions.dll引用
    要学的技术
    Sql 表变量
    Tomcat 7.0的配置
    开发工具
    jQuery UI Dialog
  • 原文地址:https://www.cnblogs.com/beeone/p/3622289.html
Copyright © 2011-2022 走看看