How to write SQL Query for this condition? [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
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.

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

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

Ascending and Decending ORDER BY clauses in a single query [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 8 years ago.
Improve this question
I working on these problems for a class I"m taking, but this one has me stumped. Here is the problem:
--Using the AUTHOR table, write a query that will list all information about authors
--whose first name ends with an “A”. Put the results in descending order of last name,
--and then ascending order by first name. This should be done using a single query.
Here is what I've come up with so far:
SELECT *
FROM author
WHERE(fname LIKE '%A')
ORDER BY lname DESC, fname ASC;
However all I get in the result is the information ordered by last name descending. First name ascending doesn't seem to work.
Any thoughts on what I'm missing? Using Oracle Express 10G, if it matters.
Thanks.
There is nothing wrong with your query. All you have to do is just pay attention to the data :-)
Here is how you would interpret your data output:
--------------+--------------
zzz | john
zza | adam
zaa | bob
ccc | jack
ccc | john
cca | mike
So, ordering works just you instruct Oracle - lname desc, fname acs, but you need to realize that fname asc comes in a picture once lname desc is processed. In other words: ZZZ comes before ZZA , but once CCC is ordered then and only then jack comes before john .

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.