sql 查询数据表后 在统计某一列数据不重复的数量
SELECT [Store_Abbreviation] ,[Goodname], [Goodmodel] ,SUM(Order_Quantity) AS Order_Quantity, [Store_Abbreviation] ,
[Workid] FROM (select [Goods_Name],[Order_Quantity],[Delivery_Date],[Store_Abbreviation],[Goodmodel] ,[Workid],[Goodname] from
[fangtaidata].[dbo].[SalesStatistics] where [Delivery_Date] between '2014-07-02' and '2014-07-25' ) AS QT
GROUP BY [Store_Abbreviation],[Goodmodel],[Workid],[Goodname] 展开
1、创建测试表,
create table test_SalesStatistics (Goods_Name varchar2(200),Order_Quantity varchar2(200),
Delivery_Date varchar2(200),Store_Abbreviation varchar2(200),
Goodmodel varchar2(200),Workid varchar2(200),Goodname varchar2(200) );
2、插入测试数据,
insert into test_SalesStatistics
select 'name_' || (level / 4),
level / 4,
sysdate - level,
level / 10,
level / 100,
level / 8,
'goods_' || (level / 4)
from dual
connect by level < 10000;
3、创建所需临时表,
create table TEST_MID as
SELECT Store_Abbreviation,
Goodname,
Goodmodel,
SUM(Order_Quantity) AS Order_Quantity,
Workid
FROM (select Goods_Name,
Order_Quantity,
Delivery_Date,
Store_Abbreviation,
Goodmodel,
Workid,
Goodname
from test_SalesStatistics
where to_char(Delivery_Date) between '19980810' and '20000810'
)
GROUP BY Store_Abbreviation, Goodmodel, Workid, Goodname
4、统计每一列不重复的数据量,
select count(distinct Store_Abbreviation) Store_Abbreviation,
count(distinct Goodmodel) Goodmodel,
count(distinct Workid) Workid ,
count(distinct Goodname) Goodname
from TEST_MID t;
友善的提醒两点,如有帮助还望采纳,谢谢!
第一、你这个语句本身写的有点麻烦,简化如下
SELECT [Store_Abbreviation] ,[Goodname], [Goodmodel] ,SUM(Order_Quantity) AS Order_Quantity, [Workid]
from [fangtaidata].[dbo].[SalesStatistics]
where [Delivery_Date] between '2014-07-02' and '2014-07-25'
GROUP BY [Store_Abbreviation],[Goodmodel],[Workid],[Goodname]
第二、统计不重复的数量也很简单,再写个语句
--只取第一列
select count(distinct Store_Abbreviation)
from [fangtaidata].[dbo].[SalesStatistics]
where [Delivery_Date] between '2014-07-02' and '2014-07-25'
--想得到多列的数量
select count(distinct Store_Abbreviation) ,count(distinct Goodname) ,count(distinct Goodmodel) ,count(distinct Workid)
from [fangtaidata].[dbo].[SalesStatistics]
where [Delivery_Date] between '2014-07-02' and '2014-07-25'
码字不易,如有帮助,还望采纳,谢谢!
FROM (select [Goods_Name],[Order_Quantity],[Delivery_Date],[Store_Abbreviation],[Goodmodel] ,[Workid],[Goodname] from
[fangtaidata].[dbo].[SalesStatistics]
where [Delivery_Date] between '2014-07-02' and '2014-07-25'
) AS QT
GROUP BY [Store_Abbreviation]
having count(*)=1
;
显示第一列不重复的数据
你理解错误了 我的意思是 获取结果得到数据4 是统计第一列不相同的数据的条数