Jump to content

New SQL creation problem


Erwin007
Go to solution Solved by kicken,

Recommended Posts

Table reps:
- rep_id
- rep_name
- rep_touroperatorid

Table sales:
- sales_id
- sales_repid
- sales_totaldollars

my query so far:
SELECT reps.rep_name, sum(sales.sales_totaldollars) as total
FROM `sales`
JOIN reps on reps.rep_touroperator_id = '5' 
WHERE (sales.sales_date BETWEEN '$from' AND '$to')
GROUP BY reps.rep_name

and this as code:

$result = mysqli_query($con,$query);
$total = 0;
while($row = mysqli_fetch_assoc($result)){
      $total+=$row['total']


It is working but per rep I get the total of all sales for that period of time instead of the total sales per rep.
I need something like this:
Juan                 3,500
Maria                2,000
Peter                 1,500

But I get this:

Juan                 7,000
Maria                7,000
Peter                 7,000

I hope I gave enough info to solve this.

Thanks


 

Link to comment
Share on other sites

your column names imply that sales.sales_repid contains matching reps.rep_id values. if the query didn't produce any result, that indicates there isn't matching data, either in the join condition or in the where clause. you would need to post an example of the data that the query should match for anyone here to have a chance at helping with the problem.

Edited by mac_gyver
Link to comment
Share on other sites

Hi,

I solved the abovementioned problem but have now another problem but it's kind of complex, let me try:
There are 5 tables in the DB:
reps: rep_id, rep_name, rep_touroperator_id and rep_active
touroperators: touroperator_id, touroperator_name
products: product_id, product_name
sales: 


 

Edited by Erwin007
too com plex to explain
Link to comment
Share on other sites

I have 2 queries:

Query1 =

SELECT reps.rep_name, sum(sales.sales_totaldollars) as total
FROM sales
JOIN reps on reps.rep_id = sales.sales_repid
AND reps.rep_touroperator_id = ‘5’
AND reps.rep_active = 1
WHERE (sales.sales_date BETWEEN ‘$from’ AND ‘$to’)
GROUP BY reps.rep_name

Query2 =

SELECT reps.rep_name, sum(salesdetails.salesdetails_pricedollars) as totalrum
FROM sales
JOIN salesdetails on salesdetails.salesdetails_salesticketnr = sales.sales_ticketnr
AND salesdetails.salesdetails_productid = ‘41’
JOIN reps on reps.rep_id = sales.sales_repid
AND reps.rep_touroperator_id = ‘5’
AND reps.rep_active = 1
WHERE (sales.sales_date BETWEEN ‘$from’ AND ‘$to’)
GROUP BY reps.rep_name

Separately the result is 100%, combined it is not.
Anybody?

 

Link to comment
Share on other sites

1 hour ago, Erwin007 said:

Separately the result is 100%, combined it is not.

define combined.

We haven't a clue what you are tryng to do, or what you expect, and in which way it isn't doing what you expect.

Without information we cannot help. Your test data too would be good.

Link to comment
Share on other sites

Ok, I will try again:
image.png.61c0a4e5f60d933caeec1864d68d2fc5.png

This report I need:
REP:                                            Product(41):                              TOTAL SALES:
Cely                                                76                                           206
Heike                                            192                                           502

The Report I get (with my query):
REP:                                            Product(41):                              TOTAL SALES:
Cely                                                76                                           206
Heike                                            192                                           416

This is my query:

SELECT reps.rep_name, sum(sales.sales_totaldollars) as total, sum(salesdetails.salesdetails_pricedollars) as totalrum
FROM `sales`
JOIN salesdetails ON salesdetails.salesdetails_salesticketnr = sales.sales_ticketnr AND salesdetails.salesdetails_productid =  '41'                                                                                        
JOIN reps ON reps.rep_id = sales.sales_repid AND reps.rep_touroperator_id = '5' AND reps.rep_active = 1
WHERE (sales.sales_date BETWEEN '$from' AND '$to') 
GROUP BY reps.rep_name";

The error (the missing 86 at total sales for Heike) is that is "skips" the 2 tickets that don't have the product id 41.
The date I choose here is from May 8 till May 8, but it works 100% (just like the GROUP BY part) with other periods, also the part JOIN reps on... works 100%.
Somewhere in this JOIN salesdetails on... is my error.


I hope it's a little bit more clear.

 

Edited by Erwin007
more explanation
Link to comment
Share on other sites

Which set of data gave you result you posted...

1 hour ago, Erwin007 said:

The Report I get (with my query):
REP:                                            Product(41):                              TOTAL SALES:
Cely                                                76                                           206
Heike                                            192                                           416

Was it

  • the data in your picture, or
  • the data in those sql files

I created data by typing from the pictures (not a chore I enjoy) but on running your query my results matched neither of those posted - expected/actual

Link to comment
Share on other sites

The 416 appears to be correct.

mysql> SELECT reps.rep_id
    ->       , SUM(sales_totaldollars) as totaldollars
    -> FROM `sales`
    -> JOIN salesdetails ON salesdetails.salesdetails_salesticketnr = sales.sales_ticketnr
    ->                  AND salesdetails.salesdetails_productid =  '41'                                                                      
    -> JOIN reps ON reps.rep_id = sales.sales_repid
    ->          AND reps.rep_touroperator_id = '5'
    ->          AND reps.rep_active = 1
    -> WHERE sales.sales_date BETWEEN '2023-05-01' AND '2023-05-15'
    -> GROUP BY rep_id;
+--------+--------------+
| rep_id | totaldollars |
+--------+--------------+
|     77 |          206 |
|    259 |          416 |
+--------+--------------+

Why do you think it should be 502?

Link to comment
Share on other sites

Sounds like you want to separate totals.

  • The total sales for Product 41
  • The overall total sales (for all products)

Is that right?  If so, you'd want to make two queries that will get you each of those totals, then use them as sub-queries in a third query that lists the reps.

Link to comment
Share on other sites

I have a query to find out the total sales for that rep for that period and it works 100%:
SELECT reps.rep_name, sum(sales.sales_totaldollars) as total
FROM `sales`
JOIN reps on reps.rep_id = sales.sales_repid AND reps.rep_touroperator_id = '5' AND reps.rep_active = 1
WHERE (sales.sales_date BETWEEN '$from' AND '$to')
GROUP BY reps.rep_name

I have anothe query where I find the total sales of productid 41 for that rep in that period and it also works 100%:
SELECT reps.rep_name, sum(salesdetails.salesdetails_pricedollars) as totalrum
FROM `sales`
JOIN salesdetails on salesdetails.salesdetails_salesticketnr = sales.sales_ticketnr  
JOIN reps on reps.rep_id = sales.sales_repid AND reps.rep_touroperator_id = '5' AND reps.rep_active = 1
WHERE (sales.sales_date BETWEEN '$from' AND '$to') AND salesdetails.salesdetails_productid = '41'
GROUP BY reps.rep_name

"Combining" these 2 has been a nightmare the last 20hours....

Link to comment
Share on other sites

2 minutes ago, kicken said:

Sounds like you want to separate totals.

  • The total sales for Product 41
  • The overall total sales (for all products)

Is that right?  If so, you'd want to make two queries that will get you each of those totals, then use them as sub-queries in a third query that lists the reps.

Yes, I want to know the sales for productid 41 and the total sales for that rep.
I just posted the 2 queries I made and they work flawless, but "combining" them...pppffffttt

 

Link to comment
Share on other sites

select 
	reps.rep_name,
    p41Sales.totalrum,
    allSales.total
from reps 
inner join (
	SELECT reps.rep_id, sum(sales.sales_totaldollars) as total
	FROM `sales`
	JOIN reps on reps.rep_id = sales.sales_repid AND reps.rep_touroperator_id = '5' AND reps.rep_active = 1
	WHERE (sales.sales_date BETWEEN '2023-05-01' AND '2023-05-15')
	GROUP BY reps.rep_id
) allSales on allSales.rep_id=reps.rep_id
inner join (
	SELECT reps.rep_id, sum(salesdetails.salesdetails_pricedollars) as totalrum
	FROM `sales`
	JOIN salesdetails on salesdetails.salesdetails_salesticketnr = sales.sales_ticketnr  
	JOIN reps on reps.rep_id = sales.sales_repid AND reps.rep_touroperator_id = '5' AND reps.rep_active = 1
	WHERE (sales.sales_date BETWEEN '2023-05-01' AND '2023-05-15') AND salesdetails.salesdetails_productid = '41'
	GROUP BY reps.rep_name
) p41Sales on p41Sales.rep_id=reps.rep_id

Your two independent queries become sub-queries that select the totals you want, grouped by rep ID.

You join the results of those queries then to your reps table by the ID and output the rep details and the totals.

 

Link to comment
Share on other sites

It almost works.
It doesn't show the reps that sold something other than product id=41. )the amount at product must be 0 but the total sales has an amount.

if you put the from date 2023-04-01 and to-date 2023-04-30 you see 2 more reps (Jose Mendez and Honeydi) which didn't sell productid 41 but they sold some other stuff.

Edited by Erwin007
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.