pad/truncate strings [closed] - sql

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

Related

How to sort by proximity? [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 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

How to write SQL Query for this condition? [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
I have following table in my database
---------------------
|ID |Date
---------------------
|15 |2015-07-01
|15 |2015-07-02
|15 |2015-07-03
|18 |2015-07-04
|18 |2015-07-05
|22 |2015-07-06
|22 |2015-07-07
|22 |2015-07-08
I am writing this query
select * from table where date = "2015-07-04";
by this code i can get current id but how can i select next id to my current id.
I know only date and want to retrieve data.
like i have date=2015-07-03 and on that date id=15
so i want result containing all rows related to id=15 and also id next to id=15 that is id=18;
Not Sure.. But, seems like you are looking for this
select max(id)+1 from yourTable;
If you are using Oracle, then create SEQUENCE and use NEXTVAL to get next sequence value. If you are using SQL Server, then you can select AUTO INCREMENT option.

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]

SQL code for SUM AND MAX [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following table, and want to write a SQL statement to summarize every Article and Extract Maximum? :
| A | 3 |
| B | 6 |
| A | 4 |
Output: A=7, B=6.
select columnname,sum(columnname),max(expression/columnname)
from tablename
group by columnname
Something like this;
SELECT Article, SUM(Num_Column)
FROM Table_Name
GROUP BY Article
ORDER BY Article

Testing a legacy code [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm following a course in agile practices and I have a homework. What they taught me, is that before changing the code (refactoring or adding functionality) I should add some test, in order to gain confidence and be sure that I will not change the behavior while refactoring. This is clear and makes sense, but what if the code is not testable without doing some refactoring first?
Simple example:
public class Summation
{
private int addend1;
private int addend2;
public Summation(int addend1, int addend2)
{
this.addend1 = addend1;
this.addend2 = addend2;
}
public int doSum()
{
System.out.println(addend1 + addend2);
}
// Getters/setters
}
I would like to run an acceptance/integration test using FIT, and check that the following table is verified:
----------------------------
| addend1 | addend2 | result |
----------------------------
| 1 | 1 | 2 |
----------------------------
| 1 | -1 | 0 |
----------------------------
| -1 | 1 | 0 |
----------------------------
| -1 | -1 | -2 |
----------------------------
But, because the function doSum() prints the sum to the standard output stream, it's difficult to write a test for it (I prefer to avoid to intercept the standard output). It would make more sense to slightly refactor the code in order to have a method that returns the sum. But because I'm technically "not allowed" to change the code before writing any test, this is not recommended.
Can you give me any hints? How would you proceed?
Thank you!
Sounds similar to this question.
Whether or not you're allowed to change the code, you're forced to intercept standard output stream. In any case, it's a part of the behaviour.
You could write a test.cpp file to call Summation.Summation(x,y) with various values for x and y in your table and verify/record the results. Make sure the results are what you expect. Next, you may now edit the Summation class and run your tests again (from test.cpp) to ensure the values are the same as they were initially.