When I run my query in the Query Designer, it returns the correct information. When I run the exact same code as a report, it ignores my where clause, and my DB just pukes everything into the table/charts. How do I make it only report what my query is asking for?
This query works through MS query, and it works in the SSRS Query Designer. But it will not return the correct info in the Report Builder
SELECT q.entityid,
i.reqnum as "Request Number",
CASE
WHEN (p.work_group) IS NULL THEN (w.work_group)
ELSE (p.work_group)
END as "Workgroup",
w.creator_userid as "Creator",
a.store_number as "Store Number",
s.division as "Division",
s.region as "Region",
s.city as "City",
s.state as "State",
CASE
WHEN CAST(a.relo_date AS INT) = 10101 THEN MONTH(a.open_date)
ELSE MONTH(a.relo_date)
END as "Date Occupied",
i.servicetyp as "Service Type",
i.subservtyp as "Sub Service Type",
CASE
WHEN (v.vdrname) IS NULL THEN (w.vendor_name)
ELSE (v.vdrname)
END as "Vendor",
i.vendinvnum as "Invoice Num",
i.invdate as "Invoice Date",
i.invamount as "Invoice Amount",
i.description as "Description",
i.class as "Class",
i.createuser as "Created By",
i.createdate as "Create Date",
a.bldtype as "Building Type",
a.prttype as "Property Type"
FROM dwdata.fminvoice i
LEFT JOIN dwdata.fmaccrnt a
ON i.storenum = a.store_number
LEFT JOIN dwdata.fmpohdr p
ON i.reqnum = p.purordnum
LEFT JOIN dwdata.fmvendor v
ON p.ordrvennum = v.vdrnum
LEFT JOIN dwmirror.store s
ON s.sstrno=i.storenum
LEFT JOIN dwdata.fmwrkord w
ON i.reqnum = w.wrkordnum
LEFT JOIN dwdata.fmsrvreq q
ON (w.service_request_number = q.service_request_number OR p.service_request_number = q.service_request_number)
WHERE a.lease_use = 'STORE' and i.invdate >= '2019-01-01'
AND
(i.invdate >= case
when cast(a.relo_date AS INT) =10101 THEN a.open_date
ELSE a.relo_date
END
AND
i.invdate <= case
when cast(a.relo_date AS INT) =10101 THEN a.open_date
ELSE a.relo_date
END + 365 days )
AND a.entityid= q.entityid
I expect the WHERE clause to actually filter the results on the server before sending them over, but it is not. I am getting everything.
****code posted. The where clause works everywhere else but the report builder.
Steve-o169 is on to it. Instead of
i.invdate <= case
when cast(a.relo_date AS INT) =10101 THEN a.open_date
ELSE a.relo_date
END + 365 days )
try
i.invdate <= case
when cast(a.relo_date AS INT) = 10101
THEN DateAdd('D',365, a.open_date)
ELSE DateAdd('D',365,a.relo_date)
END)
https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017
Related
I am trying to execute GROUP BY CASE query on the Couchbase workbench.
The query is -
SELECT Hemisphere, COUNT(DISTINCT name) AS LandmarkCount
FROM `travel-sample` AS l
WHERE type="landmark"
GROUP BY CASE
WHEN l.geo.lon <0 THEN "West"
ELSE "East"
END AS Hemisphere;
This query is provided by the Couchbase documentation as an example to execute GROUP BY CASE queries on this link
I am getting the following error:
[
{
"code": 3000,
"msg": "syntax error - at AS",
"query_from_user": "SELECT Hemisphere, COUNT(DISTINCT name) AS LandmarkCount\r\nFROM `travel-sample` AS l\r\nWHERE type=\"landmark\"\r\nGROUP BY CASE\r\n WHEN l.geo.lon <0 THEN \"West\"\r\n ELSE \"East\"\r\nEND AS Hemisphere;"
}
]
I am working on Community Edition 6.0.0 build 1693.
I think I have the solution you want:
SELECT Hemisphere,
COUNT(DISTINCT name) AS LandmarkCount
FROM `travel-sample` AS l
LET Hemisphere = CASE
WHEN l.geo.lon <0 THEN "West"
ELSE "East"
END
WHERE type="landmark"
GROUP BY Hemisphere;
The error you're getting is because in 6.0, AS isn't supported in GROUP BY (as shown in the 6.0 docs and as the error message says).
An alternative is to use LET in 6.0+ to define a variable, as I did in the above example.
But note that LET is not required either; you could also write your query without it, like this:
SELECT CASE
WHEN l.geo.lon <0 THEN "West"
ELSE "East"
END AS Hemisphere,
COUNT(DISTINCT name) AS LandmarkCount
FROM `travel-sample` AS l
WHERE type="landmark"
GROUP BY CASE
WHEN l.geo.lon <0 THEN "West"
ELSE "East"
END;
But I think you'll agree that the former is easier to read, understand, and change.
I am working on the following query:
declare #start date = '06/01/2016';
declare #end date = '07/31/2017';
----------------------------------------------------------------------------- ------------------------------------------------
-- Pull all claims with paid date in range parameter
-----------------------------------------------------------------------------
------------------------------------------------
if object_id('LA_Temp.dbo.Item19') is not null drop table LA_Temp.dbo.Item19
select distinct
c.claimid,
c.formtype,
c.facilitycode + c.billclasscode as BillType,
case when primaryclaimid = '' and resubclaimid = '' then 'Clean' else 'Other' end as CleanClaim,
-- DHHClaimtype 04 needs to be broken out based on provider specialty and location
Case when c.formtype = '1500' and cd.location = 21 and ps.specialtycode in ('05','22','1T','1F','30','1C') then '04-Hospitalist'
when c.formtype = '1500' then '04-Other'
else ' '
end as DHHClaimtype,
c.status,
c.totalpaid,
e.phystate as MemberState,
e.phycounty as MemberParish,
pc.ParishCode as MemberParishCode,
con.contracted as NetworkProvider,
reject
into LA_Temp.dbo.Item19
from claim c
inner join member m on c.memid = m.memid
inner join entity e on m.entityid = e.entid
left join LA_Temp.dbo.ParishCodes pc on e.phycounty = pc.Parish
inner join contract con on c.contractid = con.contractid
inner join provider p on c.provid = p.provid
inner join provspecialty ps on p.provid = ps.provid and ps.spectype =
'PRIMARY'
inner join claimdetail cd on c.claimid = cd.claimid and cd.claimline = 1 -- just pull the first line to
grab the location code, exclude any location codes with non-numeric values
where c.paiddate between #start and #end
and c.status in ('PAID','DENIED');
-- add the claim types to the table
EXECUTE LA_Temp.[dbo].[USP_LA_SetDHHClaimType] #Table = 'Item19';
The problem exists in the first case statement. Specifically here:
and cd.location = 21
Upon further investigation of the claimdetail (cd) table, I have found that column cd.location (datatype = int) has 4 values ('H', 'U8', 'A', 'OH') which are onviously not numeric. I would like to convert these values to blanks (if possible, not sure if blanks (' ') are compatible with int datatype) or zeros if blanks will not work. Due to the NonNumeric values, I am getting the following error (which is to be expected):
Msg 245, Level 16, State 1, Line 13
Conversion failed when converting the varchar value 'H ' to data type int.
I am aware that I can exclude these records in either the join clause or in a where statement such as:
Where cd.location not in ('H', 'U8', 'A', 'OH')
However, I want to keep the records that these values are tied to, I just want the cd.location value to be blank when it is one of these 4 values. Can someone show me how I can keep these records, by converting cd.location to ' ' when cd.location in ('H', 'U8', 'A', 'OH').
I think I got it...
Again we are focusing on the first case statement in the originally posted query:
case
-- DHHClaimtype 04 needs to be broken out based on provider specialty and location
When c.formtype = '1500' and cd.location = 21 and ps.specialtycode in ('05','22','1T','1F','30','1C')
Then '04-Hospitalist'
When c.formtype = '1500'
Then '04-Other'
else ' '
end as DHHClaimtype,
Changed to:
case
-- DHHClaimtype 04 needs to be broken out based on provider specialty and location
When c.formtype = '1500' and Case When IsNumeric(cd.location) = 0 Then ''
Else cd.location End = 21 and ps.specialtycode in ('05','22','1T','1F','30','1C') then '04-Hospitalist'
When c.formtype = '1500'
Then '04-Other'
else ' '
end as DHHClaimtype,
I have this query.
(CASE
WHEN (T1."LineNum") = '0' THEN (T1."U_UNE_BLDT"[enter image description here][1])
END AS "1",
CASE
WHEN (T1."LineNum") = '1' THEN T1."U_UNE_BLDT"
END AS "2",
CASE
WHEN (T1."LineNum") = '2' THEN T1."U_UNE_BLDT"
END AS "3",
Expected output
I want to use CASE with three conditions. It is showing error.
My query :
select distinct a.phone, other,starttime,duration,imeinumber,
imsinumber,call_type,a.provider_key, a.celltowerid, siteaddress,
lat, long, azimuth from cdat a
left join cdatdupl.dbo.cdatcelltowerareanew b on
case
when a.provider_key!='9' then a.tower_key=b.tower_key
else a.celltowerid = b.celltowerid
and a.provider_key=b.provider_key
and a.state_key end=b.state_key
You have to close the CASE expression with end keyword
syntax
CASE { simple_case_expression
| searched_case_expression
}
[ else_clause ]
END
Updated Query
select distinct a.phone, other,starttime,duration,imeinumber,
imsinumber,call_type,a.provider_key, a.celltowerid, siteaddress,
lat, long, azimuth from cdat a
left join cdatdupl.dbo.cdatcelltowerareanew b on
case
when a.provider_key!='9' then a.tower_key=b.tower_key
else (a.celltowerid = b.celltowerid
and a.provider_key=b.provider_key
and a.state_key end=b.state_key)
end
I have a main data set called "IncomeRecievedGeneralNeeds". This is what is used on the main report. This includes a field called "Scheme"
I have created a new dataset called "scheme" and a parameter called scheme, but I only want the scheme parameter to show schemes that are in the main dataset.At present it is showing me schemes that are not in the main dataset.
The Scheme code is here -
SELECT DISTINCT loc.scheme AS 'Scheme'
FROM ih_location loc
The IncomeRecievedGeneralNeeds code is here -
SELECT DISTINCT trans.tncy_sys_ref AS 'TncySysRef'
,ten.tenancy_ref AS 'TenancyRef'
,trans.created_date AS 'TransactionCreatedDate'
,MHS.startdate AS 'StartofWeekDate'
,MHS.enddate AS 'EndofWeekDate'
,MHS.financialyear AS 'TransactionFiscalYear'
,MHS.monthname AS 'TransactionFiscalMonthName'
,MHS.month AS 'TransactionFiscalMonth'
,MHS.week AS 'TransactionFiscalWeek'
,CONCAT('Week ',
MHS.week,
' (',
CONVERT(varchar(11),MHS.startdate,103),
'-',
CONVERT(varchar(11),MHS.enddate,103),
')'
) AS 'TransactionFiscalWeekWithDates'
,trans.comment_ AS 'TransactionComment'
,trans.trans_amt AS 'TransactionAmount'
,CASE WHEN trans.account_type IN ('IN','LI') Then 'Income'
WHEN trans.account_type = 'HB' Then 'HousingBenefit'
ELSE '' END AS 'AccountType'
,trans.account_type
,ACC.description AS 'AccountCode'
,loc.scheme AS 'Scheme'
,loc.mgt_area AS 'Management Area'
,loc.location_type AS 'Location Type'
,loct.description AS 'Location Type Description'
,ten.tncy_start AS 'TenancyStartDate'
,ten.tncy_end AS 'TenancyEndDate'
,CASE WHEN ten.tncy_end IS NULL
OR TRANS.created_date < ten.tncy_end
THEN 'Current' ELSE 'Former' END AS 'IncomeTenancyStatus'
,ten.tncy_status AS 'TenanacyStatus'
,CASE WHEN ten.tncy_status = 'FOR' THEN LOC.former_arrs_ofcr ELSE loc.arrears_ofcr END AS 'OfficerCode'
FROM [dbo].[re_tncy_trans] trans
INNER JOIN
re_tenancy ten
ON trans.tncy_sys_ref = ten.tncy_sys_ref
INNER JOIN
re_tncy_place tenpl
ON TEN.tncy_sys_ref = tenpl.tncy_sys_ref
INNER JOIN
ih_location loc
ON tenpl.place_ref = loc.place_ref
INNER JOIN
[dbo].[re_accounts] acc
ON
trans.account_code = ACC.account_code
AND trans.account_type = acc.account_type
INNER JOIN
[mhsInsight].[dbo].[mhs_month_week] mhs
ON trans.created_date BETWEEN mhs.startdate AND MHS.enddate
INNER JOIN
[dbo].[ih_location_type] AS loct
ON loc.location_type = LOCT.location_type
WHERE trans.account_type IN ('IN','HB','LI')
AND MHS.startdate >= CONVERT(DATETIME, '2014-04-07 00:00:00', 102)
and loc.place_ref <> '9999999999'
In your parameter query, use the same FROM and WHERE clause that you use in your main query, and you will only get the same Schemes that you have in your main dataset.