SQL查询适用于Microsoft Query,而不适用于Microsoft Excel

时间:2013-09-24 22:06:46

标签: sql excel

我有一个复杂的SQL查询,似乎无法正确转换为Microsoft Excel的数据连接。

此代码在Microsoft Query中粘贴时有效,但只要我将其返回到Excel,Excel就会放弃并且不会用数据填充工作表。 我认为当我们添加Left Outer Join部分以确保我们在两组SkitTpes中拥有最旧的SkitTypes时它停止了正常工作,但现在这件事情变得如此复杂,我并不完全确定。

left outer join Table2 b2 WITH (NOLOCK) on (a.ActingNumber = b2.ActingNumber and b.ActingNumber = b2.ActingNumber and b2.SkitType in ('180','184','185') ) left outer join Table2 b3 WITH (NOLOCK) on (a.ActingNumber = b3.ActingNumber and b.ActingNumber = b3.ActingNumber and b3.SkitType in ( '980','984','985') )

和完整查询:     将@ClipID声明为Varchar(15)     设置@ClipID ='总线'

Declare @Mnth as int
Declare @BgDate as varchar(8)
Declare @EndDate as varchar(8)
Declare @PrvMnth as int

set @Mnth = (select max(mnth) from Table1)
set @Mnth = (left(replace(convert(varchar(10),dateadd(mm, -5, convert(varchar(10),@Mnth,120) + '01'),120),'-',''),6))
set @BgDate =  convert(varchar(8), @Mnth, 120) + '01'
set @EndDate = replace(convert(varchar(10),dateadd(dd, -1, dateadd(mm, 6, convert(smalldatetime, @BgDate))),120),'-','')
set @PrvMnth = (left(replace(convert(varchar(10),dateadd(mm, -1, convert(varchar(10),@Mnth,120) + '01'),120),'-',''),6))

DECLARE @Mnth2 INT
DECLARE @Delq_Mnth CHAR(8)
DECLARE @l_MonthEndDate datetime

SET @Mnth2 = left(replace(convert(varchar(10),dateadd(mm, 4, convert(varchar(10), @Mnth,120) + '01'),120),'-',''),6)
SET @Delq_Mnth = (CONVERT(VARCHAR(8),DATEADD(d,-1,DATEADD(m,1,CONVERT(SMALLDATETIME,CONVERT(VARCHAR(6),@Mnth2) + '01'))),112))
SET @l_MonthEndDate = DATEADD(d,-1,DATEADD(m,1,CONVERT(SMALLDATETIME,CONVERT(VARCHAR(6),@Mnth2) + '01')))


select a.ActingNumber, 
max(a.ClipID) as ClipID, 
max(a.OutfitID) as OutfitID, 
max(b.SkitTrainDate) as SkitTrainDate, 
max(b.sctrn$) as SkitTrainHours,
max(a.CrowdControl) as Last_Mnth_CrowdControl
from Table1 a WITH (NOLOCK)
join Table2 b WITH (NOLOCK) on a.ActingNumber = b.ActingNumber
left outer join Table2 b2 WITH (NOLOCK) 
on (a.ActingNumber = b2.ActingNumber and b.ActingNumber = b2.ActingNumber and b2.SkitType in ('180','184','185') )
left outer join Table2 b3 WITH (NOLOCK) 
on (a.ActingNumber = b3.ActingNumber and b.ActingNumber = b3.ActingNumber and b3.SkitType in ( '980','984','985') )

join (
select *
FROM 
(
SELECT distinct A.ActingNumber,
CASE WHEN a.SkitPerfDate <> 0 THEN B.Prnbal ELSE a.prnbal END as prnbal , '' as ClipID ,'' as OutfitID ,
CASE WHEN c.UsePrebump = 1 THEN  
        CASE WHEN a.CrowdAge < 1 THEN 0
        WHEN a.CrowdAge BETWEEN 1 AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-1,@Delq_Mnth),@Delq_Mnth)) THEN 15
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-1,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-2,@Delq_Mnth),@Delq_Mnth)) THEN 30
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-2,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-3,@Delq_Mnth),@Delq_Mnth)) THEN 60
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-3,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-4,@Delq_Mnth),@Delq_Mnth)) THEN 90
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-4,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-5,@Delq_Mnth),@Delq_Mnth)) THEN 120
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-5,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-6,@Delq_Mnth),@Delq_Mnth)) THEN 150
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-6,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-7,@Delq_Mnth),@Delq_Mnth)) THEN 180
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-7,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-8,@Delq_Mnth),@Delq_Mnth)) THEN 210
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-8,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-9,@Delq_Mnth),@Delq_Mnth)) THEN 240
        WHEN a.CrowdAge BETWEEN CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-9,@Delq_Mnth),@Delq_Mnth)+1) AND CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-10,@Delq_Mnth),@Delq_Mnth)) THEN 270
        WHEN a.CrowdAge > CONVERT(VARCHAR(4),DATEDIFF(dd,DATEADD(MONTH,-10,@Delq_Mnth),@Delq_Mnth)) THEN 300
        ELSE 0
        END 
ELSE 
        ISNULL(a.PostBucket,0) 
END as CrowdControl,
A.SkitPerfDate,
a.Mnth,
A.Closcd
From Table3 a WITH (NOLOCK)
LEFT OUTER JOIN Table4 b WITH (NOLOCK) ON b.ActingNumber = a.ActingNumber
LEFT JOIN Table5 c WITH (NOLOCK) on c.ClipID = ClipID AND c.OutfitID = OutfitID
--left outer join Table3 a2
--on (a.ActingNumber = a2.ActingNumber and a.SAFLDT > a2.SAFLDT )
--left outer join Table3 a3
--on (a.ActingNumber = a3.ActingNumber and a.CrowdAge > a3.CrowdAge )
Where 
a.SAFLDT = 0 and a.CrowdAge = 0

--a3.ActingNumber is null 
--and a2.ActingNumber is null
--Mnth >= @Mnth 
and a.calcSTATUS = 'Completed'

) a
) h on a.ActingNumber = h.ActingNumber
where a.ClipID = @ClipID
-- Transaction codes:
and b.SkitType in ('180','184','185')--, '980','984','985')
and b.SkitTrainDate >= @BgDate 
--and b2.SkitType not in ( '980','984','985')
and b2.SkitType is not null 
and (b2.SkitTrainDate  > b3.SkitTrainDate or b3.SkitTrainDate is null )

group by a.ActingNumber
--order by Last_Mnth_CrowdControl desc

1 个答案:

答案 0 :(得分:0)

事实证明,使用现有查询并对其进行修改并不总是有效。 我必须先添加查询,进行修改,然后将该查询插入电子表格(使用现有数据连接)。