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 8 years ago.
Improve this question
I have the following Oracle database, and I need to return the following:
d.directdomain, d.domaindisplayname, r.lastdate (The latest), and count(how many times, r.directdomain = d.directdomain)
basically, I have lots of "people" in r db, and "domains" in the d. I need to return how many times a person in R visited each "domain", and also return the last time they visited the domain.
I tried a few things, but it seems by using count, i need to GROUP BY the date, so that's confusing me.
Example return:
1, Site1, 21/05/13, 5
2, Site2, 20/05/13, 2
d
directdomain (PK)
domaindisplayname
r
rsld (PK)
lastdate
directdomain (FK)
Are you looking for something like this?
SELECT d.directdomain,
d.domaindisplayname,
MAX(r.lastdate) lastdate,
COUNT(*) rcount
FROM d JOIN r
ON d.directdomain = r.directdomain
GROUP BY d.directdomain, d.domaindisplayname
Sample output:
| DIRECTDOMAIN | DOMAINDISPLAYNAME | LASTDATE | RCOUNT |
|--------------|-------------------|-------------------------------|--------|
| 1 | Site1 | August, 15 2013 00:00:00+0000 | 4 |
| 2 | Site2 | August, 18 2013 00:00:00+0000 | 3 |
Here is SQLFiddle demo
Related
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 2 years ago.
Improve this question
I have 2 tables: team(team_id, name), performance(team_id, stage, place, date). Teams should earn points: for 1st place - 10 points, for 2nd 5, for 3rd - 3 points and 1 point for 4-7 places. I need to output the rating of teams.
I think it should be like:
SELECT team.name, CASE place
WHEN 1 points + 10
WHEN 2 points + 5
...
Expected reasult:
|---------------------|------------------|
| team.name | Points |
|---------------------|------------------|
| Rockers | 34 |
|---------------------|------------------|
| Batmans | 23 |
|---------------------|------------------|
| ... | ... |
|---------------------|------------------|
First aggregate inside the table performance to calculate the total points of each team with conditional aggregation and then join the table team to the results and rank the teams with RANK() or maybe DENSE_RANK() analytical functions:
select t.team_id, t.name,
coalesce(p.points, 0) points,
rank() over (order by coalesce(p.points, 0) desc) rnk
from team t left join (
select team_id,
sum(
case
when place = 1 then 10
when place = 2 then 5
when place = 3 then 3
when place <= 7 then 1
else 0
end
) points
from performance
group by team_id
) p on p.team_id = t.team_id
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 3 years ago.
Improve this question
I'm kind of new in database design and I'm trying to find the best way to solve an issue I'm facing.
Let's think about the following example:
Imagine I want to store information about patients and these patients can have 0+ diseases.
What's the best way of storing arranging the tables to display the diseases that each patient can have? I get confused as to what happens when a patient would have 3 diseases; how is this stored in a relational database? e.g. without having repeated rows on the diseases table for example (static number of diseases)..
I'm not sure if I'm making myself totally clear here!
But let's say I don't think it's efficient to have:
Patient table -
Patient_id , disease_id
1, (3,4,5,6)
Any help is appreciated!
As mentioned in the comments, you would use an associative entity for that.
Let say you have the following diseases:
| Disease_id | Disease_name |
-----------------------------
| 1 | Cancer |
-----------------------------
| 2 | Leucemia |
-----------------------------
Your patients may look like this
| Patient_id | Patient_name |
-----------------------------
| 1 | Peter Jones |
-----------------------------
| 2 | Mark Jacobs |
-----------------------------
You now create a table like so (lets call it ill_patients)
| Patient_id | Disease_id |
---------------------------
| 1 | 1 |
---------------------------
| 1 | 2 |
---------------------------
This would mean that poor Peter Jones has cancer as well as leucemia. You can now query your patient table like so:
SELECT patients.patient_name, diseases.disease_name
FROM (diseases INNER JOIN ill_patients ON diseases.disease_id = ill_patients.disease_id) INNER JOIN patients ON ill_patients.patient_id = patients.patient_id;
This gives you all the patients with their respective diseaes.
Patients:
patient_id, name, phone
Disease:
disease_id, name, description
Patient_Disease:
patient_id, disease_id
Example:
patient_id, name , phone
1 , 'jhon', '555-1234'
disease_id, name , description
1 , 'Cancer' , 'Cancer is the uncontrolled development of cells.'
2 , 'Diabetes', 'Diabetes is a disease that occurs when your blood glucose is too high.'
patient_id, disease_id
1 , 1
1 , 2
Then you can do
SELECT p.name, d.name, d.description
FROM patients p
JOIN patient_disease pd
ON p.patient_id = pd.patient_id
JOIN diseases d
ON pd.disease_id = d.disease_id
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 8 years ago.
Improve this question
Using a database, I have a table with 2 columns: amount of hours worked, and type of work. I want to sum the number of hours for each work type.
For example, my "work" table has 2 columns:
+---------+------------+
| hours | type |
+---------+------------+
| 5 | ABC |
| 7 | DEF |
| 3 | GHJ |
| 9 | ABC |
+---------+------------+
And I need it to return 14 hours for type ABC
I need to use it in a select statement as part of other columns I am pulling.
Any help would be greatly appreciated.
Select type, sum(hours) total_hours from work group by type
SELECT SUM(hours), type FROM work GROUP BY type
Select type, sum(hours) as "amount of hours worked" from work group by type
You can use group by to achieve your result
Select type, sum(hours) total_hours from work group by type
group by class is used to find the sum based on the columns specified after the group by class. You can specify more than one column after the group by.
For more detail: Group By
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I have table structure for competition:
user_id | score
and I need get information 2 rows before (for example) user_id=20 and 2 rows after for show rank table.
I want this result of score table:
order | user_id | score
23 | XY1 | 240
24 | XY1 | 247
25 | 20 | 250 (my specific row)
26 | XY1 | 252
27 | XY1 | 290
with aaa as
(
SELECT
ROW_NUMBER() OVER (ORDER BY score ) AS 'ROW_NUMBER', score, user_id
)
select * from aaa where ROW_NUMBER between
(select ROW_NUMBER-2 from aaa where user_id = 25) AND
(select ROW_NUMBER+2 from aaa where user_id = 25)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
Here is the raw data
Book | Author | Year
A | A1 | 1985
A | B1 | 1985
B | A1 | 1988
B | C1 | 1988
D | A1 | 1990
D | C1 | 1990
D | B1 | 1990
Here is what output I am looking for,
Author1 | Author2 | year | count
A1 | B1 | 1985 | 1
A1 | C1 | 1985 | 1
A1 | C1 | 1988 | 1
A1 | B1 | 1990 | 1
A1 | C1 | 1990 | 1
B1 | C1 | 1990 | 1
Any help is deeply appreciated.
Thanks
The query you are looking for is a self join with an aggregation:
select t1.author as author1, t2.author as author2, t1.year, count(*) as `count`
from t t1 join
t t2
on t1.book = t2.book and
t1.author < t2.author
group by t1.author, t2.author, t1.year
order by t1.author, year;
SELECT A.author AS author1,
B.author AS author2,
A.year,
COUNT(*) AS "count"
FROM Author A
LEFT JOIN Author B
ON B.book = A.Book
AND B.author > A.Author
GROUP BY A.author, B.author, A.year
ORDER BY A.author, B.author, A.year
This will work okay only as long as there are no more than two rows per book in your Author table. Otherwise, it will produce multiple lines per book. If that is possibly the case, you should indicate what flavor of SQL should be used, as the means to limit the results from Table B differ from implementation to implementation. I have arbitrarily chosen to list the authors in alphabetical order, since there appears to be no indicator of which author is "primary."
I would hope that there are come additional columns in the table that you are not telling us about--most specifically a primary key, and perhaps some attribute indicating the "billing order" of the authors with respect to a given book.
You might want to reconsider your table design, if that's possible: it's in a non-normalized form that makes data integrity hard to enforce.