Ascending and Decending ORDER BY clauses in a single query [closed] - sql

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 .

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.

How to eliminate false results [duplicate]

This question already has answers here:
Q: How to exclude persons name from a table
(2 answers)
Closed 1 year ago.
I am trying to use bigquery to extract data about 10 most mentioned personalities in the leading newspapers in Israel using this code:
SELECT
person,
COUNT(1) AS count_mentions,
COUNT(DISTINCT url) AS count_distinct_urls
FROM
`composed-hold-309910.dataset_1.israel_media_person`
GROUP BY
person
ORDER BY
count_mentions DESC
LIMIT
10;
Unfortunately, some of the results iv'e gotten were not actual people but some "buzzwords" like 'Maccabi Haifa' and 'Gaza Gaza'
person
count_mentions
count_distinct_urls
Benjamin Netanyahu
32965
20660
------------------
----------------
--------------------
Maccabi Haifa
16528
5947
------------------
----------------
--------------------
Gaza Gaza
13267
7623
I would be delighted to find a way to eliminate these false results.
Matan
You can filter out these buzzwords by using where person not in ([list_buzz_word])

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

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