Members Table: Column: member_id, firstname, lastname, branch_id
Branch Table: Column: branch_id, name etc ```
I want to display total number of members per branch.
Example:
BranchName Number of users
Durban 1000
Cape Town 2000
Mpumalanga 2500
Assuming you have a relation from branch to members you could do the below.
#foreach ($branches as $branch)
<div>Name: {{ $branch->name }} </div>
<div>Members: {{ {{ $branch->members->count() }}</div>
#endforeach
Related
I have an ecommerce site with 4 tables
(Users, Vendors, Products and Orders).
Vendor here means a shop.
A User is a person and can own many different Vendors.
These Vendors can have many different products (Vendor hasMany Product relationship and a Product belongsTo Vendor relationship).
The orders table saves all transaction information when a customer buys.
Now we make our money via commission for each sale that happens on the eCommerce site.
The commission is calculated for each product based on the percentage rate of the Vendor(Shop) it is found under. These rates are stored in the Vendors table.
In my products table I have
id
product_name
product_price
product_image
product_file
product_description
product_level
vendor_id
In my Vendors table I have:
id
user_name
vendor_email
vendor_name
rate
And in my orders table I have:
id
customer_name
customer_tel
product_name
product_price
product_id
vendor_email
transaction_id
payment_status
Key things to note: Vendor Email is unique. vendor_id in the products table is the id field in the vendors table etc.
Now, let's take a user John who has 3 Vendors (A, B and C) and 5 products each under these vendors.
For Vendor A, we have products A1, A2, A3 etc same for Vendor B (B1, B2 ..) and Vendor C (C1, C2 etc).
Let's say John has a 5% commission rate to shop A, a 10% rate to shop B and 0% rate to shop C.
Vendor A: rate = 5%.
Products = [A1, A2, A3, A4, A5]
Vendor B: rate = 10%.
Products = [B1, B2, B3, B4, B5]
Vendor C: rate = 0%.
Products = [C1, C2, C3, C4, C5]
This means if product A1 (found in Shop A) cost $10 and it is bought we get 5% of the sale:
5/100 * $10 = $0.5
and John gets 95% which is
$10 - $0.5 = 9.5$.
Let's take another product B2 (found in Shop B with a rate of 10%) and cost $100. this means we get %10 of each sale and John gets 90%.
that is: 10/100 * $100 = $10 for us and John gets $100 - $10 = $90.
Let's take a final product c1 (found in shop C with a rate of 0%) and cost $200. This means when this product is bought, John gets all of the money.
Now for this three products, if John was to calculate his total income, this will be:
$9.5 + $90 + $200 = $299.5
and our total commision will be:
$0.5 + $10 + $0 = $10.5
I want to be able to display this data to the user. So fat, I am able to get the collection using eloquent in my controller and display the data to the user.
In my controller I have:
public function index()
{
$myOrders = ['vendor_email' => Auth()->User()->email, 'payment_status' => 'SUCCESSFUL'];
$orders = Order::where($myOrders)->orderBy('created_at', 'desc')->paginate(10);
$products = Product::with('vendor')->get();
$totalOrders = Order::where($myOrders)->count();
return view('admin.orders', compact('orders', 'products', 'totalOrders'));
}
And in my view, to get the amount John makes for each product, I use a Foreach loop in my blade:
Total: {{totalOrders}}
#foreach($orders as $order)
#if(Auth()->User()->email == $order->vendor_email)
Product Name: {{$order->product_name}}
Product Price: {{$order->product_price}}
#foreach($products as $product)
#if($order->product_id == $product->id)
Rate: {{$product->vendor->rate}}
Income: {{$order->product_price * $product->vendor->rate}}
Vendor: {{$product->vendor->vendor_name}}
#endif
#endforeach
#endif
#endforeach
This works perfectly as it returns the correct figures for each product which has been bought. If we take our example above, it will return
Total: 3
Product | Price |Rate |Income |Vendor
A1 | $10 |5% |$9.5 |A
B2 | $100 |10% |$90 |B
C1 | $200 |0% |$200 |C
My biggest problem is how to dynamically get the total of the income from the controller and displaying it as a variable in the Blade View. I want to be able to get this value in the controller so I can do calculations on it, say the User wants to withdraw part of his income, I should be able to deduct the withdrawal amount from the total income and display what's left etc.
I've been stuck with this problem and I have done a lot of googling but can't seem to find any solution. I got the total count of the products of each user by:
$totalOrders = Order::where('vendor_email', Auth()->User()->email)->where('payment_status', 'SUCCESSFUL')->count();
From that I can display this total orders of each user by simply calling this variable in blade:
Total: {{$totalOrders}}
My question now is how do I calculate the total income of the user: that is taking the price of each product sold and multiplying it by the commision rate of the Vendor shop so I can have some variable such as:
Total Income: {{some Variable}}
I really need help with this.
Order Model
public function product(){
return $this->belongsTo(App\Product::class, 'product_id', 'id');
}
Product Model
public function orders(){
return $this->hasMany(App\Order::class, 'order_id', 'id');
}
public function vendor(){
return $this->belongsTo(App\Vendor::class, 'vendor_id', 'id');
}
Controller
// These are orders for the logged in user.
$orders = Order::with('product.vendor')->where([
'vendor_email' => Auth()->User()->email, 'payment_status' => 'SUCCESSFUL'])
->get();
// Their income should be (100 - rate)% since the rate is the commission you get // as you stated in your post.
$totalIncome = $orders->sum(function ($order) {
return ($order->product->product_price * (100 - $order->product->vendor->rate);
});
$totalOrders = sizeof($orders);
return view('admin.orders', compact('orders', 'totalIncome', 'totalOrders'));
Blade
Total Orders: {{totalOrders}}
#foreach($orders as $order)
Product Name: {{$order->product->name}}
Product Price: {{$order->product->product_price}}
Rate: {{$order->product->vendor->rate}}
Income: {{$order->product->product_price * (100 - $order->product->vendor->rate)}}
Vendor: {{$order->product->vendor->vendor_name}}
#endforeach
Total Income: {{totalIncome}}
The situation. I am trying to make a report that lists the sales rep names (all of them; there are 50), the sales rep's state, and the total sales broken down by YYYY-MM for that state. If there are two or more sales reps in the state, they should each be listed with the same information for their state. How the final list is ordered does not matter, so long as all the information is included.
The problem. I also need totals by state in addition to the totals I have.
Here is my code:
SELECT
dim_sales_rep.sales_rep_name as 'Sales Rep',
dim_state.abbreviation as 'State',
date_format(dim_date.date, '%Y-%m') as 'Year-Month',
concat('$',sum(fact_sales.total_sales)) as 'Sales'
FROM
(
(
dim_sales_rep
JOIN dim_state ON dim_sales_rep.state_key = dim_state.state_key
)
JOIN fact_sales ON dim_sales_rep.sales_rep_key = fact_sales.sales_rep_key
)
JOIN dim_date ON fact_sales.date_key = dim_date.date_key
GROUP BY dim_date.year, dim_date.month, dim_state.abbreviation, dim_sales_rep.sales_rep_name
Sample Output:
Rep State Year-Month Sales
Michele Harris GA 2010-08 $679.79
T.S. Eliot GA 2010-07 $2938.74
It should look like this:
Rep State Year-Month Sales
Michele Harris GA 2010-08 $679.79
Georgiana Woe GA 2010-08 $482.98
State total $1162.77
Or like this:
Rep State Year-Month YM Total State Total
Michaele Harris GA 2010-08 $679.79 $1162.77
Georgiana Woe GA 2010-08 $482.98 $1162.77
Here is the data structure:
table fact_sales
date_key (PK) Surrogate Key
account_key (PK) Surrogate Key
sales_rep_key (PK) Surrogate Key
total_sales Total sales dollars.
count_of_products Number of products sold
table dim_state
state_key (PK) Surrogate Key
abbreviation e.g. AL or CA
name e.g. California
table dim_account
account_key (PK) Surrogate Key
account_name
account_address
state_key Surrogate Key
effective_date Starting date that this record is active
expiration_date Ending date that this record is active
is_current Represents the active record
table dim_sales_rep
sales_rep_key (PK) Surrogate Key
sales_rep_name
state_key Surrogate Key
effective_date Starting date that this record is active
expiration_date Ending date that this record is active
is_current Represents the active record
table dim_date
date_key (PK) Surrogate Key date e.g. 2011-01-01
month e.g. 01
year e.g. 2011
Notes:
PK: Denotes that the column is the primary key or is part of the primary key of the table.
Surrogate Keys are represented as numeric and do not represent actual values from an application. For example date_key could be 1,2,3,4, etc. and is not a real date.
Assume that dim_date contains all dates for all time.
Assume that if the column has the same name in a different table that they are equivalent.
When you group by just the state, you get one row per state in your result set. That's what GROUP BY means: aggregate your data broken out by the column values it mentions.
Use this:
GROUP BY dim_date.year, dim_date.month, dim_state.abbreviation, dim_sales_rep.sales_rep_name WITH ROLLUP
And, formatting your dates well can be done using MySQL's date functions.
Try this:
SELECT DATE_FORMAT(STR_TO_DATE(CONCAT_WS('-',2014,3,1),'%Y-%m-%e'),'%Y-%m')
I have 4 tables
customers
sales
sale_items
stock_items
I want to list all my customers. For each customer I want the total amount purchased - a field that I want the sql query to create (xxx as totalSales)
I tried selecting customers as the primary table and joining other tables on it. I tried selecting sales as the primary table and also the sale_items. I kind of got the correct sum calculation but it will show me the first customer first.
This is what my tables look like:
*TABLE customers*
id
customer_name
email
*TABLE sales*
id
customer_id
transaction_date
*TABLE sale_items*
id
sale_id
stock_item_id
*TABLE stock_items*
id
item_name
price
I want to
Create a list of all customers, sorted by the customer with the most sales (in value) first. Not the count of the sales, but the total amount (sales value) of the sales
Display the items purchased per customer under the customer name. This would not be per order, but for all sales. So if a tin of coffee was purchased by customer X, over a count of 4 orders each, the tin of coffee would display 4 times. Though if possible I'd like the items listed a-z.
I have inner joined the tables on each other, so I would get a list of all transactions. I've tried SELECT * FROM customers, tried FROM sales, tried from sale_items. I've tried GROUP_BY customer_id, but then I would get incorrect counts.
I want to display the data as such.
CUSTOMER ITEMS TOTAL VALUE
John Doe Coffee
Milk
Tea
Milk
Bread
Coffee 500
Jane Doe Coffee
Milk
Coke 350
Denver Doe Coffee
Milk
Bread
Bread 125
I don't want to use PHP and make a query for every single "thing". I am trying to run one or two queries.
I don't think you're going to be able to quite get them in separate rows like you're showing (unless you look into the ROLLUP function), but that's probably not a requirement for you.
I think you should use GROUP_CONCAT which is an aggregation function sort of like SUM but it creates a comma separated list out of all of the values:
SELECT
*
SUM(sale_amount) as total_sales,
GROUP_CONCAT(item_name) as item_names
FROM customers c
JOIN sales s USING (customer_id)
JOIN sale_items si USING (sale_id)
JOIN stock_items sit USING (stock_item_id)
GROUP BY customer_id
What you should see as a sample row is:
Denver Doe Coffee,Milk,Bread,Bread 125
(I had to make up some column names, like sale_amount, but you must have those in there I'm sure. You'll have to make some adjustments in those names and maybe in how I did the joins as well, but this should work with some changes if I'm understanding your needs).
I have a table like so:
Customer Purchase Date Product
Frank 7/28/2015 Hammer
Bob 7/29/2015 Shovel
Bob 7/29/2015 Pickaxe
Bill 7/30/2015 Pliers
The Purchase Date field records a new entry for every purchase. So, if in one visit a customer purchases four items, my database creates four entries each with the same date.
I'm trying to write a query that displays the numbers of visits for each customer. Output like so:
Frank 1
Bob 1
Bill 1
But when I use the COUNT function on the date in my query, it returns:
Frank 1
Bob 2
Bill 1
I want my query to only count unique dates, but the COUNT function doesn't work. Everywhere I read, it also says that the SQL COUNT (Distinct) doesn't work in Access. Access help says that if I set the Query Properties to Unique Values "Yes", it should only return unique values, but it doesn't work. I tried Unique Record "Yes" also, but that didn't work either.
Please help! Thanks!
Try this:
select Cust, count(cust) as CustomerCount
from (Select Distinct Table1.Customer as cust, Table1.PurchaseDate
from Table1)
group by cust
i'm a newbie on mysql. i want to display the largest number of votes in the table as well as the candidate number, last name, first name, and middle name. but when i use the max() function to select the largest number of votes. the largest vote is selected but the candidate number, last name, first name and middle name are the default first values in the database... here's the example:
candidate table:
candidate no last name first name middle name position votes
038-001 banchero chris ace president 99
038-002 castro jayson texk president 100
what i want to display:
038-002 castro jayson texk president 100
my problem is that the first row is always displayed only the highest votes are display. like this:
(038-001 banchero chris ace 100)
thanks. I would really appreciate any help. :)
select * from tablename
where votes = (select max(votes) from tablename)
Will return the row with max number of votes. (And both rows if it's a tie.)