I've never worked with ORDER BY CASE or ORDER BY IF() and the few examples I've found on the internet are more confusing than helping me trying to accomplish following task:
I have a member list and I want to "reward" the user's activity a bit by ordering a user higher (to the top) in this member list.
In my example, I have a MySQL table called "users" with 3 columns:
user_percentage | user_photo | user_lastlogin
----------------------------------------------------------
12 1 1356389646
42 1 1456549641
37 0 1776389443
84 1 1356535535
56 0 1868689646
66 0 1811189622
71 1 1656779645
"user_percentage" holds the value (0 - 100) of all filled in profile
fields by each user.
"user_photo" holds the value (0 = false , 1 =
true) if a user has upload a profile photo.
"user_lastlogin holds the
value (timestamp) of their last visit.
It's a bit difficult to explain, but to say it simple what I want:
A user with a higher user_percentage value should be on top of the member list, but if he got no user_photo, then he should be "moved down" in the member list, same if he got an old user_lastlogin timestamp, then list him even more down in the member order.
Also what I'm trying to prevent is that a user has signed up, filled in all profile fields (then user_percentage value will be = 100) and uploaded a photo (then user_photo will be = 1), but hasn't login anymore since a long time (so he has a very old user_lastlogin = timestamp), then I want this user moved down in the member list order.
My question is: Is this somehow possible to do with 1 MySQL ORDER BY statement?
Let's say user_photo has just an importance of 30% while user_lastlogin got a higher importance and user_percentage also a bit higher.
Any ideas?
Best regards!
In order to even begin writing the SQL, you need to clarify how much each parameter is worth.
You should produce a list of examples and then convert it into either a simple calculation like:
(photo*timesincelogin*20)+(percentage)
or CASE based statements which each have a calculation - i.e:
ALL people with photos THEN ALL people with out, subsorted by time*percentage.
Hand-wavy 'move down a bit' is not specific enough for an answer with SQL commands.
Related
This is the current data table that I have, which is called 'Potential High Usage'
CLICK TO SEE DATA TABLE
I tried to use the TOPN function to create a table of the top 10 'User ID' based on the 'Number of clicks'. This is how I tried to do it:
High Usage IDs = topn(10,'Potential High Usage',[Number of clicks])
The problem is that it returns 11 rows instead of 10. I am thinking that its because I have many duplicates for the least number of clicks that will still be considered as top 10 (Number of clicks = 2). I was wondering if there is a way to return ONLY 10 rows?
Yes. TOPN will return more than N rows when there are ties at Nth row.
See the Microsoft TOPN dax function documentation.
First add index column using power query.
Then use following DAX to return top 10 rows.
I have a list of leads data.
The table has a lot of infos like date, name, email, mobile number, etc.
However, some of these leads are duplicates: the same person generated more than one lead
What i want to do is to remove the duplicate leads.
The problem is, to be considered a duplicated lead, the email or the mobile number must appear in more than one row, in a time interval of 30 days.
And only those who come next must be considered duplicated. The first one is always a not duplicated lead.
E.g.
1) If Jones generated a lead in 01/01/20 with his email abc#abc.com and then generate another lead 10 days after, in 10/01/20. The first lead is a single lead (not duplicated) and this second lead must be considered duplicate.
2) If Maria generated a lead in 01/01/20 with her email xyz#abc.com and then generate another lead 40 days after, in 10/02/20. The first lead is a single lead (not duplicated) and this second lead also must be considered single (or not duplicate).
To mark the lead as duplicate or not, i want to generate a new column with the time between the last lead of the same person (same email or same mobile number)
Then generate a new column with the label "Duplicate" or "Not Duplicate" based on the time showed on the last column. If its > than 30 days, that is a single lead. Otherwise (<30 days) is a duplicate lead.
E.g picture:
Can someone please help me on how to do that?
Getting the lag/lead data is not very straight forward in Power BI. You will have to use a combination of EARLIERand some aggregate function to get the specific result. For your specific scenario, the following calculation might work:
Day Difference =
VAR name1 = 'Table'[Name]
VAR Lastdate1 = MAXX(FILTER('Table','Table'[Name]=name1 && 'Table'[date]<EARLIER('Table'[date])),'Table'[date])
RETURN
IF(
DATEDIFF(Lastdate1,'Table'[date],DAY)=BLANK(),
100,
DATEDIFF(Lastdate1,'Table'[date],DAY))
Once the column is created, you can filter for all records <=30 to get the result you are looking for. I have replaced the blanks with 100, so that the original records don't get removed when applying the condition.
If you are looking for the "Tag" value, then the following calculation will get you the tag values directly:
Tag =
VAR name1 = 'Table'[Name]
VAR Lastdate1 = MAXX(FILTER('Table','Table'[Name]=name1 && 'Table'[date]<EARLIER('Table'[date])),'Table'[date])
RETURN
IF(IF(
DATEDIFF(Lastdate1,'Table'[date],DAY)=BLANK(),
100,
DATEDIFF(Lastdate1,'Table'[date],DAY))<=30,"Duplicate","Single")
I am working on a report in Access 2013 I need to seperate the first 20 records in a column that contain a value and assign a name to them. Such as at 1-20 I need it to insert Lot 1 at 21-40 need to assign Lot 2 etc... The report needs to be separated by lots of 20. I can also just insert a line when it reaches sets of 20 without a name if that makes it easier. Just need something to show a break at sets of 20.
Example: As you can see the report is separated by welder stencil. When the count in the VT column reaches 20 I need to enter a line or some type of divider to separate data. What our client is asking for is we separate the VT in sets of 20. I don't know whats the easiest way to accomplish this. I have researched it but haven't found anything.
Example Report with Divisions
Update the report's RecordSource query by adding "Lot" values for each row. There are multiple ways of doing this, but the easiest will be if your records already have a sequential, continuous numerical key. If they do not have such a key, you can research generating such sequential numbers for your query, but it is beyond the scope of this question and no details about the actual data schema were supplied in the question.
Let's imagine that you have such a key column [Seq]. You use the modulo (mod) and/or integer division operators (\ - backslash) to determine values that are exactly divisible by 20, e.g. ([Seq] - 1) mod 20 == 0.
Generate a lot value for each row. An example SQL snippet: SELECT ("Lot " & (([Seq] - 1) \ 20)) As LotNumber ...
Utilize Access report sorting and grouping features --grouping on the new Lot field-- to print a line and/or label at the start of each group. You can also have the report start a new page at the beginning or end of such a group.
The details about grouping can be found elsewhere in tutorials and Access documentation and are beyond the scope of this question.
I'm using sequel pro to select data from several tables. There are two things I need to do that seem to need a loop of some kind. I have never used any form of iteration in sql and can't find a beginners-level resource to learn from.
Can anyone suggest how to do the following two tasks, or suggest a tutorial where I can learn the fundamentals and figure it out from there:
Task 1: Go through a version history table, find the relevant history record for a given id that applied at a given date, and select the value from that record. The form of the history table is:
id, Item_id, version-created_at, value
eg
1, 123, 2014-05-01, 754
2, 456, 2014-05-10, 333
3, 123, 2014-05-27, 709
and I need to find what the value of item 123 was on the date 2014-05-25 (ie I need to find record id=1 and value = 754 because that is the most recent version for item 123 created prior to my target date.
So I figure I need to run through the table looking for item 123 and comparing dates of those records. But I don't know how to deal with the iteration of moving from one record to the next and comparing them.
Task 2: Go through a single text field that contains a number of product id and matching product prices in a string, and find the id of the product with the lowest price. Form of the string is a series of pairs of price "p" and id "i", in random order, like this:
"
- :p: 99.8
:i: 3
- :p: 59.0
:i: 5
- :p: 109.8
:i: 18
- :p: 82.45
:i: 46
"
and in this example I need to find "5", being the id of the product with the lowest price $59.
So I figure I need to step through each of the p/i sets, maybe by counting characters, but I have no idea how to iterate through and compare to find the best price.
A little help would go a long way.
Thanks.
For first answer you can do something like this:-
SELECT value FROM history where id = 123 AND version-created_at = '2014-05-01';
and for another task you must try this at front end rather than at back end.
I am taking over designing a CMS from another programmer. As the site is filling up, we're finding loops in mysql queries causing long hangs. I have found a temp solution for this one, but am wondering if there is a quicker way of doing it?
take the table (tracks resources):
id resource click
1 res_1 192
2 res_2 12
3 res_3 300
what we need to get is a popularity of the resource - res_click/total_click
what he had was a while loop:
while ($item = mysql_fetch_array ($result)) $total_clicks = $total_clicks + $item[0];
As there could be 100 or more resources to a page, this was running for each resource, and it is causing major hangs.
My solution is to get a sum:
SELECT SUM(click) FROM uri
SELECT click FROM resource WHERE id=$x
then divide them both.
But this two calls are still running for around a 100 items per page. Is there a way I can have a field in mysql that is the result of a formula based on another another, like in excell? So I could add a field "percentage", tell mysql that it is the sum of click divided by the current click value, then every time click is updated the 'percentage' field is automatically updated?
any help would be appreciated,
cheers ;)
you can create a view on your table that present the sum you want