日期:2014-05-18  浏览次数:20618 次

还是SQL查询问题
先上代码(我通过视图生成的)
SQL code

SELECT
      dbo.xsph.销售票号, dbo.xsd.客户全称, dbo.xsph.日期, 
      dbo.xs_Coustom.交货地点, dbo.xs_Coustom.计重方式, 
      dbo.xs_Coustom.运输方式, dbo.xs_Coustom.付款期限, 
      dbo.xs_Coustom.结算方式, dbo.xs_Coustom.备注, 
      dbo.xsph.金额, dbo.kh.地址, dbo.kh.邮政编码, 
      dbo.kh.电话, dbo.kh.开户银行, dbo.kh.银行帐号
FROM dbo.khmx INNER JOIN
      dbo.xsd INNER JOIN
      dbo.xsph INNER JOIN
      dbo.xs_Coustom ON dbo.xsph.销售票号 = dbo.xs_Coustom.销售票号 ON 
      dbo.xsd.票号 = dbo.xsph.销售票号 ON 
      dbo.khmx.票号 = dbo.xsph.销售票号 INNER JOIN
      dbo.kh ON dbo.khmx.客户全称 = dbo.kh.客户全称


结果:
2012-04-10xs0028 天泽装潢公司 2012-04-10 00:00:00.000 顺德 货柜车 1个月 NULL NULL 永乐码头交易 74000.0000 人民大路12号 130000 8978999 人民广场建行 134009211450  
2012-04-10xs0028 天泽装潢公司 2012-04-10 00:00:00.000 顺德 货柜车 1个月 NULL NULL 永乐码头交易 74000.0000 人民大路12号 130000 8978999 人民广场建行 134009211450  
2012-04-09xs0027 天泽装潢公司 2012-04-09 00:00:00.000 永乐码头 NULL NULL NULL NULL NULL 43000.0000 人民大路12号 130000 8978999 人民广场建行 134009211450  
2012-04-10xs0028 天泽装潢公司 2012-04-10 00:00:00.000 顺德 货柜车 1个月 NULL NULL 永乐码头交易 74000.0000 人民大路12号 130000 8978999 人民广场建行 134009211450  
2012-04-10xs0028 天泽装潢公司 2012-04-10 00:00:00.000 顺德 货柜车 1个月 NULL NULL 永乐码头交易 74000.0000 人民大路12号 130000 8978999 人民广场建行 134009211450  
2012-04-09xs0027 天泽装潢公司 2012-04-09 00:00:00.000 永乐码头 NULL NULL NULL NULL NULL 43000.0000 人民大路12号 130000 8978999 人民  

 
在以上结果中,有好多重复了,用group by提示"不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符" 字段是肯定有text,datetime之类的,不能的,求大神指点.....

------解决方案--------------------
select后面的详细字段dbo.xsph.销售票号, dbo.xsd.客户全称, dbo.xsph.日期, 
dbo.xs_Coustom.交货地点, dbo.xs_Coustom.计重方式, 
dbo.xs_Coustom.运输方式, dbo.xs_Coustom.付款期限, 
dbo.xs_Coustom.结算方式, dbo.xs_Coustom.备注, 
dbo.xsph.金额, dbo.kh.地址, dbo.kh.邮政编码, 
dbo.kh.电话, dbo.kh.开户银行, dbo.kh.银行帐号
有text、ntext 和 image 这三种类型?
要去重复,先试试select distinct
------解决方案--------------------
重复就是有一对多的情况。你仔细查看表的连接对应情况
------解决方案--------------------
一对多是主表里的一个键可能对应子表里的多个而不是一个,连接就会出现错误.
------解决方案--------------------
试试select distinct
------解决方案--------------------
SQL code

if object_id('test1') is not null
    drop table test1;
go

create table test1
(
    [name] text,
    price int
);
go

insert into test1
select '压力锅', 23 union all
select '平底锅', 45 union all
select '热水壶', 88;
go

--直接group by
select [name], sum(price) as total_price
from test1
group by [name]
/*
消息 306,级别 16,状态 2,第 3 行
不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。
*/

--解决办法
select cast([name] as nvarchar(max)) as [name], sum(price) as total_price
from test1
group by cast([name] as nvarchar(max)); 
/*
name    total_price
------- ------------
平底锅  45
热水壶  8