Show IDs for the highest MAX values in a matrix - reporting-services

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

Related

SubTotal Query based on condition of other column

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

mySQL: Given 2 column data, find 3rd column and then AVG() 3rd column based on 2nd column

I've 3 columns, staffName, dateOfIncident, incidentNo. I'm looking for 'total incidents' which will be the total number of incidents from a certain staff for a particular year, which I get from dateOfIncident. Now I must find the average of the number of 'total incidents', finally providing the headings:
staffName | avgIncidents
What I have so far is:
SELECT l.staffName, l.dateOfIncident, COUNT(l.incidentNo) AS avgIncidents
FROM incidentsR l
Which displays:
staffName | dateOfIncident | avgIncidents
.... | ..... | ....
Though this obviously supplies 3 columns output, and so far I'm only able to count the total amount of incidents, which I then need to use to calculate the avg.
What I need help with is how to take information from the first 2 columns of name and date to find the 'total incidents' for that specific person for that year. And then for each staffName calculate the average of the number of 'total incidents' for the years we have data on the staff.
Your query would be:
SELECT l.staffName, year(l.dateOfIncident) as year,
COUNT(*) as incidentsPerYear
FROM incidentsR l
GROUP BY l.staffName, year(l.dateOfIncident);
To get the average per year for the staff, you could use a subquery. Or do the calculation directly:
SELECT l.staffName,,
COUNT(*) / COUNT(DISTINCT year(l.dateOfIncident))as avgIncidentsPerYear
FROM incidentsR l
GROUP BY l.staffName;

multiply two columns from two different tables and group them together

I have two tables in MySQL which are sale and material details.
I want to calculate the profit I made by selling items which is
profit = (total) -(qty* landedcost)
Here is the structure of the two tables:
This is the query
SELECT sale.name ,sale.total-(sale.qty * materialdetails.landingcost) AS
result
FROM sale JOIN materialdetails
on sale.id = materialdetails.id
GROUP BY sale.name,result;
the result i get :
query result
I want something like this
name result
A4 5000
Computer 40000
Flash memory 1000
Headphone 22000
Mobile 35000
Any idea please?
You should sum the result and group by sale.name only, something like this:
SELECT sale.name ,sum(sale.total-(sale.qty * materialdetails.landingcost)) AS
result
FROM sale JOIN materialdetails
on sale.id = materialdetails.id
GROUP BY sale.name;
Explanation: if you group by two fields GROUP BY sale.name,result you will get one line for all records that have the same sale.name and result, so for instance
name result
Computer 10000
Computer 25000
are two different lines and they are not grouped together as one.

SQL: Find closest number to given value with ties

I'm trying to find the closest number(s) to a given value in SQL. I have already made my query for multiple results:
SELECT *
FROM Cars
ORDER BY ABS(price - $price)
I know I can limit the table by using LIMIT 1, by which I have one number closest to the given value. But how can I include ties? Like for example when there are three or four cars with the same price? The amount of cars which have the same price is dynamic, so I can't specify a certain LIMIT.
I also know I could use SELECT TOP 1 WITH TIES, but I can't use this query because my database driver doesn't allow it. Does anybody have another idea of how to accomplish this?
Example:
car 1 = 2000
car 2 = 3000
car 3 = 3000
car 4 = 1500
When I want the cars closest to 3000, the query should return:
car 2
car 3
But without using a static LIMIT in the query, because the amount of cars with the same price can be different every time. Thanks
If your driver supports nested queries:
SELECT *
FROM CARS
WHERE ABS(price - $price) = ( SELECT MIN(ABS(price - $price)) FROM CARS )

Finding The Sum OF Two Values In SQL Server 2008

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