I have two tables. One is stock_by_month which keeps records on bookId, stockvalue and reportId. The second table is book; which keeps records on BookId,BookTitle and bookprice.
The reportId is unique for every different month, so for 2010 october is 1, 2010 november is 2 and 2010 december is 3
The Task is I want to generate a report which will include book details and also give the stock value of current month(reportid 3) and the previous month(reportId 2).
I am new to this sort of complexity. I am not sure if that is possible. if it is, I will be very grateful for any help. Thanks
Sounds like you want to use a join. There are plenty of tutorials on SQL out there, so read up on joins and you should see how you can implement your report.
You should join the two tables to get all the information, then group by the reportid. Below is some SQL to get you started but definitely read up on joins as Cameron suggested, and also on 'group by'
SELECT m.bookId, m.stockvalue, m.reportId, b.BookId, b.BookTitle, SUM(b.bookprice) AS total
FROM stock_by_month m
JOIN book b
ON b.PRIMARY_KEY = m.FOREIGN_KEY
GROUP BY m.reportId, m.bookId, m.stockvalue, b.BookId, b.BookTitle, b.bookprice
ORDER BY m.reportId DESC
Related
Dear stack overflow community,
This is my first post so please bear with me :)
I need to solve a SQL problem for a friend of mine.
He is running a web shop and wants to create a finance report.
The application he is using provides such functionality using an interface were MySQL queries can be executed.
I already created most of the report with my (limited) SQL knowledge, however I am struggeling to solve the last problem.
The goal of the report is to UNION and JOIN several tables to get an overview of all commissions, invoices and proposals with their respective articles and prices.
So what I did so far:
I did a union of commissions, invoices, proposals (lets call them receipt) and joined them with articles and prices.
That worked very well.
However, here is my problem:
An article could have multiple prices depending on the date of the respective receipt.
So I end up with more rows in my table as there should be.
There is a "valid_until" field within the prices table, which I have to use for the filter ... but how?
Example:
receipt_id
receipt_date
article_id
article_price
valid_until
price_id
209986-1
2020-09-10
2925
13
2020-12-06
1
209986-1
2020-09-10
2931
13
2020-09-09
2
209986-1
2020-09-10
2937
12,6
2020-09-12
3
209986-1
2020-09-10
2980
12,32
0000-00-00
4
In this case, only price_id 3 is valid as the receipt_date is "2020-09-10".
My Query (with limited SQL knowledge):
SELECT *
FROM (SELECT * FROM commissions UNION ALL SELECT * FROM invoices UNION ALL SELECT * FROM proposals) AS receipt
LEFT JOIN article ON receipt.article = article.id
LEFT JOIN prices ON article.id = prices.artikel
WHERE receipt.date <= IF(prices.valid_until = '0000-00-00', Date('3000-01-01'), prices.valid_until)
With that query I still get 3 results (price_id 4, 3 and 1).
I managed to identify the valid price using DATEDIFF(), ORDER BY and LIMIT, however MySQL does not allow to use LIMIT in sub-queries :(
Any help would be much appreciated.
KR,
Wlad
I have a practice problem where I am to write a query to find the top most 15 percent profitable products in the year 2005 from a database. The database does NOT have attributes like "Saleprice, or Purchaseprice". It has tables like PUrchaseProductDetails or SalesOrderDetails, and other stuff with Unitprice, orderquantity, ProdID, LIstPrice, ActualCost, StandardPrice, etc as attributes. I am confused as to which one I should use and how to come up with a formula. I tried to write a query, but got infinitely running results.
SELECT A.ProdID, B.ProdID, A.Unitprice - (B.Unitprice * orderquantity) Profit
FROM SalesOrderDetails A join PurchaseOrderD B
ON A.ProdID = B.ProdID
WHERE year(DateOrdered) = 2005
Group by A.ProdID
I have spent hours on these type of questions and my brain is at a dead end right now. If someone can please direct me to do it the right way, it would really help me out.
SELECT sale.ProdID, sum(sale.Unitprice - buy.Unitprice) * sale.Qty AS profit
FROM SalesOrderDetails AS sale
JOIN PurchaseOrderD AS buy ON ...
WHERE year(...) = 2005
GROUP BY 1
ORDER BY 2 DESC
LIMIT 15
I have been playing around with this for what seems like hours and I can't get the results I want. Here is the query I am having trouble with:
SELECT year.year, dstate,
(SELECT sum(amount) FROM gift
WHERE year.year = gift.year
AND gift.donorno = donor.donorno)
FROM donor, gift, year
WHERE year.year = gift.year
AND gift.donorno = donor.donorno;
This seems redundant. Anyway, I am trying display the total donations (gift.amount) for each state by year.
ex.
1999 GA 500 (donorno 1 from GA donated 200 and donorno 2 from GA donated 300)
1999 FL 400
2000 GA 600
2000 FL 500
...
To clarify donors can be from the same state but I am trying to total the gift amounts for that state for the year it is donated.
Any advice is appreciated. I feel like the answer is right in front of me.
Here is a picture of tables for reference:
This is a very simple join & aggregation problem.
SELECT y.year, d.state, SUM(g.amount) AS total
FROM gift AS g
INNER JOIN year AS y ON y.year=g.year
INNER JOIN donor AS d ON d.donorno=g.donorno
GROUP BY y.year, d.state
You don't need the sub-query in your SELECT clause in order to get the total amount. You can sum it by grouping. (I think the GROUP BY clause is what you're missing. I recommend reading up on it.) What you've done is called a correlated sub-query and it is going to be very slow over large data sets because it has to be calculated row-by-row instead of as a set operation.
Also, please don't use the old style comma join syntax. Instead use the explicit join syntax as shown above. It is much clearer and will help avoid accidental Cartesian products.
Evening folks,
I have a complex MySQL COUNT query I am trying to perform and am looking for the best way to do it.
In our system, we have References. Each Reference can have many (or no) Income Sources, each of which can be validated or not (status). We have a Reference table and an Income table - each row in the Income table points back to Reference with reference_id
On our 'Awaiting' page (the screen that shows each Income that is yet to be validated), we show it grouped by Reference. So you may, for example, see Mr John Smith has 3 Income Sources.
We want it to show something like "2 of 3 Validated" beside each row
My problem is writing the query that figures this out!
What I have been trying to do is this, using a combination of PHP and MySQL to bridge the gap where SQL (or my knowledge) falls short:
First, select a COUNT of the number of incomes associated with each reference:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `income`.`status` = 0
GROUP BY `reference_id`
Next, having used PHP to generate a WHERE IN clause, proceed to COUNT the number of confirmed references from these:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `reference_id` IN ('8469', '78969', '126613', ..... etc
AND status = 1
GROUP BY `reference_id`
However this doesn't work. It returns 0 rows.
Any way to achieve what I'm after?
Thanks!
In MySQL, you can SUM() on a boolean expression to get a count of the rows where that expression is true. You can do this because MySQL treats true as the integer 1 and false as the integer 0.
SELECT `reference_id`,
SUM(`status` = 1) AS `validated_count`,
COUNT(*) AS `total_count`
FROM `income`
GROUP BY `reference_id`
In SSRS 2005 I am reporting on all available posts by regional office, listed by region,office,vacancyID.
I then display a total per office on how many people started in a particular vacancyID by doing a Count(VacancyStartID).
In the same group row with the Count(VacancyStartID) I need to display SUM(VacancyID).
However at present this does not give the correct SUM, because some vacancies have multiple VacancyStartID's and hence the vacancyID is listed few times, like so:
office vacancyID Number_of_vacancies VacancyStartID (person who started a job)
1 1 2 4567
1 1 2 5678
Totals: 4 (needs to be 2) 2
P.S. Note:These questions are not applicable in this instance:
How can I remove duplicate rows?
How do I remove "duplicate" rows from a view?
Using multiple COUNTs and SUMs in a single SQL statement
If it's in the Underlying SQL Server call...
You can do ...SUM(DISTINCT VacancyID)... like you can COUNT (DISTINCT ..)
Edit:
SELECT
col1, col2, SUM(DISTINCT Number_of_vacancies) as foo, COUNT (VacancyStartID) as bar
FROM
MyView
...
If it's in the table or for a cell in the report, then there is no equivalent in the SSRS SUM function.
Do some grouping already in your query and then make a group with a simple Count in SSRS.