T-SQL简单查询

时间:2015-08-12 08:48:36

标签: sql sql-server-2008 tsql

1)如何仅查找具有正数和负数的记录,例如在下面的样本中,ABC的记录具有正数和负数。

Name   Tool   Security  QUANTITY    PRICE
  ABC      ML      XXX     100         50      
  ABC      DB      XXX    -50          50      
  XYZ      CS      YYY     30          30

2)如何总结正面和负面并制作一条记录,如

Name              Quantity
ABC               50
XYZ               30

3)平均非经理人员的工资。

ID  Employee    salary      ManagerID
1   ABC        300      3   
2   DEF        200      5
3   GHI        550      6
4   JKL        500      5
5   MNO        600      3
6   PQR        600      6
7   STU        550      NULL

2 个答案:

答案 0 :(得分:1)

1

select t1.* from table as t1
inner join
(
    select Name from table
    group by Name
    HAVING min(Quantity)<0 and max(Quantity)>0
) as t2 on t1.Name=t2.Name

其他问题很容易并付出一些努力

答案 1 :(得分:0)

ONE:使用GROUP BYHAVING

SELECT name, quantity
FROM yourtable
GROUP BY name
HAVING MIN(quantity) < 0 AND MAX(quantity) > 0

TWO:使用SUMGROUP BY

SELECT name, SUM(quantity)
FROM yourtable
GROUP BY name

三:使用AVG

SELECT AVG(Salary)
FROM yourtable
WHERE ManagerID IS NULL