我正在尝试运行以下查询,它返回时没有数据。但是,如果我在不使用where子句的情况下运行它,它会给我数据。但我需要包括那里的条件。有些变量有多个值,因此使用' In'
DECLARE @start as DATETIME, @end as DATETIME,
@Effective_dt as datetime, @Expiry_dt as datetime,
@Strategic_Account as varchar (Max),
@BIC_PILOT as varchar(max),
@BIC as varchar(max),
@Active as varchar (max)
SET @start = '8/1/2016'
SET @end = '8/31/2016'
select *
from
(
Select
B.Strategic_Account, b.expiry_dt,
isnull(b.bic_pilot,'No') as BIC_PILOT,
A.*, B.TIN, B.BIC,
Case
when B.Phase = 'Beta'
then 'Phase 1'
else B.Phase
end as Phase,
b.region
from
(SELECT
CASE
WHEN Site IN ('Albuquerque', 'CSMiami', 'Detroit', 'Houston', 'Kingsport', 'Phoenix', 'Pittsburgh', 'Ridgeland', 'CSHonolulu')
THEN 'C&S'
ELSE Site
END AS 'Line of Business',
CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month,
Site,
skillnumber, VCCNode, calldate,
IVRCallerType,
SUM(1) AS Completed_Surveys,
Question,
SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes,
SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No,
ProviderTaxIDAgencyTaxID, SurveyID
FROM
(/* Unpivot the table.*/
SELECT
Question, Response, Site, Bus_segment_type,
calldate, skillnumber, VCCNode, IVRCallerType,
ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT
Q7, site, Bus_segment_type, CallDate,
SkillNumber, VCCNode, IVRCallerType,
ProviderTaxIDAgencyTaxID, SurveyID
FROM
SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Chaska Node 2', 'Plymouth Node 1', 'Chaska Node 14', 'Chaska 14') OR VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
AND (Site IN('Albuquerque', 'CSMiami', 'Detroit', 'Houston', 'Kingsport', 'Phoenix', 'Pitts5urgh', 'Ridgeland', 'CSHonolulu')
AND skillnumber IN ('728','729','730','731','732','1369','1371','1376','1380','1396','309', '311','321', '323', '326', '765', '766', '767', '768', '769','169','204','480','481','483','484','486','487','488','489','490','491','719','364', '1694','1695','1696','1697','1698','1699','1700','1701','1702','1703','1704','1705','1706','1707','1708','1709','1710','1711','1712','1713','1714','1715','1716','1717','1718','1719','1720','1721','1722','1723','1724','1725','1726','1727'))
AND SurveyID NOT IN (SELECT SurveyID
FROM SRVY_InsightData_Excpt)
--where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end) pvt
UNPIVOT
(Response FOR Question IN (Q7)) AS unpvt) AS Data
GROUP BY
Question, Site, bus_segment_type, CallDate,
skillnumber, VCCNode, IVRCallerType, ProviderTaxIDAgencyTaxID,
SurveyID
UNION ALL
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/* 4/12/2016 removed skills '220','222', '523', '524', '525','526', '538', '539', '540', '548',
add in skills '439','440' eff 4/1/2016 */
--DECLARE @start as DATETIME, @end as DATETIME
--SET @start = '5/1/2015'
--SET @end = '5/31/2015'
SELECT CASE WHEN site IN ('Oldsmar', 'Huntsville', 'Moline PHS', 'Manila', 'MnR_Manila') THEN 'M&R'
ELSE Site END AS 'Line of Business'
, CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month
, Site
, skillnumber
, VCCNode
, calldate
, IVRCallerType
, SUM(1) AS Completed_Surveys
, Question
, SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes
, SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No
,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(/* Unpivot the table.*/
SELECT Question, Response, Site, Bus_segment_type, calldate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT Q7, site, Bus_segment_type, CallDate, SkillNumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Plymouth Node 3','Chaska Node 14', 'Chaska 14')or VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
and (Site in ('Oldsmar', 'Moline', 'Moline PHS', 'Huntsville') and SkillNumber in ('407','408','409','410','411','412','413','414', '439','440'))
and SurveyID NOT IN (SELECT SurveyID from SRVY_InsightData_Excpt) --where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end
) pvt UNPIVOT
(Response FOR
Question IN (Q7)) AS unpvt) AS Data
GROUP BY Question, Site, bus_segment_type, CallDate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
UNION ALL
/************************************************** Employer and Individual ******************************************************************/
/************************************************** Employer and Individual ******************************************************************/
/************************************************** Employer and Individual ******************************************************************/
/* removed '1180', '1209', '834', '809','718', ,'1074' '1392','1393',
added in '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'
*/
SELECT CASE
WHEN Site In ('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'Int_Frederick', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu') THEN 'E&I'
ELSE Site END AS 'Line of Business'
, CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month
, Site
, skillnumber
, VCCNode
, calldate
, IVRCallerType
, SUM(1) AS Completed_Surveys
, Question
, SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes
, SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No
,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(/* Unpivot the table.*/
SELECT Question, Response, Site, Bus_segment_type, calldate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT Q7, site, Bus_segment_type, CallDate, SkillNumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Eagan Node 7','Plymouth Node 1', 'Eagan Node 5') or VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
AND (((Site IN('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'Int_Frederick', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu', 'PHS_Manila')
AND skillnumber IN ('718','735','736','1689','1690', '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'))
and CallDate < '2/1/2015')
or ((Site IN('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu', 'PHS_Manila')
AND skillnumber IN ('718','735','736','1689','1690', '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'))
and CallDate >= '2/1/2015'))-- adding code to remove INT_Frederick after 2/1/2015 - FLH
AND SkillNumber not IN ('777', '215', '217','235','249')
and SurveyID NOT IN (SELECT SurveyID from SRVY_InsightData_Excpt) --where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end
) pvt UNPIVOT
(Response FOR
Question IN (Q7)) AS unpvt) AS Data
GROUP BY Question, Site, bus_segment_type, CallDate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID, SurveyID
) A left join D_PPSM_TINs B on A.ProviderTaxIDAgencyTaxID=B.TIN
) final
where
[BIC_PILOT] in (@BIC_PILOT)
and
Case when [Strategic_Account] is Null then 'Null' else [Strategic_Account] end in (@Strategic_Account)
and
Case when BIC is Null then 'No' else 'Yes' end in (@BIC)
and
case when Expiry_dt = convert(varchar(10),'12/31/9999',121) then 'Active' else 'Inactive' end in (@Active)
这是我没有Where子句的结果: -
答案 0 :(得分:0)
尝试将IN
替换为CHARINDEX
,它会检查文本是否存在于逗号分隔字符串中。
Select B.Strategic_Account,b.expiry_dt,
isnull(b.bic_pilot,'No') as BIC_PILOT,
A.*, B.TIN,B.BIC,Case when B.Phase ='Beta' then 'Phase 1' else B.Phase end as Phase,
b.region
from (
SELECT CASE
WHEN Site IN ('Albuquerque', 'CSMiami', 'Detroit', 'Houston', 'Kingsport', 'Phoenix', 'Pittsburgh', 'Ridgeland', 'CSHonolulu') THEN 'C&S'
ELSE Site END AS 'Line of Business'
, CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month
, Site
, skillnumber
, VCCNode
, calldate
, IVRCallerType
, SUM(1) AS Completed_Surveys
, Question
, SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes
, SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No
,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(/* Unpivot the table.*/
SELECT Question, Response, Site, Bus_segment_type, calldate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT Q7, site, Bus_segment_type, CallDate, SkillNumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Chaska Node 2', 'Plymouth Node 1','Chaska Node 14','Chaska 14')or VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
AND (Site IN('Albuquerque', 'CSMiami', 'Detroit', 'Houston', 'Kingsport', 'Phoenix', 'Pitts5urgh', 'Ridgeland', 'CSHonolulu')
AND skillnumber IN ('728','729','730','731','732','1369','1371','1376','1380','1396','309', '311','321', '323', '326', '765', '766', '767', '768', '769','169','204','480','481','483','484','486','487','488','489','490','491','719','364',
'1694','1695','1696','1697','1698','1699','1700','1701','1702','1703','1704','1705','1706','1707','1708','1709','1710','1711','1712','1713','1714','1715','1716','1717','1718','1719','1720','1721','1722','1723','1724','1725','1726','1727'))
and SurveyID NOT IN (SELECT SurveyID from SRVY_InsightData_Excpt) --where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end
) pvt UNPIVOT
(Response FOR
Question IN (Q7)) AS unpvt) AS Data
GROUP BY Question, Site, bus_segment_type, CallDate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
UNION ALL
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/************************************************** MEDICARE AND RETIREMENT ******************************************************************/
/* 4/12/2016 removed skills '220','222', '523', '524', '525','526', '538', '539', '540', '548',
add in skills '439','440' eff 4/1/2016 */
--DECLARE @start as DATETIME, @end as DATETIME
--SET @start = '5/1/2015'
--SET @end = '5/31/2015'
SELECT CASE WHEN site IN ('Oldsmar', 'Huntsville', 'Moline PHS', 'Manila', 'MnR_Manila') THEN 'M&R'
ELSE Site END AS 'Line of Business'
, CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month
, Site
, skillnumber
, VCCNode
, calldate
, IVRCallerType
, SUM(1) AS Completed_Surveys
, Question
, SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes
, SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No
,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(/* Unpivot the table.*/
SELECT Question, Response, Site, Bus_segment_type, calldate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT Q7, site, Bus_segment_type, CallDate, SkillNumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Plymouth Node 3','Chaska Node 14', 'Chaska 14')or VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
and (Site in ('Oldsmar', 'Moline', 'Moline PHS', 'Huntsville') and SkillNumber in ('407','408','409','410','411','412','413','414', '439','440'))
and SurveyID NOT IN (SELECT SurveyID from SRVY_InsightData_Excpt) --where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end
) pvt UNPIVOT
(Response FOR
Question IN (Q7)) AS unpvt) AS Data
GROUP BY Question, Site, bus_segment_type, CallDate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
UNION ALL
/************************************************** Employer and Individual ******************************************************************/
/************************************************** Employer and Individual ******************************************************************/
/************************************************** Employer and Individual ******************************************************************/
/* removed '1180', '1209', '834', '809','718', ,'1074' '1392','1393',
added in '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'
*/
SELECT CASE
WHEN Site In ('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'Int_Frederick', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu') THEN 'E&I'
ELSE Site END AS 'Line of Business'
, CAST((RTRIM(LTRIM(CAST(MONTH(CALLDATE) AS CHAR(2)))) + '/1/' + CAST(YEAR(CALLDATE) AS CHAR(4))) AS DATETIME) AS Month
, Site
, skillnumber
, VCCNode
, calldate
, IVRCallerType
, SUM(1) AS Completed_Surveys
, Question
, SUM(CASE WHEN Response = 1 THEN 1 ELSE 0 END) AS Yes
, SUM(CASE WHEN Response = 2 THEN 1 ELSE 0 END) AS No
,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(/* Unpivot the table.*/
SELECT Question, Response, Site, Bus_segment_type, calldate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM
(SELECT Q7, site, Bus_segment_type, CallDate, SkillNumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID , SurveyID
FROM SRVY_InsightData
WHERE
--IVRCallerType IN (@ict)
IVRCallerType in ('Provider', 'Clinical Intake')
AND (VCCNODE IN ('Eagan Node 7','Plymouth Node 1', 'Eagan Node 5') or VCCNode IS NULL)
AND Q7 IS NOT NULL
AND Q7 <> '0'
AND (((Site IN('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'Int_Frederick', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu', 'PHS_Manila')
AND skillnumber IN ('718','735','736','1689','1690', '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'))
and CallDate < '2/1/2015')
or ((Site IN('Belapur', 'Cebu', 'Frederick', 'Hookset', 'Huntsville', 'Kolkata', 'Manila', 'Moline', 'Moline PHS', 'Richardson', 'St. Louis','Tampa', 'Int_Richardson', 'Int_Manila', 'INT_Miramar', 'INT_Tampa', 'OGS_Cebu', 'WIPRO_Cebu', 'PHS_Manila')
AND skillnumber IN ('718','735','736','1689','1690', '718','730','780','781','1031','1032','1033','1034','1035','1036','1511','1512','1513','1514','1515','1516','1517','1521','1037','1519'))
and CallDate >= '2/1/2015'))-- adding code to remove INT_Frederick after 2/1/2015 - FLH
AND SkillNumber not IN ('777', '215', '217','235','249')
and SurveyID NOT IN (SELECT SurveyID from SRVY_InsightData_Excpt) --where CallDate between Effective_DT and Expiry_Dt)
AND calldate BETWEEN @start AND @end
) pvt UNPIVOT
(Response FOR
Question IN (Q7)) AS unpvt) AS Data
GROUP BY Question, Site, bus_segment_type, CallDate, skillnumber, VCCNode, IVRCallerType,ProviderTaxIDAgencyTaxID, SurveyID
) A left join D_PPSM_TINs B on A.ProviderTaxIDAgencyTaxID=B.TIN
) final
where
--[BIC_PILOT] in (@BIC_PILOT)
CHARINDEX(',' + [BIC_PILOT] + ',',','+ @BIC_PILOT + ',') > 0
and
--Case when [Strategic_Account] is Null then 'Null' else [Strategic_Account] end in (@Strategic_Account)
( ([Strategic_Account] is Null AND CHARINDEX(','+ 'Null' + ',',','+ @Strategic_Account + ',')>0) OR ([Strategic_Account] is NOT Null AND CHARINDEX(','+ [Strategic_Account] + ',',','+ @Strategic_Account + ',')>0) )
and
--Case when BIC is Null then 'No' else 'Yes' end in (@BIC)
( (BIC is Null AND CHARINDEX(','+ 'No' + ',',','+ @BIC + ',')>0) OR (BIC is NOT Null AND CHARINDEX(','+ 'Yes' + ',',','+ @BIC + ',')>0) )
and
--case when Expiry_dt = convert(varchar(10),'12/31/9999',121) then 'Active' else 'Inactive' end in (@Active)
( (Expiry_dt=convert(varchar(10),'12/31/9999',121) AND CHARINDEX(','+ 'Active' + ',',','+ @Active + ',')>0) OR (Expiry_dt<>convert(varchar(10),'12/31/9999',121) AND CHARINDEX(','+ 'Inactive' + ',',','+ @Active + ',')>0) )
答案 1 :(得分:0)
通过系统地评论WHERE子句的内容并重新运行查询的过程。
单独运行子查询以确保它们按预期运行。
一旦您更好地了解查询失败的位置,就发布更多信息。