I saw a post here in stack overflow with question and answer very similar to my problem.
its this post:
MySQL: turn decimal into percent within view
and as said in the post above, use:
CONCAT(columnname * 100, '%')
and thats what i actually did..
i have a table with a column for percentages for specific ranges of salary.
its a salary deduction schedule.
i stored the percent values as a decimal datatype in column "ee".
ee | decimal(4,4) | not null
and my problem is its giving me this result and its not what i wanted..
for example, an entry in the column is 0.0200, and i want to show it upon viewing as "2%", but this is what i get.
select s_b,rangeA,rangeB,concat(ee * 100,'%') as 'ee_percent' from htbl;
+___________________________________+
|s_b| rangeA| rangeB|ee_percent |
+---+--------+---------+------------+
| 1| 0.00| 1500.00| 1.0000% |
| 2| 1500.00|999999.99| 2.0000% |
+-----------------------------------+
I know that this is a duplicate and an elementary question, but i don't know why its not working,
if you see a mistake somewhere in my syntax, or in the data type please tell me.
any suggestions and corrections are highly appreciated. Thank you so much.
You can use the following solution using FORMAT:
SELECT s_b, rangeA, rangeB, CONCAT(FORMAT(ee * 100, 0), '%') AS 'ee_percent'
FROM htbl;
The result of the calculation is always DECIMAL(4,4). So MySQL show the result with four decimal places. You can use the FORMAT function to format the result or other functions which remove the decimal places.
demo: https://www.db-fiddle.com/f/ncjBpJRwdQVbT7PUBoXgeU/0
You can try with Floor function,
select s_b,rangeA,rangeB,concat(FLOOR(ee * 100),'%') as 'ee_percent' from htbl;
Use round, i.e:
select s_b,rangeA,rangeB,concat(round(ee * 100),'%') as 'ee_percent' from htbl;
Related
I have two datasets and I'm using Lookup to get one result, but the total is only from one dataset. I've tried and tried, but I'm having no luck.
First Dataset is called MedCond
This is the Data:
Drug_Name
Start_Date
Stop_Date
InmateID
Drug_Indication
Created
ID
Second Dataset is called ProblemList
This is the Data:
Medical_Condition
Inmate_ID
Created
ID
Drug Indication and Medical Condition are the same. I want to get a combined total of both.
This only gives me the count of the Drug Indications (which I have them grouped on) =Count(Lookup(Fields!Drug_Indication.Value,Fields!Medical_Condition.Value,Fields!Medical_Condition.Value, "ProblemList"))
I feel like I've tried everything under the sun. I'm extremely exasperated. I'm trying to get a total of each of the conditions/Indications that come from each dataset. For instance, One condition/Indication might be Addiction. There may be four addictions in from the Drug_Indication in the MedCon dataset and five addictions from the Medical_Condition in the ProblemList. I want to see Addictions 9 on the table and so and so forth for each Drug Indication/Medical Condition.
Thanks in advance for your help! Save my sanity. :) I'm sure it's probably something simple?
Tara
Thank you. I've tried using the Inmate_ID and InmateID as the key to join, but I still end up with only one of counts of either Medical_Condition or Drug_Indication.
As an example, there might be 10 addictions in one and 15 addictions in the other. I need them to be grouped under the title addiction (and whatever other titles there might be) with the total being 25.
It would look something like this.
Example Look
Something like this is close, but the counts aren't quite right.
=Count(Lookup(Fields!InmateID.Value, Fields!Inmate_ID.Value, Fields!Medical_Condition.Value, "ProblemList")) + Count(Fields!Drug_Indication.Value)
Maybe it's the way I'm grouping? How do you group on a combination of values such as Medical_condition and Drug_Indication?
Thanks again!
Tara
I think you used the Lookup() wrong. When I look at your two datasets (for me) the key to join the two datasets would be Inmate_ID.
=Lookup(Fields!InmateID.Value, Fields!Inmae_ID.Value, Fields!Medical_Condition.Value, "SecondDatasetName")
This would result in a table like this (The last column comes form the lookup above):
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Now you can just get the total per column:
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Total1 Total2
To sum Total1 and Total2 you can add a new tablix and reference to the textbox totals like this:
=ReportItems!Total1TextboxName.Value + ReportItems!Total2TextboxName.Value
Consider the following table teammessages:
15:10 | Peter | I'm off to the store, call my mobile phone if you need me.
15:11 | Susy | Have you seen the new scanner? It's lightning fast.
15:15 | Anne | We have an emergency here, John broke the tap! Please switch off the water supply!
15:15 | John | I did what?? :-D I'm in Vienna!
15:16 | Peter | I'm stuck in the elevator, help!
15:17 | Anne | I said WATER, not POWER supply!
When I want to SELECT the urgent messages, containing key words like "help" or "emergency", from that chaotic freetext, I would have to have a "reverse IN" function, which doesn't check if the field is IN a given list of alternatives (WHERE APPLECOLOR IN ('YELLOW', 'RED')) but which checks if a value of the given alternatives is IN the field (WHERE freetext REVERSE_IN ('help', 'emergency')).
I've tried constructs like WHERE freetext LIKE ('%help%' OR '%emergency%'), but this gives me empty results.
Clarification: I can do this with freetext LIKE ... OR freetext LIKE ... OR freetext LIKE ... but it gets very long. So I am just looking for a shorthand like IN is in the "reverse case" (APPLECOLOR = 'RED' OR APPLECOLOR = 'YELLOW' <=> APPLECOLOR IN ('RED', 'YELLOW')).
The correct form of your original query is:
WHERE freetext LIKE '%help%' OR freetext LIKE '%emergency%'
However you might also like to consider using MATCH. For that you will need to have a FULLTEXT index on the message column which you can add with this command:
ALTER TABLE teammessages ADD FULLTEXT INDEX(message);
You can then search using a query like this one:
SELECT *
FROM teammessages
WHERE MATCH(message) AGAINST('help emergency' IN BOOLEAN MODE)
which will give you better results than LIKE, which would also match words like 'whelp' or 'helpful' or 'helps'. In your sample table, this query returns:
time name message
15:15:00 Anne We have an emergency here, John broke the tap! Please switch off the water supply!
15:16:00 Peter I'm stuck in the elevator, help!
This query also has the advantage of not getting long as fast as multiple LIKEs
I suggest a Generated Column.
Answers involving LIKE are going to be very slow here, and answers involving full text search (freetext) expressions are telling MySql to index every word in the column, rather than just the words you care about.
You can use a generated column to produce a simple Y/N or 1/0 result for just the words you care about that only needs to evaluate the full column data at INSERT/UPDATE time.
You are probably looking for the words help and emergency. You don't want to find
Jake, your jokes are not helpful.
You must subtract the tax. Hope this helps :-)
So check whether your version of MySQL supports regular expressions where you can look for words:
select *
from teammessages
where freetext regexp '[[:<:]](help|emergency)[[:>:]]';
Rextester demo: http://rextester.com/ILROPQ12660
WHERE freetext LIKE ('%help%' OR '%emergency%') does not work like this.
It should be:
WHERE freetext LIKE '%help%' OR freetext LIKE '%emergency%'
Additional: From documentation of LIKE function:
With LIKE you can use the following two wildcard characters in the
pattern:
% matches any number of characters, even zero characters.
_ matches exactly one character.
mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1
you should try :
WHERE freetext LIKE('%help%') OR freetext LIKE('%emergency%')
You could find all messages that are urgent with keywords you know by doing something similar to this:
SELECT * FROM teammessages WHERE freetext LIKE '%help%' OR freetext LIKE '%emergency%';
You could do the 'reverse' of this by using NOT LIKE this way:
SELECT * FROM teammessages WHERE freetext NOT LIKE '%help%' AND freetext NOT LIKE '%emergency%';
I am stuck with a MySQL query. I have tried a lot of ways but no luck so far. I'm still trying but I need a tip to follow the right path. The query is to get data from 3 tables based on some conditions. All is going well, just give me a hint on this part of the query.
select id,userid,amount from coins where id in (3,4)
This gives me:
id | userid | amount
3 | 2 | 900
4 | 3 | 1100
I want to get the record that has the maximum amount, ie here 1100, but I want to keep the where condition at the end (3,4), as it is the nested part, coming from another query (another table). So basically I need a solution that is in the same query.
If my question is not clear, please let me know, I will add the whole query and all tables data. Any help will greatly appreciated.
This should do the trick;
select id,userid,amount from coins where id in (3,4) order by amount DESC limit 1
This is in reference to the still-open question here - I want to try to approach it differently.
I have a MySQL table with two pieces of information: order_date and email_address. This is exhaustive and non-distinct, meaning that there are duplicates if someone happened to make more than one purchase per-day.
I need to get the following report in as few queries as possible, hopefully one:
YYYY-MM | number_emails_this_month | numer_emails_repeated_prior
Where some sample output from the query result would look like this:
YYYY-MM | number_emails_this_month | numer_emails_repeated_prior
2010-02 23423 1231
2010-03 4422 2234
2010-04 1424 650
Any help is greatly appreciated!
I am not sure I understand what is number_emails_repeated_prior. If you could post a short example of data and a corresponding example of wanted results it would be helpful.
Taking a guess about what you are aiming for, to get the number of emails from a specific user per month all you need is:
SELECT DATE_FORMAT(order_date, '%Y-%m') as `YYYY-MM`,
COUNT(email_address) as `number_emails_this_month `
FROM table_name
WHERE email_address = 'some#address'
GROUP BY 1
ORDER BY 1
This question was answered in a subsequent related question here:
MySQL Subquery with User-Defined Variables
Ultimately the solution was to create a table with the ranges as-requested and join on that instead of using a subquery to define the ranges in question. In retrospect, use of the user-defined vars in MySQL aided the search for the subquery-less solution.
I want to log certain activities in MySql with a timecode using time(). Now I'm accumulating thousands of records, I want to output the data by sets of hours/days/months etc.
What would be the suggested method for grouping time codes in MySQL?
Example data:
1248651289
1248651299
1248651386
1248651588
1248651647
1248651700
1248651707
1248651737
1248651808
1248652269
Example code:
$sql = "SELECT COUNT(timecode) FROM timecodeTable";
//GROUP BY round(timecode/3600, 1) //group by hour??
Edit:
There's two groupings that can be made so I should make that clearer: The 24 hours in the day can be grouped but I'm more interested in grouping over time so returning 365 results for each year the tracking is in place, so total's for each day passed, then being able to select a range of dates and see more details on hours/minutes accessed over those times selected.
This is why I've titled it as using PHP, as I'd expect this might be easier with a PHP loop to generate the hours/days etc?
Peter
SELECT COUNT(*), HOUR(timecode)
FROM timecodeTable
GROUP BY HOUR(timecode);
Your result set, given the above data, would look as such:
+----------+----------------+
| COUNT(*) | HOUR(timecode) |
+----------+----------------+
| 10 | 18 |
+----------+----------------+
Many more related functions can be found here.
Edit
After doing some tests of my own based on the output of your comment I determined that your database is in a state of epic fail. :) You're using INT's as TIMESTAMPs. This is never a good idea. There's no justifiable reason to use an INT in place of TIMESTAMP/DATETIME.
That said, you'd have to modify my above example as follows:
SELECT COUNT(*), HOUR(FROM_UNIXTIME(timecode))
FROM timecodeTable
GROUP BY HOUR(FROM_UNIXTIME(timecode));
Edit 2
You can use additional GROUP BY clauses to achieve this:
SELECT
COUNT(*),
YEAR(timecode),
DAYOFYEAR(timecode),
HOUR(timecode)
FROM timecodeTable
GROUP BY YEAR(timecode), DAYOFYEAR(timecode), HOUR(timecode);
Note, I omitted the FROM_UNIXTIME() for brevity.