How to sort by proximity? [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm building an app that recommends people to other people based on how frequently they've been to the same place.
So for a given person A (the one you want to give a suggestion), I have a table of all others persons and the amount of time they've been to a place. i.e, I get this:
idPerson | idPlace | nbTimesPersonWent | nbTimesPersonAWent
10 | 1 | 3 | 10
11 | 2 | 1 | 22
12 | 1 | 11 | 10
13 | 3 | 8 | 2
What I'm struggling with is finding which of these idPerson is the "best" person to recommend to A.
Is there a way (preferably pure SQL), to sort this table from "closer" value of nbTimesPersonWent and nbTimesPersonWent to "less close" values?

I would recommend using the following tables
Person:
id
Place:
id
Visit:
person_id, place_id, time_spent
Now you must choose which way you will sort people that are interesting to a particular person a.
Many different sort functions exists. For any person of interest a, you can rank any other person b based on many different criteria. For example:
f(a,b) = Sum of min_time(a,b,p) for all places p that both a and b have visited, where min_time(a,b,p) = minimum of the time a and b have spent at place p
f(a,b) = The number of places that both a and b have visited
The difference between the two methods is that the first consider the time spent at different places and that the second only considers the number of places commonly visited. You can also define functions that limits the impact of having spent much time the same place, compared to distributing that time over multiple places.
If you can specify an exact ranking criteria, I will be happy to help you write a query for it.
UPDATE: Here is an example of sorting by the 2nd ranking criteria. That is, by the number of visited places in common: sqlfiddle.com/#!9/b56745/1/0

Related

How to SUM the data properly? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Have issue I cannot sum data in the right way. Do not know what is the issue and why query is not summing all the info.
Here is the query :
select m.store, SUM(s.salePrice) as Price ,s.Date, d.name
from market m
inner join distributor d on d.marketId = m.marketId
inner join sales s on s.distributorId = d.distributorId
group by s.Date,m.store,d.name
Outcome of the query :
store | price | date | distributor |
----------------------------------------------------
Store MAX -10000 22-05-2019 RedBull
Store MAX 25000 22-05-2019 RedBull
Store Z -2000 22-05-2019 RedBull
Store Z 15000 22-05-2019 RedBull
What I want as outcome :
store | price | date | distributor |
----------------------------------------------------
Store MAX 15000 22-05-2019 RedBull
Store Z 13000 22-05-2019 RedBull
Why SQL did not include (-) operators in SUM function?
If you can help please advice, thanks!
This would be occurring because something is not the same:
The store names are not the same, but look similar.
The dates are not the same. Perhaps they have a time component, and that is not showing.
The distributor is not the same.
I am guessing that the store names are okay, because they are looked up in the table. You can check if only using that works by using aggregation functions on the other columns:
select m.store, SUM(s.salePrice) as Price,
max(s.Date), max(d.name)
from market m join
distributor d
on d.marketId = m.marketId join
sales s
on s.distributorId = d.distributorId
group by m.store;
If this does not produce duplicates, then you know that store is okay and you can check the other columns. When you find them, you will need to investigate the values to figure out how to fix them.
First replace the d.name to distributor from the "select" and "group by" as I do not see "name" field from your output.

pad/truncate strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Today i went for my first interview for .Net Developer.
Interviewer asked me one tricky question but I can't able to answer that.
I thought lots on that question but not get any solution on that question.
Question is...
ID | Name
1 | Ram
2 | Prathamesh
3 | Naresh
4 | Dasharath
Update this table with following condition;
If Name's character is less than 6 letters then New value must be like "Ram***"
(* mark will be added until characters length is 6)
and if it more than 6 letters all extra letters should be remove.
Result like this :
ID | Name
1 | Ram*** /* added three * marks */
2 | Pratha /* removed extra letters */
3 | Naresh /* No changes */
4 | Dashar /* removed extra letters */
SELECT LEFT(NAME+'******',6) FROM TABLE

Parsing column data in SQL Syntax to an SQL Query

I am trying to solve a business flow issue at my work and I have an idea that I hope is technically feasible in SQL. What I would like to try and do is store different formulas in SQL syntax into database columns. Within SQL queries I would set variables to equal these columns so that the content of the columns is parsed as a part of the query and the different SQL statements pops up depending on the select specifications.
Short and sweet: I have some widgets to sell and whether or not a client can get these widgets depends on what other widgets they have bought, should have and shouldn’t have, country, customerid, their widget version, widget category and a couple of other things.
My question is how would someone proceed with this? I’m sure someone has made a similar setup before but which methods would be useful to study for my case? Are there case studies where I can find inspiration? I have searched for this without any luck. Hopefully someone who have solved similair issues before would be able to point me in a direction.
Thanks to whom‘ever is able to answer and has had the interest to read my post.
Best regards
Zaid
**OK this is more of a comment than an answer but the formatting doesn't work if I enter it as a comment!
To very vaguely answer your question I would have a Widgets table ie
WidgetID | Widget Name
1 Widget1
2 Widget2
3 Widget3
4 Widget4
Then have a WidgetRequirements table which is
WidgetRequiredID | WidgetID | RequiredWidgetID
1 1 2
2 1 3
3 1 4
4 2 4
5 3 1
6 3 4
This tells you that WidgetID 1 needs Widgets 2, 3 and 4 in order to be "active".
Widget 2 only needs widget4 to be active and Widget3 needs widgets 1 and 4.
This should get you started, expand on this theory.

Figuring out what SQL wildcards define a set of accounts [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
We are currently building a complicated financial report where every cell in the table is link to a group of accounts. Currently we will look at the group of a accounts and we manually figure out the filter/wildcards that defines that group. We need the filters to only include the accounts in the list. I was wondering if there a program to do this for us or is there an algorithm we can implement this. Also, all account numbers will be the same length.
Example:
Group A
10004
10005
10006
21001
21023
Group B
10056
10055
Group C
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
Group A would look like 1000[4,5,6], 21001, 21023
Group B would look like 1005[5,6]
Group C would look like 1000%
One solution that comes to mind is trie.
The general algorithm would be to find the longest prefix starting from the 1-st level(not 0-th level), fix this prefix and then append the different suffixes. Hope you will guess the next steps.
For example
Group A would look like 1000[4,5,6], 21001, 21023
Trie would look like
In this case the result is: 1000[4,5,6], 210[01,23]

Table structure of a student

I want a table structure which can store the details of the student like the below format.
If the student is in
10 th standard -> I need his aggregate % from 1st standard to 9th standard.
5 th standard -> I need his aggregate % from 1st standard to 4th standard.
1 st standard -> No aggregate % has to be displayed.
And the most important thing is ' we need to use only one table'. Please form a table structure with no redundant values.
Any ideas will be greatly appreciated......
No friends this is not a home work. This is asked in Oracle interview, conducted in Hyderabad day before yesterday '24th July, 2010',. He asked me the table structure.
He even did not asked me the query. He asked me how I will design the table. Please advice me.
id | name | grade | aggregate
This would do the trick, id is your primary key, name is students first last name, grade is what grade he is in and aggregate is aggregate % based on the grade.
Fro example some rows might be:
10 | Bill Cosby | 10 | 90
11 | Jerry Seinfeld | 4 | 60
Bill Cosby would have aggregate percent of 90 in grades 1-9, and jerry would have 60 in grades 1-3. In this case it is one table and boils down to you managing the rule of aggregation for this table, since it has to be one table.
If this is an interview question, it looks like they would like to check your knowledge on Nested Tables. Essentially you would have one column as roll number, and other column which is a nested table as Class and Percentage.