i have 3 tables, i want to get the total sum, but im not getting the right results.
First table(investors)
id investor_id
1 27
2 27
3 29
4 30
5 31
Second table(payments)
id investor_id
1 27
2 27
3 28
4 29
5 30
6 31
7 27
Third table(billed)
id payments_id billed
1 1 189
2 2 300
3 3 500
4 4 700
5 5 200
6 6 300
HERE is my query
SELECT SUM(billed.billed)
FROM billed AS billed
INNER JOIN payments AS payments ON billed.payments_id = payments.id
INNER JOIN investors AS investors ON payments.investor_id = investors.investor_id
instead of getting 1689 only, im getting a results of 2178
i am really stuck with my answer is there really a way to do this just by using query only?
If you just want the total sum for bills, just query that table directly, no need to join:
SELECT SUM(billed) FROM billed
If for some reason you need to JOIN, the I select the distinct investors in a subquery, then join it:
SELECT SUM(billed.billed)
FROM billed AS billed
INNER JOIN payments AS payments ON billed.payments_id = payments.id
INNER JOIN (SELECT DISCTINCT investors FROM investors) AS investors ON payments.investor_id = investors.investor_id
Related
I am working on a query with a join of 5 tables, and also there is parent child relation.
I am trying to query university sales and this is my query. I am struggling to get total sales of university. How should I query this?
I attached category table and invoice table.
Select invoice.id, invoice.total, categories.type, categories.name
FROM invoices
Join sales on sales.invoice_id = invoices.id
Join course on course.id = sales.course_id
Join category_subject on category_subject.subject_id = course.subject_id
Join categories on categories.id = category_subject.category_id
categories table with parent-child relationship.
Here is the categories table:
id
parent_id
name
type
1
null
England
country
2
null
France
country
3
16
A university
university
4
17
B university
university
5
16
C university
university
6
17
D university
university
7
1
E high school
stage
8
2
F high school
stage
9
3
computer engineering
department
10
4
art
department
11
5
chemistry
department
12
7
grade 9
grade
13
7
grade 10
grade
14
8
grade 11
grade
15
8
grade 12
grade
16
1
England- university
stage
17
2
France-university
stage
18
6
business
department
and this is invoice table:
id
total
1
50
2
100
3
350
4
850
5
65
6
75
7
850
8
650
9
250
10
450
11
300
12
100
13
450
14
950
15
350
16
750
17
320
There's not currently enough information included the in the question to really answer it, but start with this and tell us what you're still missing:
SELECT i.id, i.total, ct.type, ct.name
FROM invoices i
INNER JOIN sales s on s.invoice_id = i.id
INNER JOIN course c on c.id = s.course_id
INNER JOIN category_subject cs on cs.subject_id = c.subject_id
INNER JOIN categories ct on ct.id = cs.category_id
WHERE ct.type = 'University'
Additionally, from what I've inferred you want include all invoices for category IDs 3, 4, 5, and 6 -- which are directly Universities -- as well as 9, 10, 11, and 18 -- which are children of the Universities.
This is easy enough to do if you know you only have one level like this. One additional join + a coalesce() in the WHERE clause can do the job, as could a UNION to a similar query. But if you could have arbitrarily more levels the query becomes much trickier; you must use a recursive CTE to check the whole category tree. Also, right now the sample data never shows a University with a parent category. If the actual data allows for this that adds another wrinkle. So again: not enough information in the question to provide a good answer at this time.
I have two tables.
One table records orderstock which has FK_stock and FK_orderNo
I want to count the number of orders each item of stock has. The following code works correctly to do this:
(1)
SELECT orderstock.FK_orderNo, Count(orderstock.FK_stock) AS CountOfFK_stock
FROM stock INNER JOIN orderstock ON stock.StockID = orderstock.FK_stock
GROUP BY orderdemo.FK_orderNo
However, I wish to add to this such that only stock items which are non perishable (stock.perishable=0) are listed. So something like
SELECT orderstock.FK_orderNo, Count(orderstock.FK_stock) AS CountOfFK_stock
FROM stock INNER JOIN orderstock ON stock.stockID = orderstock.FK_stock
WHERE stock.perishable=0
GROUP BY orderstock.FK_orderNo
How do I access information relating to the FK_stock to make this work? When I attempt to combine information from the stock table to this end, each item of stock is counted separately.
Results from (1)
FK_OrderNo CountOfFK_Stock
9 10
104 8
105 3
106 10
107 8
108 10
109 11
110 9
Desired results (something like):
FK_OrderNo CountOfFK_Stock
9 7
104 8
105 3
106 4
107 7
108 2
109 11
110 6
I guess you are looking for conditional count
Move the where clause filter to Count Aggregate and make the count aggregate to count the record only when stock.perishable = 0.
SELECT orderdemo.fk_orderno,
Count(CASE
WHEN stock.perishable = 0 THEN 1
END) AS nonperishable_count
FROM stock
INNER JOIN orderdemo
ON stock.studentid = orderdemo.fk_stock
GROUP BY orderdemo.fk_orderno
Count Aggregate can be replaced by SUM aggregate as well. Something like this
Sum(CASE
WHEN stock.perishable = 0 THEN 1
ELSE 0
END) AS nonperishable_count
I've the two tables orders
id article amount
1 1 1
2 2 50
and prices
id article min_amount price
1 1 1 42.99
2 2 1 5.06
3 2 5 4.55
4 2 10 4.3
5 2 25 4.05
6 2 100 2.66
The prices tables contains IDs of articles and a minimum amount you would have to buy to get a bulk discount (which would change the price for the order). I would like to join prices into orders, so that the result looks like:
id article amount price
1 1 1 42.99
2 2 50 4.05
The order id 2 is above the minimum (25) to get the article for 4.05€, but still below 100 at which you would get a bigger discount, so the query would to have pick the next-lower value.
I've tried this query so far
SELECT
orders.id AS id,
orders.article,
orders.amount,
prices.price,
(orders.amount - prices.min_amount) AS discount_diff
FROM orders
LEFT JOIN prices ON (prices.article = orders.article) AND (prices.min_amount <= orders.amount)
which gives this result
id article amount price discount_diff
1 1 1 42.99 0
2 2 50 5.06 49
2 2 50 4.55 45
2 2 50 4.3 40
2 2 50 4.05 25
You can find this example on "js"fiddle: http://sqlfiddle.com/#!9/1b2bf/8
The query you need is this:
SELECT orders.id AS id,
orders.article,
orders.amount,
prices.price
FROM orders
INNER JOIN prices ON ( prices.article = orders.article
and prices.min_amount <= orders.amount)
INNER JOIN ( SELECT orders.article,
orders.amount,
min(prices.price) minprince
FROM orders
INNER JOIN prices ON (prices.article = orders.article
AND prices.min_amount <= orders.amount)
GROUP BY orders.article,
orders.amount) b
ON ( prices.article = b.article
AND orders.amount = b.amount
AND prices.price = b.minprince)
See it here: http://sqlfiddle.com/#!9/1b2bf/27
Heres my query
SELECT
fsi_courier_assignment_print_master_listing.master_listing_id,
fsi_master_listing.transmittal_id,
fsi_transmittals.product_name,
fsi_transmittals.transmittal_id
FROM fsi_courier_assignment_print_master_listing
LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing.master_listing_id = fsi_master_listing.master_listing_id
LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."
UNION ALL
SELECT
fsi_courier_assignment_print_master_listing_undelivered.master_listing_id,
fsi_master_listing.transmittal_id,
fsi_transmittals.product_name,
fsi_transmittals.transmittal_id
FROM fsi_courier_assignment_print_master_listing_undelivered
LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing_undelivered.master_listing_id = fsi_master_listing.master_listing_id
LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."
fsi_courier_assignment_print_master_listing table
master_listing_id dispatch_code_id
2 2
5 2
36 2
37 2
134 2
135 2
136 2
137 2
138 2
139 2
140 2
fsi_courier_assignment_print_master_listing_undelivered table
master_listing_id dispatch_code_id
1 2
fsi_master_listing table
master_listing_id transmittal_id
1 1
2 1
5 2
36 2
37 2
134 3
135 3
136 3
137 3
138 3
139 3
140 3
fsi_transmittals table
transmittal_id product_name
1 Name 1
2 Name 2
3 Name 3
What Im trying to do is to get the combined result of product from fsi_courier_assignment_print_master_listing and fsi_courier_assignment_print_master_listing_undelivered where dispatch_code_id='2' and count them
My desire Output would be
Product Name Product Count
Name 1 2
Name 2 3
Name 3 7
Thanks in advance, hope somebody can help me to this..
Your query is fine, you just need to:
Add COUNT with GROUP BY product_name and put your query as a subquery.
The transmittal_id is specified two times in the two union queries, either remove one of them or give them different names (It might work fine in MySQL, but it is recommended not to do so).
So your query will be something like this:
SELECT
t.product_name,
COUNT(*) AS ProductCount
FROM
(
SELECT
ml.master_listing_id,
m.transmittal_id,
t.product_name
FROM fsi_courier_assignment_print_master_listing AS ml
LEFT JOIN fsi_master_listing AS m
ON ml.master_listing_id = m.master_listing_id
LEFT JOIN fsi_transmittals AS t
ON m.transmittal_id = t.transmittal_id
UNION ALL
SELECT
u.master_listing_id,
m.transmittal_id,
t.product_name
FROM fsi_courier_assignment_print_master_listing_undelivered as u
LEFT JOIN fsi_master_listing AS m
ON u.master_listing_id = m.master_listing_id
LEFT JOIN fsi_transmittals AS t
ON m.transmittal_id = t.transmittal_id
) AS t
GROUP BY t.product_name;
This will give you:
I was successful in writing the query that lists salesmen that did sell to a particular customer, but not those that have not. I suspect it is because the same salesmen that sold to the specific customer, also sold to other customers.
select a.name from salesperson a inner join orders b on
a.salesperson_id = b.salesperson_id where cust_id="4";
I was thinking that modifying the same query like this would do the trick:
.... a.salesperson_id <> b.salesperson_id where cust_id="4";
But the result lists all the salesmen. This is most likely due to the fact that the same salesmen that were returned in the original query, also sold to other customers
The 3 tables look like this:
Salesperson table
salesperson_ID, Name, Age, Salary
1 Abe 61 140000
2 Bob 34 44000
5 Chris 34 40000
7 Dan 41 52000
8 Ken 57 115000
11 Joe 38 38000
Customer table
cust_ID, Name, City Industry Type
4 faralon sacramento H
6 Apple cupertino S
7 Honda NY B
9 Kolb Oshkosh B
Orders table
Number, Order_date, cust_id, salesperson_id, Amount
10 8/2/1996 4 2 540
20 1/30/1999 4 8 1800
30 7/14/1995 9 1 460
40 1/29/1998 7 2 2400
50 2/3/1998 6 7 600
60 3/2/1998 6 7 720
70 5/6/1998 9 7 150
Any help would be greatly appreciated. ~Alpinehyker
You can do something like this:
select a.name from salesperson a
left join orders b on a.salesperson_id = b.salesperson_id and b.cust_id="4"
where b.Number is null
So, get all salepersons, left join to orders for customer 4, and return only rows where there is no such order.
I am assuming that Number is the primary key for Orders, or at least not null.
All salespeople who have NOT sold to customer_ID 4:
SELECT s.Name FROM Salesperson AS s
LEFT JOIN Orders AS o
ON s.salesperson_ID = o.salesperson_ID
WHERE o.customer_ID <> 4
GROUP BY o.salesperson_ID;
Perhaps this will work
SELECT
s.*
FROM `Salesperson` AS s
LEFT JOIN `Orders` AS o ON o.`salesperson_id` = s.`salesperson_ID`
WHERE
o.`cust_id` NOT IN (4)
GROUP BY s.`salesperson_ID`;
Answer to your 2nd question:
SELECT
COUNT(*) AS num_of_orders
,s.`Name`
FROM `Salesperson` AS s
LEFT JOIN `Orders` AS o ON o.`salesperson_id` = s.`salesperson_ID`
GROUP BY s.`salesperson_ID`
HAVING num_of_orders >= 2;
...and 3rd question. (assuming you have your highAchiever table ready)
INSERT INTO `highAchiever`
(`Name`,`Age`)
SELECT
`Name`
,`Age`
FROM `Salesperson`
WHERE
`Salary` >= 100000;