sql查询以随机顺序为每个提供程序提取2个客户端

时间:2017-11-08 19:37:29

标签: sql reporting-services

有没有办法让sql查询以随机顺序为每个提供程序提取2个客户端?我的查询设置为提供不同的提供者列表。我还有一个单独的查询设置来拉随机2个客户端。我想组合这两个查询,以便在SSRS中创建报告。有没有办法使用2个数据集创建报告?或者我不能在没有合并2个查询的情况下继续前进?

查询以拉出不同的提供者

SELECT distinct provider_mstr.description 
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
full JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
where provider_mstr.delete_ind <> 'Y' 

查询以拉出2个随机客户端:

SELECT top 2 person.last_name + ', ' + person.first_name + ' ' + person.middle_name as 'Client', 
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
INNER JOIN service_item_mstr ON charges.service_item_lib_id = service_item_mstr.service_item_lib_id 
FULL OUTER JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
WHERE provider_mstr.delete_ind <> 'Y' and charges.amt > 0.01 and 
order by newid() 

3 个答案:

答案 0 :(得分:0)

如下所示,语法错误查询:

SELECT distinct provider_mstr.description as provider, null as Client
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
full JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
where provider_mstr.delete_ind <> 'Y' 

union all 

SELECT top 2 null as provider, person.last_name + ', ' + person.first_name + ' ' + person.middle_name as Client 
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
INNER JOIN service_item_mstr ON charges.service_item_lib_id = service_item_mstr.service_item_lib_id 
FULL OUTER JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
WHERE provider_mstr.delete_ind <> 'Y' and charges.amt > 0.01 and 
order by newid() 

这里的原始答案:

--<sql for query1>

UNION ALL

--<sql for query2>

两个查询中的列必须是相同的数字和类型。

您可能需要在此示例中使用null

 SELECT 1 as NUM1, 2 as NUM2, null as VC1, null as VC2
 FROM -- etc

 UNION ALL

 SELECT null as NUM1, null as NUM2, 'A' as VC1, 'B' as VC2
 FROM -- ect

答案 1 :(得分:0)

您可能需要提供更多详细信息。编辑您的问题以显示两个查询的输出和预期结果。

猜猜......

如果您对

等提供商有疑问
SELECT DISTINCT Provider from myProviders

然后返回

Provider
========
ProviderA
ProviderB
ProviderC

并且您的随机客户端查询类似于

SELECT ClientName from myCLients WHERE <<my criteria>>

并返回类似

的内容
ClientName
==========
ClientA
ClientB

...然后你可以做类似

的事情
SELECT * FROM (SELECT DISTINCT Provider from myProviders)
    CROSS JOIN (SELECT ClientName from myCLients WHERE <<my criteria>>)

会给你

Provider    ClientName
=========   ==========
ProviderA   ClientA
ProviderA   ClientB
ProviderB   ClientA
ProviderB   ClientB
ProviderC   ClientA
ProviderC   ClientB

答案 2 :(得分:0)

你可以做这样的事情

Dis_provider AS (
SELECT distinct provider_mstr.description 
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
full JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
where provider_mstr.delete_ind <> 'Y' )
Rando_Cient AS (
SELECT top 2 person.last_name + ', ' + person.first_name + ' ' + person.middle_name as 'Client', 
FROM person 
INNER JOIN charges ON person.person_id = charges.person_id 
INNER JOIN service_item_mstr ON charges.service_item_lib_id = service_item_mstr.service_item_lib_id 
FULL OUTER JOIN provider_mstr ON provider_mstr.provider_id = charges.rendering_id 
WHERE provider_mstr.delete_ind <> 'Y' and charges.amt > 0.01 
order by newid() )
Select 
provider_mstr.description,
Client
from Dis_Provider,Rando_Cient

如果您向我们提供更多信息,我希望如下:

  • 我们将要使用的表的概述
  • 所需的输出