Select highest total amount in different tables (Access)
Date : March 29 2020, 07:55 AM
around this issue I have 2 tables: "sales" and "services". Both tables have these fields: customer and amount , I think a union query would be most appropriate: SELECT Top 1 Customer, Sum(Amount) As Total FROM
(SELECT Customer, Amount, Date FROM Sales
UNION ALL
SELECT Customer, Amount, Date FROM Services)
WHERE Date Between Date() AND Date()-30
GROUP BY Customer
ORDER BY Sum(Amount) DESC
|
Select a record with highest amount by joining two tables
Tag : sql , By : littlefuzz
Date : March 29 2020, 07:55 AM
wish of those help I've 2 tables Sales & Purchase, Sales table with fields SaleId, Rate, Quantity, Date, CompanyId, UserID. Purchase table with fields PurchaseId, Rate, Quantity, Date, CompanyId, UserID. , Try: select top 1 * from
(select SalesId ID, Rate, Quantity, 'Sales' TransactionType
from sales
union all
select PurchaseId ID, Rate, Quantity, 'Purchase' TransactionType
from purchase)
order by Rate * Quantity desc
|
Select statement to show the corresponding user with the lowest/highest amount?
Date : March 29 2020, 07:55 AM
I wish this helpful for you This seems like good use of window functions. I've assumed a column vb_bids.bid_user_id. If there's no link between a bid and a user, you can't answer this question With x as (
Select
b.bid_item_id,
count(*) over (partition by b.bid_item_id) as number_of_bids,
row_number() over (
partition by b.bid_item_id
order by b.bid_amount desc
) as high_row,
row_number() over (
partition by b.bid_item_id
order by b.bid_amount
) as low_row,
b.bid_amount,
u.user_firstname + ' ' + u.user_lastname username
From
vb_bids b
inner join
vb_users u
on b.bid_user_id = u.user_id
Where
b.bid_status = 'ok'
)
Select
i.item_name,
i.item_reserve,
min(x.number_of_bids) number_of_bids,
min(case when x.low_row = 1 then x.bid_amount end) lowest_bid,
min(case when x.low_row = 1 then x.username end) low_bidder,
min(case when x.high_row = 1 then x.bid_amount end) highest_bid,
min(case when x.high_row = 1 then x.username end) high_bidder
From
vb_items i
inner join
x
on i.item_id = x.bid_item_id
Where
i.item_sold = 'no'
Group By
i.item_name,
i.item_reserve
Order By
i.item_reserve
|
Select all records from A and the respective amount of related records from B in MySQL
Tag : mysql , By : FuzzyHornet
Date : March 29 2020, 07:55 AM
it should still fix some issue USE LEFT JOIN and Count the B.id, if you dont get match you will have null and count will return 0 SELECT A.*, COUNT(B.id)
FROM TableA A
LEFT JOIN TableB B
ON A.id = B.id
|
Select records with highest salary from duplicate records
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Select employee record with highest salary from duplicate records with same name and different salary , Using NOT EXISTS SELECT s1.id, s1.name, s1.salary
FROM salaries s1
WHERE NOT EXISTS
(
SELECT *
FROM salaries s2
WHERE s1.name = s2.name AND s1.salary < s2.salary
)
SELECT s1.id, s1.name, s1.salary
FROM salaries s1
WHERE s1.salary >=
ALL(
SELECT salary
FROM salaries s2
WHERE s1.name = s2.name
)
|