I have a mysql query:
SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'
How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?
If not, how can I make the change in the query so that each function is called minimum times possible.
MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
You can do
SELECT * from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)
Related
so here is the query
select * from test order by pow(c1 - 8, 2) + pow(c2 - 5, 2) limit 3
is there any way to show order section -> "pow(c1 - 8, 2) + pow(c2 - 5, 2)" for each record in result ?
Just include it in the select:
select t.*, pow(c1 - 8, 2) + pow(c2 - 5, 2) as distance_squared
from test t
order by distance_squared
limit 3
I am stuck at a point where i have to increment a string, and my strings are of type C001,SC001,B001
in my data base they are defined like
what i am trying to do do is write a query which check the previous highest code present into my db and the incriment it to +1
for example C001 -> C002,C009->C010,C099`->C100 and so on
Similarly for SC001->SC002,SC009->SC010,SC099->SC100 and so on
Similarly fro B001 -> B002,B009->B010,B099`->B100 and so on
I have a query which my friend has suggested me to use but that query only incriminating AAAA->AAAA01 , AAAA09->AAAA10
query is
SELECT id AS PrevID, CONCAT(
SUBSTRING(id, 1, 4),
IF(CAST(SUBSTRING(id, 5) AS UNSIGNED) <= 9, '0', ''),
CAST(SUBSTRING(id, 5) AS UNSIGNED) + 1
) AS NextID
FROM (
-- since you allow strings such as AAAA20 and AAAA100 you can no longer use MAX
SELECT id
FROM t
ORDER BY SUBSTRING(id, 1, 4) DESC, CAST(SUBSTRING(id, 5) AS UNSIGNED) DESC
LIMIT 1
) x
when i am replacing ID with CategoryCode it is giving me PrevID-C004 NextID-C00401 which is not my requirement i want PrevID-C004 and NextID->C005
NOTE i am using my sqlServer 5.1
Just try this one ,
SELECT
CategoryCode,CAST(CONCAT(LPAD(CategoryCode,1,0),LPAD(MAX(RIGHT(CategoryCode,
3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
SubCategoryCode,CAST(CONCAT(LPAD(SubCategoryCode,2,0),
LPAD(MAX(RIGHT(CategoryCode, 3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
BrandCode,CAST(CONCAT(LPAD(BrandCode,1,0), LPAD(MAX(RIGHT(BrandCode, 3)) +
1, 3, 0)) AS CHAR) FROM test
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))
How to calculate the sum of each digit of an Text value in MySQL?
for example
SET #Chars = CONCAT('1','2','33');
-- #Chars = '1233'
-- and result should be 1+2+3+3 = 9.
Yeah, not pretty to do, but here's a way to do it. I'm like you -- had to do it for some spot checking. We do check digit calculations and I needed some specific accounts where the digits would sum in various ways.
select (substring('123456', 1, 1) +
substring('123456', 2, 1) +
substring('123456', 3, 1) +
substring('123456', 4, 1) +
substring('123456', 5, 1) +
substring('123456', 6, 1) +
substring('123456', 7, 1) +
substring('123456', 8, 1) +
substring('123456', 9, 1) +
substring('123456', 10, 1) +
substring('123456', 11, 1) +
substring('123456', 12, 1)) as sumOfDigits;
So just replace '123456' with your string and then make sure that the 1..12 in this example is enough to cover the length of the max string you are expecting to calculate a total for.
Here's my real world example:
select (substring(a.member_number, 1, 1) +
substring(a.member_number, 2, 1) +
substring(a.member_number, 3, 1) +
substring(a.member_number, 4, 1) +
substring(a.member_number, 5, 1) +
substring(a.member_number, 6, 1) +
substring(a.member_number, 7, 1) +
substring(a.member_number, 8, 1) +
substring(a.member_number, 9, 1) +
substring(a.member_number, 10, 1) +
substring(a.member_number, 11, 1) +
substring(a.member_number, 12, 1)) as sumOfDigits,
a.member_number from account a
left join account a2 on a2.member_number = a.member_number and a2.discriminator = 'D'
where a.discriminator = 'S'
and a2.account_id is null having sumOfDigits = 16;
According to https://en.wikipedia.org/wiki/Digital_root there is very simple formula to do it.
In MySQL in can be specified this way:
#Chars - 9 * FLOOR( (#Chars-1 ) / 9 ).
Or any field / expression can be used instead #Chars.
To calculate the weightage of faults i've formulated this query,
Select id,faultdistribution, faulttype, faultseverity,
IF (faultdistribution='crs', COUNT(id) * 8, 0) +
IF (faultdistribution='configuration', COUNT(id) * 6, 0) +
IF (faulttype='bs' AND faultseverity='ft', COUNT(id) * 4, 0) +
IF (faulttype='bs' AND faultseverity='mj', COUNT(id) * 2, 0) +
IF (faulttype='bs' AND faultseverity='md', COUNT(id) * 5, 0) +
IF (faulttype='bs' AND faultseverity='mi', COUNT(id) * 3, 0) +
IF (faulttype='lf' AND faultseverity='ft', COUNT(id) * 2, 0) +
IF (faulttype='lf' AND faultseverity='mj', COUNT(id) * 1, 0)
FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
What i intend to do is;
If Fault distribution= 'crs' then Fault * 8 + If Fault distribution= 'configuration' then Fault * 6 .........
As, there are records in database but i am not having any result from above query, help/suggestions required.
Regards
Becuase you are using Aggregate function COUNT in your query which returns only one row.But your query is running for all rows one by one which is wrong.
The COUNT() function returns the number of rows that matches a specified criteria.