我正在努力解决SQL Server中的查询问题。我有以下专栏:
会员编号;从属数量;提供者编号;服务日期
我连接上面的内容以创建一个唯一的ID,请参阅下面的摘录:
MbrNo DepNo PracticeNo ServiceDt UniqueProviderConsults
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 1 243264 2014-07-02 00:00:00.000 243264100001077141820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-02 00:00:00.000 243264100001077041820
100001077 0 243264 2014-07-07 00:00:00.000 243264100001077041825
100000838 1 243264 2014-07-09 00:00:00.000 243264100000838141827
100000838 5 243264 2014-07-14 00:00:00.000 243264100000838541832
100000838 3 243264 2014-07-17 00:00:00.000 243264100000838341835
100000838 0 243264 2014-07-17 00:00:00.000 243264100000838041835
100000838 5 243264 2014-07-18 00:00:00.000 243264100000838541836
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001077 0 243264 2014-07-14 00:00:00.000 243264100001077041832
100001480 1 243264 2014-07-17 00:00:00.000 243264100001480141835
我的唯一ID是数字(30)数据类型。然后,我想计算唯一ID出现的次数。使用count_big,我没有得到任何溢出警告,但它仍然没有给我正确的计数。我认为这是因为精度为30太高,因此在计数时会将其切断。还有另一种选择吗?不幸的是,上面的组件是定义唯一ID的最小组件数。我已经尝试记录我的唯一ID,但计数也不正确。
有人可以帮忙:)
代码:
ALTER TABLE [Claims Edited] ADD [UniqueProviderConsults] NUMERIC(30)
GO
UPDATE [Claims Edited] SET [UniqueProviderConsults] = CONCAT(CONVERT(DECIMAL(38,0),ProviderNo),CONVERT(DECIMAL(38,0),MbrNo),CONVERT(VARCHAR(MAX),DepNo),CONVERT(DECIMAL(38,0),ServiceDt))
GO
Select PracticeNo,
count_big(DISTINCT case when [ServiceMth]='2014-06-30' THEN [UniqueProviderConsults] else 0 end) as [Jun-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-07-31' THEN [UniqueProviderConsults] else 0 end) as [Jul-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-08-31' THEN [UniqueProviderConsults] else 0 end) as [Aug-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-09-30' THEN [UniqueProviderConsults] else 0 end) as [Sep-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-10-31' THEN [UniqueProviderConsults] else 0 end) as [Oct-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-11-30' THEN [UniqueProviderConsults] else 0 end) as [Nov-14 Consults],
count_big(DISTINCT case when [ServiceMth]='2014-12-31' THEN [UniqueProviderConsults] else 0 end) as [Dec-14 Consults],
Into [EM Consultation Count temp]
from [EM Claims Edited]
Group by PracticeNo
使用上面摘录中的数据
提供商编号243264 对于2014年6月,没有行,但我的代码计数为1。 对于2014年7月,有10个唯一ID,但我的代码为11。