易截截图软件、单文件、免安装、纯绿色、仅160KB

求sql语句写法,谢谢。 - MS-SQL Server / 基础类

现有一个表:
  部门 项目 项目明细 资金方向  
  A T R 支出
  B Y E 支出
  A W Q 支出
  C L K 收入
  A M N 收入  

我想统计出部门个数,但要按“资金方向”,支出中A部门有重复,算一个,但收入中有A部门,这个要加1的。总共统计出来4个部门,就对了。请问SQL语句如何写?
SQL code:
select count(distinct 部门)
from [Table]
group by 部门,资金方向


group by 部门 ,资金方向

我试试。

SQL code:
--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([部门] [nvarchar](10),[项目] [nvarchar](10),[项目明细] [nvarchar](10),[资金方向] [nvarchar](10))
INSERT INTO [tb]
SELECT 'A','T','R','支出' UNION ALL
SELECT 'B','Y','E','支出' UNION ALL
SELECT 'A','W','Q','支出' UNION ALL
SELECT 'C','L','K','收入' UNION ALL
SELECT 'A','M','N','收入'

SELECT count(distinct 部门+资金方向) from [tb]

-->SQL查询如下:
/*
-----------
4

(1 行受影响)
*/


收入重复没有吗?



相关问答:

jsp链接sql2000的疑问?



type Exception report


message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jas ......

数据以xml格式返回 - MS-SQL Server / 应用实例

从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
   <folder state="unchecked&qu ......

sql 问题 - MS-SQL Server / 基础类

需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......

SQL 问题 - MS-SQL Server / 基础类

有2个数据表A与B,如何补充A中没有,B中有的数据,使A表数据完整?请写下具体源代码
SQL code:
insert A select * from A right join B on A.key=b.key where A.key is null


select * from B left join ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号