Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.
Related
I have a column which I am trying to convert in MySQL into another column with a pattern where ever there are consecutive 1s in the data. Please see the example dataset below
Dataset Sample: https://1drv.ms/x/s!ApGNZAoiMmX3gi9OR7SUxt3ou84v?e=tuSV7f
Following is the code I have written but not able to make it work and any suggestions would be helpful.
select rownum,result,movingsum,new_result
(select rownum,result,movingsum,
if(result_norm_max=0,0,if(movingsum=1,1,0)) as new_result
from
(select rownum,result,
sum(result) over (order by rownum rows between 2 preceding and current row) as movingsum
from mytable) a;
The issue is, the above code doesn't return the output needed for all required logic of:
when result column is 0 new_result should be 0
when result is 1, new_result = 1 but only when previous 2 new_results are 0
Any suggestion on how I should approach this will be useful.
Thanks!
With some tries I was able to find the solution which is close to what I need as mentioned below. I used 2 variables to carry out the trick,
select rownum,result,
if (result= 0, 0, if(#n = 1, if(#m >= 7, 1 , 0), 1)) as new_max,
if (result= 0, 0, if(#n = 1,
case when #m >= 7 then #m:=0 else 0 end
, 1)) as new_max1,
if (result= 0, if(#m>0,#m:=#m-1,#m:=0), if(#n = 1, #m:=#m+1,#m:=#m-1)) as new_m,
#n := result
from mytable a, (select #n:= 0, #m:= 0) b
I have strings that are 6 characters in length and I need to test if the first character and third are the same, or the 1st and 5th or 3rd and 5th. The strings contain letters and numbers
So
aabbcc --> false
abbcad --> true
aaabcd --> true
bacada --> false
1a1b33 --> true
I need this to be part of a mysql query. Help is greatly appreciated!
The simplest (and probably fastest) way is to compare the individual substrings:
SELECT str,
SUBSTR(str, 1, 1) = SUBSTR(str, 3, 1) OR
SUBSTR(str, 1, 1) = SUBSTR(str, 5, 1) OR
SUBSTR(str, 3, 1) = SUBSTR(str, 5, 1) AS matching
FROM data
Output
str matching
aabbcc 0
abbcad 1
aaabcd 1
bacada 0
1a1b33 1
If you are running MySQL 8+ you can take advantage of the enhanced regex capability to use back-references in the pattern:
SELECT str,
REGEXP_LIKE(str, '^(.).\\1') OR
REGEXP_LIKE(str, '^(.)...\\1') OR
REGEXP_LIKE(str, '^..(.).\\1') AS matching
FROM data
Output is the same as the previous query:
str matching
aabbcc 0
abbcad 1
aaabcd 1
bacada 0
1a1b33 1
Demo on dbfiddle
You can use substring().
...
WHERE substring(nmuloc, 1, 1) = substring(nmuloc, 3, 1)
OR substring(nmuloc, 1, 1) = substring(nmuloc, 5, 1)
OR substring(nmuloc, 3, 1) = substring(nmuloc, 5, 1)
...
I have a table RAW_SCORES that contains a bunch of homework/exam grades. There is a row in RAW_SCORES that contains the max points for each assignment.
('6410', 'Rivera', 'Rhonda', '315', 64, 64, 28, 85, 98, 152),
('0001', 'MAX', 'POINTS', '415', 100, 80, 32, 100, 120, 200),
I want to create a procedure that prints a table with the (Raw Score / Max Score) for each assignment. So for assignment 1:
SET hw1M = (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001'));
SELECT RAW_SCORES.SSN,
RAW_SCORES.FName,
RAW_SCORES.LName,
ROUND(RAW_SCORES.HW1 / hw1M, 2)
FROM RAW_SCORES WHERE NOT (RAW_SCORES.SSN = '0001' OR RAW_SCORES.SSN = '0002');
Gives me the correct result for HW1, but the table header says Round(bla bla bla) and the question wants it to be printed as HW1Pct. So I tried:
SET hw1M = (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001'));
SET HW1Pct = (SELECT ROUND(RAW_SCORES.HW1 / hw1M, 2) FROM RAW_SCORES WHERE hw1M IN (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001')));
SELECT RAW_SCORES.SSN,
RAW_SCORES.FName,
RAW_SCORES.LName,
HW1Pct
FROM RAW_SCORES WHERE NOT (RAW_SCORES.SSN = '0001' OR RAW_SCORES.SSN = '0002');
But this gives me the Subquery returns more than 1 row error. Most of the other answers to this error are a JOIN statement, but I'm not sure how I would implement that in my case. Any help is appreciated. Sorry for it being a dumb question.
Just add a column alias to your original query:
ROUND(RAW_SCORES.HW1 / hw1M, 2) AS HW1Pct
See the manual
When attempting to get a total weighted average interest rate I occasionally receive Error when there is only one item in certain columns. Having trouble with the Iif statement handling this:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0, SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value))/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value))
Moved your brackets slightly, this seems to work:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0,
SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value)/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value)))
I'm looking to bring in my expression 2 values that I what to add together.
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
but is just coming back as 0 when the value should be 400 or so.
Can any one point me in the right direction?
I'm not sure how SSRS evaluates your expression
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
I think SUM(1 AND 0) and SUM(1 AND 1) both equal 1.
Your expression needs to be changed a little - though I'm not sure which you need.
=Sum(IIF(Fields!Leadsource.Value = "set1" OR Fields!Leadsource.Value = "set", 1, 0))
Otherwise if you want to count the two different criteria separately, use:
=Sum(IIF(Fields!Leadsource.Value = "set1", 1, 0) + (Fields!Leadsource.Value = "set", 1, 0))