mysql how to show order number in result - mysql

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

Related

SSRS Overload resolution failed because no accessible 'IIf' accepts this number of arguments

I wrote a formula using the values of other textboxes in a textbox, but the system does not accept it. The formula is as below
=IIF(ReportItems!Textbox66.Value>=1,IIF(ReportItems!Textbox257.Value>=500,CInt((ReportItems!Textbox66.Value*100-100)/5),IIF(CInt((ReportItems!Textbox66.Value*100-100)/5)>=4,4,CInt((ReportItems!Textbox66.Value*100-100)/5))),IIF(ReportItems!Textbox257.Value<500,IIF(CInt((100-ReportItems!Textbox66.Value*100)/5)>-4,-4,IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))),(CInt((100-ReportItems!Textbox66.Value*100)/5*(-1)))))
It looks like there's an IIF statement with only one of the three arguments.
IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))
It should have an argument for the value when the IIF expression is True of False.
IIF((100-ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
It's easier to find if you use returns and tabs to break up the expression. A few spaces don't hurt either.
=IIF(ReportItems!Textbox66.Value >= 1,
IIF(ReportItems!Textbox257.Value >= 500,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
IIF(CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4,
4,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5)
)
),
IIF(ReportItems!Textbox257.Value<500,
IIF(CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
IIF((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
),
CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
)
It might be a bit more tedious to use a SWITCH but it may be easier to read and make it work the way you want.
=SWITCH(ReportItems!Textbox66.Value >= 1 AND ReportItems!Textbox257.Value >= 500, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox66.Value >= 1 AND CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4, 4,
ReportItems!Textbox66.Value >= 1, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox257.Value < 500 AND CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
ReportItems!Textbox257.Value < 500 AND (100 - ReportItems!Textbox66.Value * 100) / 5 * (-1) = ?????, ?????,
ReportItems!Textbox257.Value < 500, CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)

How to auto increment a string with sql query

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

MySQL Calculating the sum of each digit of an Text value

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.

MySQL Query for calculating weightage

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.

Does mysql query cache the dynamically calculated columns

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)