I have a backend limitation that the Resultant column which is of decimal type(aban) can not have more than 1000 characters in it formula.
How to optimise this query?
I have tried using tempTable but results are not as expected.
aban =
CASE
WHEN (
sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.ndr,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0))
)
= 0 THEN
0
ELSE (sum(isnull(a.totalcallsaband,0)) * 1.0) / (sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.networkdefaultrouted,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0)))
END
,
Optimise the query in such a way that number of characters in formula is less then 1000 and resultant column should have same value before and after query optimisation.
Please try this way:
DECLARE #dividend int = 10;
DECLARE #divisor int = 0;
SELECT COALESCE(#dividend / NULLIF(#divisor,0), 0);
So you can avoid duplicate calculation for divisor.
I'm struggling with an Access database:
I have a large database with the headers TAG, ZST, KL and R1H00 to R1H23 (24 of them, each one stands for one hour of a day) and I need to get a specific dataset out of it:
SELECT TAG, ZST, (R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
So far so good, but the result contains many items with the same ID (ZST). I need to sum all entries with the same ZST, but I couldn't manage to do it so far. (I tried to use the GROUP BY statement, but that only results in errors)
Any experienced SQL people here that could help me with this?
use group by
SELECT TAG, ZST, sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
GROUP BY TAG, ZST;
Use the Sum() function
SELECT TAG, ZST, Sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
where ZST = '(Whatever it is)'
group by tag, zst
I can't seem to figure out how to use the opposite of isnull or ifnull statements in sql. I need to say if a.Error1 is not null -- then print the ' - ' and the + CHAR(13)+CHAR(10). Basically There should be no dash or no new line break if the a.Error1 comes back null. So print the information if the field isn't null.
select a. ....
' - ' + a.Error1 + CHAR(13)+CHAR(10) +
' - ' + a.Error2 + CHAR(13)+CHAR(10) +
' - ' + a.Error3 + CHAR(13)+CHAR(10) +
' - ' + a.Error4 + CHAR(13)+CHAR(10) +
' - ' + a.Error5 + CHAR(13)+CHAR(10) +
' - ' + a.Error6 as 'error_message'
...
from table1 a
For example if for a given record error1, 2 and 5 returned output I would like the output to be as follows:
- Error1: There was a ...
- Error2: ....
- Error5: The data was ...
If no errors existed for that row it should simply be an empty/null field.
You can use CASE:
SELECT a. ....
(CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error3 IS NOT NULL
THEN ' - ' + a.Error3 + CHAR(13)+CHAR(10)
ELSE ''
END) +
...etc
Yes! i know i'm like 5 years too late but i too enountered this problem.
It's weird how it doesn't exist some kind of !ISNULL() but whatever.
Try this for a cleaner code:
select a. ....
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error1,
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error2
from table1 a
Learn more about IIF() function : SQL Server IIF Function
The COALESCE function does what you want here. The result of COALESCE is the first NOT NULL value it is passed. Below we use '', which is distinct from NULL so that the outer + is always applied to NOT NULL strings.
e.g.
select a. ....
COALESCE( ' - ' + a.Error1 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error2 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error3 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error4 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error5 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error6 , '' ) as 'error_message'
...
from table1 a
SELECT (CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10) +
ELSE a.Error1
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10) +
ELSE a.Error2
END) +
.....etc
I have a query - which is combining with a Union statement the 5 columns into 2 columns.
So basically - an ID + Some Information. As the ID could be in column 2-5 - several times... I rearranged to be everything in one ID column + the text.
Now - I wanted to use a second "with" statement - to combine the IDs of several rows - with a XML path. But here I got stuck.
That is the code - which gives me the two column - with content as expected:
With
STall as (Select
'Status: ' + ST.Status + Char(10) + 'Crew Information:' + Char(10) +
'Instructor: ' + ST.IP + Char(10) + 'Student: ' + ST.SP + Char(10) + Case
When ST.ACM1 = 'NA' Then '' Else 'ACM1: ' + ST.ACM1 + Char(10) End + Case
When ST.ACM2 = 'NA' Then '' Else 'ACM2: ' + ST.ACM2 + Char(10)
End + 'Lesson: ' + ST.Lesson + Char(10) + Case
When ST.ScheduleRemarks = '' Then ''
Else ' REM:' + ST.ScheduleRemarks + Char(10)
End + 'Equipment: ' + ST.EquipmentName + ' (' + ST.Callsign + ')' +
Char(10) + 'Start: ' + Convert(VARCHAR(10),ST.ScheduleTO,104) + ' - ' +
Convert(VARCHAR(5),ST.ScheduleTO,108) + ' UTC' + Char(10) + 'End: ' +
Convert(VARCHAR(10),ST.ScheduleTD,104) + ' - ' +
Convert(VARCHAR(5),ST.ScheduleTD,108) + ' UTC' + Char(10) + Case
When ST.ATC = 'NA' Then '' Else ST.ATC End As SInfo,
ST.IDIP,
ST.IDSP,
ST.IDA1,
ST.IDA2
From
(Select
Case When Schedule.StatusRelease = 1 Then 'OK' Else Case
When Schedule.StatusRequest = 1 Then 'STBY' Else 'N/A' End
End As Status,
Schedule.ContactIDIP,
Schedule.ContactIDSP,
MasterLesson.Lesson,
Equipment.EquipmentName,
CallSignList.Callsign,
Schedule.Meeting,
Schedule.ScheduleRemarks,
Schedule.StatusRelease,
Schedule.StatusRequest,
Schedule.ScheduleDay,
ContactList.FullNameTLC As IP,
ContactList1.FullNameTLC As SP,
Case When Schedule.ContactIDACM1 = 0 Then 'NA'
Else ContactList2.FullNameTLC End As ACM1,
Case When Schedule.ContactIDACM2 = 0 Then 'NA'
Else ContactList3.FullNameTLC End As ACM2,
Schedule.ScheduleTO,
Schedule.ScheduleTD,
Case When Schedule.RouteID = 0 Then 'NA'
Else 'Route: ' + Airport.ICAO + ' - ' + Case
When IsNull(Airport1.ICAO, 'NA') = 'NA' Then ' '
Else Airport1.ICAO + ' - ' End + Airport2.ICAO + Char(10) + Char(13)
End As ATC,
ContactList.Id As IDIP,
ContactList1.Id As IDSP,
ContactList2.Id As IDA1,
ContactList3.Id As IDA2
From
Schedule Inner Join
StudentLesson On Schedule.ScheduleLessonID = StudentLesson.StudentLessonID
Inner Join
MasterLesson On StudentLesson.LessonID = MasterLesson.ID Inner Join
Equipment On Schedule.EquipmentID = Equipment.ID Left Join
CallSignList On CallSignList.ID = Schedule.CallSignListID Left Join
Route On Route.ID = Schedule.RouteID Inner Join
ContactList On ContactList.Id = Schedule.ContactIDIP Inner Join
ContactList ContactList1 On ContactList1.Id = Schedule.ContactIDSP
Left Join
ContactList ContactList2 On Schedule.ContactIDACM1 = ContactList2.Id
Left Join
ContactList ContactList3 On Schedule.ContactIDACM2 = ContactList3.Id
Left Join
Airport On Route.AirportID_Dep = Airport.ID Left Join
Airport Airport1 On Route.AirportID_Via = Airport1.ID Left Join
Airport Airport2 On Route.AirportID_Arr = Airport2.ID
Where
((Schedule.StatusRelease = 1) Or
(Schedule.StatusRequest = 1)) And
Schedule.ScheduleDay = '21.02.2014') As ST)
Select Distinct
STall.IDIP,SInfo
From
STall
where STall.IDIP > 0
UNION
Select Distinct
STall.IDSP,SInfo
From
STall
where STall.IDSP > 0
UNION
Select Distinct
STall.IDA1,SInfo
From
STall
where STall.IDA1 > 0
UNION
Select Distinct
STall.IDA2,SInfo
From
STall
where STall.IDA2 > 0
Now - I tried to add another With STtotal as ( in the beginning...
and
)
Select Distinct
STsub.IDIP,
SubString((Select
+Char(10) + STn1.SInfo As [text()]
From
STsub STn1
Where
STn1.IDIP = STtotal.IDIP
Order By
STn1.IDIP
For Xml Path('')), 2, 1000) ScheduleInfo
From
STtotal
But I get an error - with wrong Statement at "with".
Maybe there is another approach - how to combine - the "Info Text" column with all IDs - which could be in column 2-5.
Thanks for any imput
Separate your CTEs with a comma; the WITH only needs to be specified once:
;WITH CTE1 AS (
...
), CTE2 AS (
...
)
SELECT ...
Did you remember to put a semi colon at the end of the first statement?
When I run the following query I get the above error. I realize this is because the subquery is returning more than 1 result but am not sure how to fix it.
update a
set a.covtypeplus = (SELECT distinct
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT distinct POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN ('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION)
from results_vehicle a
left join staging.coverage c on a.polnum =c.policyid and a.polver = c.version and a.covcode = c.COVCODE
When I run the following original query I created (not an update) it works fine so it must be something I am doing wrong in my update syntax:
SELECT DISTINCT p.POLICYID AS PolNum, p.VERSION As PolVer,
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT DISTINCT POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN
('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION
Any help is greatly appreciated, I've tried playing around with the query and yield the same results. I also tried adding a SELECT TOP 1 and got the same results for all policies.
You are doing:
update a
set a.covtypeplus = (SELECT distinct . . .
This suggests that you are expecting more than one result from that subquery. Not allowed.
Perhaps this will fix it, but it may not be what you want:
update a
set a.covtypeplus = (SELECT top 1 . . .
It appears to be this part that is messing you up:
from results_vehicle a
left join staging.coverage c on a.polnum =c.policyid
and a.polver = c.version and a.covcode = c.COVCODE
This must be generating more than one result for some values of a.covcode.