If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Average in query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-09, 10:40
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Average in query

Is it possible to calculate an avarage based on the cusomer_id (off_klant_id) I want the avarege of all the entrys of an customer and number of orders.

Code:
$sql = "SELECT
			off.offerte AS off_offerte,
			off.klant_id AS off_klant_id,
			off.ref_klant AS off_ref_klant,
			off.datum_dossier AS off_datum_dossier,
			off.datum_offerte AS off_datum_offerte,
			off.bedrag AS off_bedrag,
			off.plasma AS off_plasma,
			off.water AS off_water,
			off.laser AS off_laser,
			off.knip_zet AS off_knip_zet,
			off.handel AS off_handel,
			off.dikte AS off_dikte,
			off.materiaal AS off_materiaal,
			off.omschrijving AS off_omschrijving,
			off.status_id AS off_status_id,
			off.reden_vervallen AS off_reden_vervallen,
			off.datum_vervallen AS off_datum_vervallen,
			off.archief_doos AS archief_doos,
			adr.klantnaam AS adr_klantnaam,
			ordnr.ordernummer AS ordnr_ordernummer,
			ordnr.datum_order AS ordnr_datum_order,
			ordnr.datum_leveren AS ordnr_datum_leveren,
			fac.factuurnummer AS fac_factuurnummer,
			fac.datum_verzonden AS fac_datum_verzonden,
			fac.datum_betaald AS fac_datum_betaald,
			st.id AS st_id,
			st.status AS st_status
            FROM offertenummers AS off
            LEFT OUTER JOIN adressen AS adr 
            ON adr.id = off.klant_id 
            LEFT OUTER JOIN ordernummers AS ordnr
            ON ordnr.offerte_id = off.offerte 
            LEFT OUTER JOIN factuurnummers AS fac
            ON fac.offerte_id = off.offerte
            LEFT OUTER JOIN status AS st
            ON st.id = off.status_id
            ORDER BY off.offerte DESC
    		LIMIT $currentRecord, $recordsPerPage";
Reply With Quote
  #2 (permalink)  
Old 05-02-09, 13:49
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
I made this query, does it look ok?

Code:
// Query voor scoringspercentage alles van klant
	    $sql_totaal = "SELECT * FROM offertenummers WHERE klant_id = '".$row['off_klant_id']."' ";
	    
        // Query voor scoringspercentage order van klant
		$sql_score = "SELECT
			off.klant_id AS off_klant_id,
			ordnr.ordernummer AS ordnr_ordernummer
            FROM offertenummers AS off
            RIGHT OUTER JOIN ordernummers AS ordnr
            ON ordnr.offerte_id = off.offerte 
            WHERE off.klant_id = '".$row['off_klant_id']."' "; 
            
           $res_totaal = mysql_query($sql_totaal,$con);    
    	   $totaal = mysql_num_rows($res_totaal);
    	   $res_score = mysql_query($sql_score,$con);    
    	   $score = mysql_num_rows($res_score);
    	   $percentage = $score / $totaal * 100;
Reply With Quote
  #3 (permalink)  
Old 05-02-09, 16:36
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
I made this query, does it look ok?
This might sound a bit radical but have you tried running it? and did it produce the right results? It might be worth looking up the AVG function.
Reply With Quote
  #4 (permalink)  
Old 05-03-09, 03:39
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Yes, I'm running it without any problems. I just need to to know if this was the right way.

Thank you for you're tip about 'average'.
Reply With Quote
  #5 (permalink)  
Old 05-03-09, 04:28
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I'm afraid your method is the wrong way. You should be using the AVG function allong with GROUP BY. It's very simple to use and if you read the link I gave then you'll quickly become an expert.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On