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.
Related
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
In a DB2 Database, I want to do the following simple mathematics using a SQL query:
AvailableStock = SupplyStock - DemandStock
SupplyStock is stored in 1 table in 1 row, let's call this table the Supply table.
So the Supply table has this data:
ProductID | SupplyStock
---------------------
109 10
244 7 edit: exclude this product from the search
DemandStock is stored in a separate table Demand, where demand is logged as each customer logs demand during a customer order journey. Example data from the Demand table:
ProductID | DemandStock
------------------------
109 1
244 4 edit: exclude this product
109 6
109 2
So in our heads, if I want to calculate the AvailableStock for product '109', Supply is 10, Demand for product 109 totals to 9, and so Available stock is 1.
How do I do this in one select query in DB2 SQL?
The knowledge I have so far of some of the imagined steps in PseudoCode:
I select SupplyStock where product ID = '109'
I select sum(DemandStock) where product ID = '109'
I subtract SupplyStock from DemandStock
I present this as a resulting AvailableStock
The results will look like this:
Product ID | AvailableStock
109 9
I'd love to get this selected in one SQL select query.
Edit: I've since received an answer (that was almost perfect) and realised the question missed out some information.
This information:
We need to exclude data from products we don't want to select data for, and we also need to specifically select product 109.
My apologies, this was omitted from the original question.
I've since added a 'where' to select the product and this works for me. But for future sake, perhaps the answer should include this information too.
You do this using a join to bring the tables together and group by to aggregate the results of the join:
select s.ProductId, s.SupplyStock, sum(d.DemandStock),
(s.SupplyStock - sum(d.DemandStock)) as Available
from Supply s left join
Demand d
on s.ProductId = d.ProductId
where s.ProductId = 109
group by s.ProductId, s.SupplyStock;
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 .
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 two table as follows:
employ_offical:
id name salary
1 raj 5000
2 ram 8000
3 balu 3000
4 david 4000
employee personal:
name location
raj india
ram china
balu india
david china
From these two tables, I need to know how to retrieve who gets maximum salary in every location; in one location one person should come under these category.
How to retrieve who gets maximum salary in every location?
Here is one way to solve this:
select p.location, p.name, o.salary
from employ_offical o,
employee_personal p
where o.name = p.name
and o.salary = (
select max(o2.salary)
from employ_offical o2,
employee_personal p2
where o2.name = p2.name
and p2.location = p.location);
There is a SQL Fiddle here.
Please note that I have joined on name here as it is the only join available, but from a design perspective it would be much better to have id in both tables and join on id.
This should work:
SELECT eo.name, ep.location, eo.salary
FROM employ_offical eo
INNER JOIN employee_personal ep ON eo.name = ep.name
WHERE eo.salary = (SELECT MAX(salary)
FROM employ_offical eo1
INNER JOIN employee_personal ep1 ON eo1.name=ep1.name
WHERE ep.location = ep1.location
)
It might not be the most efficient or pretty way to do this though.
Note that if multiple persons in the same location share the max salary for that location this query will return all of them.
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have a table with several columns such as "pcode", "completion in %" and "pstatus".
For example:
pcode P_Name completion subordinate pstatus
p123 inventory 10% sanjay progrssing
p123 test 20% komal progrssing
p124 asd 20% ritika progrssing
p124 qwsdf 10% asd progrssing
I want to get result with different code with highest complete% with other column. All field are text. I'm using asp.net using c# and msaccess database.
What I'm looking for as a result is:
pcode P_Name completion subordinate pstatus
p123 test 20% komal progrssing
p124 asd 20% ritika progrssing
On the database side, you'll have to filter your data. Following query should help you with that
SELECT t1.[pcode], t1.[P_Name], t.[completion], MAX(t1.[subordinate]) [subordinate], [pstatus]
FROM [YOUR_TABLE] t1
JOIN
(SELECT [pcode], max([completion]) [completion]
FROM [YOUR_TABLE]
GROUP BY [pcode]) t
on t.[pcode] = t1.[pcode] and t.[completion] = t1.[completion]
GROUP BY t1.[pcode], t1.[P_Name], t.[completion], t1.[pstatus]
Note: Concerning the binding part of your question, you'll have to provide more information if you're wiling to get (precise) answers.