组合需要交替行的2个SQL查询

时间:2016-10-14 14:39:02

标签: sql ssis

寻找从两个单独的SQL查询将输出发送到平面文件的方法。他们来自同一张桌子。

查询1结果

ABC TransactionID Misc1 misc2

查询2结果

123名称info1 info2

请求同一文件中的结果

ABC TransactionID MISC1 MISC2
123 Name INFO1 INFO2
ABC TransactionID MISC1 MISC2
123 Name INFO1 INFO2
ABC TransactionID MISC1 MISC2
123 Name INFO1 INFO2
ABC TransactionID MISC1 MISC2
123 Name INFO1 INFO2

===========================================

关键是让第一行列以ABC开头,然后是第二行123,然后重复。

查询1

select

--TRANSACATION INFO FOR HT
'ABC' as 'DN1', --TRANSACTION SET ID
isnull(convert(char(2), c2.froimaintcode), space(2)) as 'D2', --MAINTENANCE TYPE CODE
isnull(left(convert(char(8), c2.froimaintdate, 112), 8), space(8)) as 'DN3c', ---MAINTENANCE TYPE CODE DATE 

--STATE AGENCY
left(c1.jurst, 2) as 'DN4',  --Jurisdiction Code
isnull(convert(char(25), c1.agencynumb), space(25)) as 'DN5', --Claim Number

--INSURER/CARRIER/SELF-INSURER
 case
  when ca.invmsg like 'CARRIER%' then isnull(left(uw.fedid, 9), space(9))
  else left(ph.fedid, 9)
 end 
 as 'DN6', --Carrier/Self-Insurer FEIN
space(129) as 'DN560', --FILLER
convert(char(15), 'CITYNAME') as 'DN12', --Claim Administrator City
convert(char(2), 'WI') as 'DN13', --Claim Administrator State
convert(char(9), '72548') as 'DN14', --Claim Administrator Zip
isnull(rtrim(convert(char(25), c.claimnum)), space(25)) as 'DN15', --Claim Administrator Claim Number

--EMPLOYER
isnull(rtrim(convert(char(9), ph.fedid)), space(9)) as 'DN16', --Employer FEIN
space(120) as 'DN570', --FILLER
isnull(rtrim(convert(char(15), e.city)), space(15)) as 'DN21', --Employer City
isnull(rtrim(convert(char(2), e.state)), space(2)) as 'DN22', --Employer State
isnull(rtrim(convert(char(9), e.zip)), space(9)) as 'DN23', --Employer Zip
space(1) as 'DN571', --FILLER
isnull(rtrim(convert(char(6), d1.code)), space(6)) as 'DN25', --SIC Code
space(10) as 'DN572', --FILLER
isnull(rtrim(convert(char(15), e.loccode)), space(15)) as 'DN27', --Insured Location Identifier
isnull(rtrim(convert(char(18), po.policyno)), space(18)) as 'DN28', --Policy Number
space(12) as 'DN573', --FILLER
isnull(left(convert(char(8), po.inceptdt, 112), 8), space(8)) as 'DN29', --Policy Effective Date
isnull(left(convert(char(8), po.expiratdt, 112), 8), space(8)) as 'DN30', --Policy Expiration Date
isnull(left(convert(char(8), c.doi, 112), 8), space(8)) as 'DN31', --Date of Injury 
isnull(left(convert(char(4), c.accdtime, 114), 4), space(4)) as 'DN32',  --Time of Injury
isnull(rtrim(convert(char(9), a.zip)), space(9)) as 'DN33', --Accident Zip
space(1) as 'DN574', --FILLER
isnull(convert(char(2), d3.code), space(2)) as 'DN35', --Nature of Injury Code
isnull(convert(char(2), d4.code), space(2)) as 'DN36', --Part of Body Code
isnull(convert(char(2), d5.code), space(2)) as 'DN37', --Cause of Injury Code
space(150) as 'DN575', --FILLER
isnull(rtrim(convert(char(2), c.inittreat)), space(2)) as 'DN39', --Initial Treatment
isnull(left(convert(char(8), c.reporteddt, 112), 8), space(8)) as 'DN40', --Date Reported to Employer
isnull(left(convert(char(8),  c.fnoirecvdt, 112), 8), space(8)) as 'DN41', --Date TPA Notified
space(39) as 'DN576', --FILLER
isnull(rtrim(convert(char(15), p.fname)), space(15)) as 'DN44', --Employee First Name
space(61) as 'DN577', --FILLER
isnull(rtrim(convert(char(15), p.city)), space(15)) as 'DN48', --Employee City
isnull(rtrim(convert(char(2), p.state)), space(2)) as 'DN49', --Employee State
isnull(rtrim(convert(char(9), p.zip)), space(9)) as 'DN50', --Employee Zip
space(10) as 'DN578', --FILLER
isnull(left(convert(char(8), p.dob, 112), 8), space(8)) as 'DN52', ---Employee DOB
case
  when p.sex = 1 then 'M'
  when p.sex = 2 then 'F'
  else 'U'
 end as 'DN53', --Employee Gender Text
 case
  when p.maritstat = '01' then 'S'
  when p.maritstat = '02' then 'M'
  when p.maritstat = '05' then 'P'
  when p.maritstat = '03' then 'K'
  when p.maritstat = '04' then 'D'
  when p.maritstat = '06' then 'W'
  when p.maritstat is null then 'K'
  else 'U'
 end 
 as 'DN54', --Employee Marital Status Text
isnull(rtrim(convert(char(2), c1.dependnumb)), space(2)) as 'DN55', --Employee Dependents
isnull(convert(char(8), c1.fdayofdis, 112), convert(char(8), c.doi, 112)) as 'DN56', --Date of Injury 
isnull(left(convert(char(8), c.dod, 112), 8), space(8)) as 'DN57', ---Employee Date of Death
case
  when c1.empstatus = '00514' then '02'
  else '01'
 end
 as 'DN58', --Employment Status Text
isnull(rtrim(convert(char(4), d2.code)), space(4)) as 'DN59', --Payroll Classification Code Class Code
space(30) as 'DN579', --FILLER
isnull(left(convert(char(8), c.hireddate, 112), 8), space(8)) as 'DN61', ---Employee Hire Date
isnull(rtrim(convert(char(11), c2.preinjwage)), space(11)) as 'DN62', --Pre Injury wage
'01' as 'DN63', --Wage Period Text
isnull(ltrim(convert(char(1), c.weekdays)), space(1)) as 'DN64', --Number of Days Worked Per Week
isnull(left(convert(char(8), c.lworkdate, 112), 8), space(8)) as 'DN65', --Date Last Worked 
 case
  when c.paidfordoi = 1 then 'Y'
  else 'N'
 end
 as 'DN66', --Full Wages Paid for DOI Indicator
space(1) as 'DN580', --FILLER
isnull(left(convert(char(8), c.rettowork, 112), 8), space(8)) as 'DN68' -- Date of Return of Work



from claim c
join claim1 c1 on c.claimno = c1.lnkclaimno
join claim2 c2 on c.claimno = c2.link2claim
join claim3 c3 on c.claimno = c3.link3claim
join person p on c.patient = p.personid
join employer e on c.empcode = e.code
join cmpolicy cm on c.claimno = cm.claimno
join policy po on cm.policyid = po.policyid
join carrier ca on c.carrier = ca.code
join masttype m on c.casetype = m.mastid
left join company uw on po.uwcompany = uw.code
left join [address] a on c.occurlocat = a.addrid
left join employer ph on e.polholder = ph.code
left join mmi1 mm on c.claimno = mm.claimno
left join dictdata d1 on ph.siccode = d1.datumid        --SIC Code
left join dictdata d2 on c2.claimncci = d2.datumid      --NCCI Class (Occ) Code
left join dictdata d3 on c1.injurnatur = d3.datumid     --Nature of Injury
left join dictdata d4 on c.bodypartid = d4.datumid      --Body Part
left join dictdata d5 on c1.injurcause = d5.datumid     --Cause of Injury
left join dictdata d6 on c1.empstatus = d6.datumid      --Employment Status
left join dictdata d7 on c2.losstype = d7.datumid       --Type Of Loss Code 
left join dictdata d8 on c2.mcotype = d8.datumid        --Managed Care Organization Code
left join dictdata d9 on c2.froimaintcode = d9.datumid  --Maintenance Type Code
left join authent au on c1.adjuster = au.shortname      --Adjuster
left join usermst u on au.authentid = u.personid        --Adjuster
left join authent au2 on c.username = au2.shortname     --User (Preparer)
left join usermst u2 on au2.authentid = u2.personid     --User (Preparer)
left join provider pv on c3.treatprovider = pv.fedidseq --Initial Treatment Provider
left join v_provaddr_master v on pv.fedidseq = v.fedidseq   --Initial Treatment Provider Address
left join cf_data cf1 on c.claimno = cf1.claimno and cf1.cf_id = '00024'    --Notice Date


--Date Function
WHERE froimaintdate >= dateadd(day,datediff(day,1,GETDATE()),0)
        AND froimaintdate < dateadd(day,datediff(day,0,GETDATE()),0)
        and c1.jurst = 'TX'

=============================================== =================================

查询2

select

'123' as 'DN1', --TRANSACTION SET ID
isnull(convert(char(2), c3.sroicorcod), space(2)) as 'DN295', --Maintenance Type correction Code
isnull(left(convert(char(8), c3.sroicordt, 112), 8), space(8)) as 'DN296', -- Maintenance Type Correction Code Date
space(8) as 'DN196' , --Denial Rescission Date
space(2) as 'DN186' , --Jurisdication Branch Office Code
isnull(rtrim(convert(char(25), c.claimnum)), space(25)) as 'DN15', --Claim Administrator Claim Number
'205233506' as 'DN187', --Claim Administrator FEIN
convert(char(40), 'ABCD') as 'DN188', --Claim Administrator Name
space(50) as 'DN135', --Claim Administrator Information/Attention Line
convert(char(40), '1 MAIN STREET') as 'DN10', --Claim Administrator Address (Primary)
convert(char(40), 'PO BOX 1') as 'DN11', --Claim Administrator Address (Secondary)
space(3) as 'DN136', --Claim Administrator Country Code
'S' as 'DN270', --Employee Id Type Qualifier
space(15) as 'DN271', --Employee ID
space(4) as 'DN255', --Employee Last name suffix
space(1) as 'DN150', --Employee Authorization to Release medical Records Indiator
space(1) as 'DN157', --Employee social security number release indicator
isnull(rtrim(convert(char(40), p.lname)), space(40)) as 'DN43', --Employee Last Name
isnull(rtrim(convert(char(15), p.mname)), space(15)) as 'DN45', --Employee Middle Name/Initial
isnull(rtrim(convert(char(40), p.addr1)), space(40)) as 'DN46', --Employee Address 1
isnull(rtrim(convert(char(40), p.addr2)), space(40)) as 'DN47', --Employee Address 2
space(3) as 'DN155', --Employee Mailing Country Code
convert(char(15),REPLACE(p.phone, '-', '')) as 'DN51', --Employee Phone
case
  when c3.deathresult = 1 then 'Y'
  else 'N'
 end
as 'DN146', --Death Result of Injury Code
isnull(convert(char(2), d7.code), '01') as 'DN290', --Type Of Loss Code
space(1) as 'DN228', --Return to Work With Same Employer Indictor
space(1) as 'DN189', --Return To Work Type Code
space(1) as 'DN224', --Physical Restrictions Indicator
isnull(rtrim(convert(char(9), ph.fedid)), space(9)) as 'DN314', --Insured FEIN
isnull (rtrim(convert(char(40), ph.name)), space(40)) as 'DN17', --Insured Name
space(1) as 'DN184', --Insured Type Code
space(25) as 'DN26', --Insured Report Number
space(1) as 'DN204', --Work Week Type Code
space(7) as 'DN205', --Work Days Scheduled Code
space(1) as 'DN229', --Injury Severity Type Code
case
  when ca.invmsg like 'CARRIER%' then isnull(convert(char(40), (uw.name)), space(40))
  else convert(char(40), (ph.name))
 end 
 as 'DN7', --Carrier/Self-Insurer Name
 space(1) as 'DN185', --Insured Type Code
 space(9) as 'DN292', --Insolvent Insurer FEIN
 space(9) as 'DN200', --Claim Administrator Alternate Postal Code
 space(15) as 'DN206', --Employee Security ID
 space(8) as 'DN350', --Filler
  case
  when c.onempprem = 1 then 'Y'
  when c.onempprem = 2 then 'N'
  else space(1)
 end
 as 'DN249', --Premise Code
isnull(rtrim(convert(char(20),a.county)), space(20)) as 'DN118', --Accident County
isnull(rtrim(convert(char(50),c2.acc_place)), space(50)) as 'DN119', --Accident Location Narrative
space(50) as 'DN120', --Accident Organization Name
isnull(rtrim(convert(char(15),a.city)), space(15)) as 'DN121', --Accident City
isnull(rtrim(convert(char(40),a.addr1)), space(40)) as 'DN122', --Accident Address
isnull(rtrim(convert(char(2),a.state)), space(2)) as 'DN123', --Accident State
isnull(rtrim(convert(char(3),a.county)), space(3)) as 'DN280', --Accident County Code
isnull(left(convert(char(8), c.lworkdate, 112), 8), space(8)) as 'D281', -- Date Employer Had Knowledge of Date of Disability
space(1) as 'DN75', --Agreement to Compensate code
isnull(rtrim(convert(char(40), e.name)), space(40)) as  'DN18', --Insured Name
space(15) as 'DN329', --Employer UI Number
isnull(rtrim(convert(char(40), e.addr1)), space(40)) as 'DN19', --Employer Address 1
isnull(rtrim(convert(char(40), e.addr2)), space(40)) as 'DN20', --Employer Address 2
space(3) as 'DN164', --Employer Physical Country Code
isnull(convert(char(15), e.entryphone), space(15)) as 'DN159', --Concurrent Employer Contact Phone Number
space(40) as 'DN160', --Employer Contact Name
space(15) as 'DN230', --Employer Id Assigned by jurisdiction
space(2) as 'DN231', --Manual Classification Sub-Code
space(73) as 'DN505', --FILLER
space(50) as 'DN163', --Employer Information/Attention Line
isnull(rtrim(convert(char(15), ph.city)), space(15)) as 'DN165', --Employer City
space(3) as 'DN166', --Employer County
isnull(rtrim(convert(char(9), ph.zip)), space(9)) as 'DN167', --Employer Zip
isnull(rtrim(convert(char(40), ph.addr1)), space(40)) as 'DN168', --Employer Address 1
isnull(rtrim(convert(char(40), ph.addr2)), space(40)) as 'DN169', --Employer Address 2
isnull(rtrim(convert(char(2), ph.state)), space(2)) as 'DN170', --Employer State
space(50) as 'DN515', --FILLER
case
  when c.occupation = '' then isnull(convert(char(50), d2.descrip), space(50))
  else convert(char(50), ltrim(c.occupation))
 end
as 'DN60', --Occupation Description
space(8) as 'DN199',  --Full Denial effective date
space(163) as 'DN530', --FILLER
convert(char(1), c.status) as 'DN73', --Claim Status Code
left(m.mastdesc, 1) as 'DN74', --Claim Type Code
space(2) as 'DN77', --Late Reason Code
space(1) as 'DN273', --Employer Paid Salary in Lieu of Compensation Indicator
space(105) as 'DN535', --FILLER - Future Defined Usage


--Variable Segment Counters
space(2) as 'DN274', --Number of Accident / Injury Description Narratives
space(2) as 'DN277', --Number of Full Denial Reason Codes
space(2) as 'DN276', --Number of Full Denial Reason Narratives
space(2) as 'DN278', --Number of Managed Care Organizations
space(2) as 'DN279' --Number of Witnesses


from claim c
join claim1 c1 on c.claimno = c1.lnkclaimno
join claim2 c2 on c.claimno = c2.link2claim
join claim3 c3 on c.claimno = c3.link3claim
join person p on c.patient = p.personid
join employer e on c.empcode = e.code
join cmpolicy cm on c.claimno = cm.claimno
join policy po on cm.policyid = po.policyid
join carrier ca on c.carrier = ca.code
join masttype m on c.casetype = m.mastid
left join company uw on po.uwcompany = uw.code
left join [address] a on c.occurlocat = a.addrid
left join employer ph on e.polholder = ph.code
left join mmi1 mm on c.claimno = mm.claimno
left join dictdata d1 on ph.siccode = d1.datumid        --SIC Code
left join dictdata d2 on c2.claimncci = d2.datumid      --NCCI Class (Occ) Code
left join dictdata d3 on c1.injurnatur = d3.datumid     --Nature of Injury
left join dictdata d4 on c.bodypartid = d4.datumid      --Body Part
left join dictdata d5 on c1.injurcause = d5.datumid     --Cause of Injury
left join dictdata d6 on c1.empstatus = d6.datumid      --Employment Status
left join dictdata d7 on c2.losstype = d7.datumid       --Type Of Loss Code 
left join dictdata d8 on c2.mcotype = d8.datumid        --Managed Care Organization Code
left join dictdata d9 on c2.froimaintcode = d9.datumid  --Maintenance Type Code
left join authent au on c1.adjuster = au.shortname      --Adjuster
left join usermst u on au.authentid = u.personid        --Adjuster
left join authent au2 on c.username = au2.shortname     --User (Preparer)
left join usermst u2 on au2.authentid = u2.personid     --User (Preparer)
left join provider pv on c3.treatprovider = pv.fedidseq --Initial Treatment Provider
left join v_provaddr_master v on pv.fedidseq = v.fedidseq   --Initial Treatment Provider Address
left join cf_data cf1 on c.claimno = cf1.claimno and cf1.cf_id = '00024'    --Notice Date


--Date Function
--Date Function
WHERE froimaintdate >= dateadd(day,datediff(day,1,GETDATE()),0)
        AND froimaintdate < dateadd(day,datediff(day,0,GETDATE()),0)
        and c1.jurst = 'TX'

1 个答案:

答案 0 :(得分:2)

这取决于您获取数据所需的顺序,但是您可以使用row_number提取两个查询来添加任意“排序”小数组件,以便您可以将单个查询排序为在源代码中需要,然后整体成为union中的交替行。

在纯SQL中,这种方法看起来像这样:

declare @a table (ID int, col1 nvarchar(50), col2 nvarchar(50), col3 nvarchar(50), col4 nvarchar(50));
declare @b table (ID int, col1 nvarchar(50), col2 nvarchar(50), col3 nvarchar(50), col4 nvarchar(50));

insert into @a values
 (1121,'ABC','TransactionID','MISC1','MISC2')
,(2532,'ABC','TransactionID','MISC1','MISC2')
,(7657,'ABC','TransactionID','MISC1','MISC2')
,(3455,'ABC','TransactionID','MISC1','MISC2');

insert into @b values
 (4653,'123','Name','INFO1','INFO2')
,(2424,'123','Name','INFO1','INFO2')
,(5466,'123','Name','INFO1','INFO2')
,(8666,'123','Name','INFO1','INFO2');


select row_number() over (order by ID) + 0.1 as rn
        ,ID
        ,col1
        ,col2
        ,col3
        ,col4
from @a

union all

select row_number() over (order by ID) + 0.2 as rn
        ,ID
        ,col1
        ,col2
        ,col3
        ,col4
from @b

order by rn

产生以下输出:

rn  ID      col1    col2            col3    col4
1.1 1121    ABC     TransactionID   MISC1   MISC2
1.2 2424    123     Name            INFO1   INFO2
2.1 2532    ABC     TransactionID   MISC1   MISC2
2.2 4653    123     Name            INFO1   INFO2
3.1 3455    ABC     TransactionID   MISC1   MISC2
3.2 5466    123     Name            INFO1   INFO2
4.1 7657    ABC     TransactionID   MISC1   MISC2
4.2 8666    123     Name            INFO1   INFO2