I am creating a spare part management database in Microsoft Access. I have two table which are ItemTable and EntryTable. ItemTable holds information about each item with unique ItemID and EntryTable holds information of each items usage. I need to calculate the total stock left for each items based on the usage.
So as you can see, for the ItemID with 2, i need to calculate the total stock left based on the usage of In or Out of Status field.
If status is In then plus elseif status is Out then minus. Then total the stock of ItemID 2. Thus the total stock for ItemID 2 will be 3. I have figured out by total and group by for the ItemID but i cannot figure out the way to subtotal based on condition from other column. Thank you.
You can do it with conditional aggregation:
select itemid,
sum(iif(status = 'In', 1, -1) * quantity) as total
from entrytable
group by itemid
Related
I have a matrix that's grouped by different company brands. Essentially, I'm showing the highest value spent by a customer for each company brand (I've used MAX in my expression to get those). I also want to show Customer IDs attributed to those MAX values. Does anybody know how to do that? Is there any expression?
Example:
Company Brand: Nike | Max value spent: £500 | Customer ID: ???
Thanks
You can use subqueries to retrieve summed values in your SQL Query
SELECT T1.CustomerID, T1.CompanyBrand,
(SELECT Max(S1.ValueSpentField) FROM TableThatContainsValueSpent S1 WHERE T1.CustomerID = S1.CustomerID)
FROM Company T1
Everything looks in order for the code to run, but the code isn't showing any rows. So, i must be doing something wrong. I can't figure out where the problem is. The problem from my instructor is:
Use the AP database
Vendor name from the vendor table
Vendor Contact First Name and Vendor Contact Last Name from the vendor table but I need them concatenated together with a space between the names
Invoice Date from the invoice table
Only the day of from the Invoice Date
Invoice Number from the invoice table
The third and fourth characters from the Invoice Number
Line Item Amount from the invoice line items table
Calculate the commission which is the Line Item Amount * 15% and make it ONLY two decimals
The FROM is the Vendors table do JOINS on the others
The WHERE is where the Line Item Amount is greater than $2000 and the Invoice Date is between July 1, 2014 and July 31st, 2014 and I only want the ones that have either a 9 or a 2 in the Invoice Number
Sort by Invoice Date
// This is the only information he's given me for this assignment. ^
I've tried to join different columns together in different orders/joining tables in different orders.
select vendor_name as vendor, concat(first_name, ' ', last_name) as Contact,
DAY(invoice_date) as ofday, invoice_date,
SUBSTRING(invoice_number, 3,2) as thirdFourth,
line_item_amount,round(line_item_amount*.15, 2) as Commission
from vendors v
JOIN vendor_contacts vc
ON v.vendor_id = vc.vendor_id
JOIN INVOICES INV
ON VC.VENDOR_ID = INV.VENDOR_ID
JOIN invoice_line_items ILI
ON INV.invoice_id = ILI.invoice_id
where line_item_amount > 2000
and invoice_date between '2014-7-1' and '2014-7-31'
and (invoice_number =9 OR invoice_number =2)
expected results :
https://imgur.com/E81ix3o
At the moment you are asking specifically for invoice_number 9 or 2. You don't have an invoice_number of 9 or 2 If you want invoice_numbers with 9 or 2 in you will need
invoice_number like '%9%' or invoice_number like '%2%'
I need to write an SQL Query for MySQL Database which gives me the data according to the necessary conditions
I have a table having structure like this
id: orderId
amount : itemAmount
rate : itemRate
time : orderTime
Now I have to select all the rows having same rates and then have to add all the items having same rates. This should work for each rate means for eg.
row with id 1 is having rate $20 with 2 items and row 3 is having rate $20 with 5 items
row with id 2 is having rate $40 with 4 items and row 4 is having rate $40 with 7 items
I should get result is
rate: $20 totalItem : 7
rate: $40 totalItem : 11
As I stated in my comment, I don't know what your columns are called, I'll assume in my answer they're called id, amount, rate and time.
SELECT rate, COUNT(*) AS totalItems -- Select the rate, and count everything
FROM theRelevantTable -- Use the rows in this table
GROUP BY rate -- But do the selection per group.
In clearer English:
Group the rows in theRelevantTable by their rate, then, for every group, give me the rate and count how many rows there are in the group.
I hope this is what you meant. I do not entirely understand your question.
maybe this? SQL FIDDLE to play with
ASSUMPTIONS: you have a table called orders with columns rate, amount, time, id.
amount stores a quantity of items, rate is the price they are at, and time is when it occurred.
SELECT
rate,
SUM(amount)
FROM orders
GROUP BY rate
I have a stock_item table, which houses all stock that has come and gone through my stock system.
Each stock item can be in one of many states, like: 0 ( not_yet_allocated ), 1 ( allocated ), 3 ( returned )... etc.
I am trying to create a notification system that will let me know when I am running low on stock for a specific stock type.
Here is the SQL I have thus far,
SELECT name, status, count(id) AS count
FROM stock_item
WHERE status IN (0,3) /* Items that are not yet allocated (0), or items that have been returned (3) */
GROUP BY name, status
ORDER BY count ASC;
The above SQL works fine, the only problem is that it does not display a stock type if the WHERE clause is not met, in other words, even though there are no items "in stock" for a certain type, I still need that type to be returned with a count of 0.
You need conditional aggregation to see all the items:
SELECT name, status, sum(case when status IN (0,3) then 1 else 0 end) AS count
FROM stock_item
GROUP BY si.name, si.status
ORDER BY count ASC;
I have a table Usage and it contains the following columns
sl_No
usage_ID
energyItem_ID
qty
unit_ID
location_ID
p_Rate
Sometimes the same EnergyItem might be located at different locations..
During those conditions how can I get the sum of qty of an individual energyItem..
How to get the sum of the qty of energyItems?
If I've understood correctly, you're trying to find the quantity of each
energy item, regardless of its location, using information in a single table.
The following query will give you the energyItem_ID of each item followed by the total quantity of each item:
SELECT energyItem_ID,Sum(qty) as TotalQuantity
FROM Usage
GROUP BY energyItem_ID
ORDER BY energyItem_ID
If, on the other hand, you wanted the quantity of each energy item, broken down by location, you would need the following:
SELECT location_ID,energyItemID,Sum(qty) as QuantityByLocation
FROM Usage
GROUP BY location_ID,energyItemID
ORDER BY location_ID,energyItemID
The order by clauses make the result easier to follow, but are not strictly necessary.
Finally, the answer by marc_s will give you the quantity of a specific energyItem.
How about:
SELECT EnergyItem_ID, SUM(qty)
FROM dbo.Usage
WHERE EnergyItem_ID = 42 -- or whatever ....
GROUP BY EnergyItem_ID
Or what are you looking for?? The question isn't very clear on the expected output....
select a.usage_ID , b.sum(p_Rate) as total from Table_1 a
inner join Table_2 as b on a.usage_ID = b.usage_ID
group by a.usage_ID