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 > Mysql Join Query with Totals

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-11, 07:14
Fabzster Fabzster is offline
Registered User
 
Join Date: Mar 2011
Posts: 1
Exclamation Mysql Join Query with Totals

Hi

I use the query below for a report of stores that have sold policies durning a period. I now need to edit that same query to additionally give me the total policies per store.

Code:
SELECT tblpolicies.PolicyNumber
     , tblpolicies.StoreId
     , tblpolicies.ConsultantFullName
     , tblpolicies.DateReceived
     , tblpolicies.ClientFullName
     , tblpolicies.Comment
     , tblpolicies.Query
     , tblpolicies.PolicyStatus
     , tblpolicies.DateModified
     , Groups.GroupName
     , Groups.StoreName
     , Groups.StoreTarget
     , Groups.StoreManager
     , Groups.PortfolioName
     , Groups.StoreStatus
  FROM tblpolicies
LEFT OUTER
  JOIN ( SELECT StoreId,
                StoreName, StoreManager, GroupName, StoreTarget, PortfolioName, StoreStatus 
           FROM tblstores
         GROUP
             BY StoreId ) AS Groups
    ON tblpolicies.StoreId = Groups.StoreId
WHERE DateReceived BETWEEN '{$_SESSION['StartDateReport']}' AND '{$_SESSION['EndDateReport']}'
AND GroupName='{$_SESSION['GroupNameReport']}' AND StoreStatus='ACTIVE' ORDER BY StoreId, DateReceived
Reply With Quote
  #2 (permalink)  
Old 03-10-11, 09:04
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Code:
SELECT tblpolicies.PolicyNumber
     , tblpolicies.StoreId
     , tblpolicies.ConsultantFullName
     , tblpolicies.DateReceived
     , tblpolicies.ClientFullName
     , tblpolicies.Comment
     , tblpolicies.Query
     , tblpolicies.PolicyStatus
     , tblpolicies.DateModified
     , Groups.GroupName
     , Groups.StoreName
     , Groups.StoreTarget
     , Groups.StoreManager
     , Groups.PortfolioName
     , Groups.StoreStatus
     , Policies.total_policies
  FROM tblstores AS Groups
INNER
  JOIN tblpolicies
    ON tblpolicies.StoreId = Groups.StoreId
   AND tblpolicies.DateReceived BETWEEN '{$_SESSION['StartDateReport']}' 
                                    AND '{$_SESSION['EndDateReport']}'
INNER
  JOIN ( SELECT StoreId
              , COUNT(*) AS total_policies
           FROM tblpolicies
          WHERE DateReceived BETWEEN '{$_SESSION['StartDateReport']}' 
                                 AND '{$_SESSION['EndDateReport']}'
         GROUP
             BY StoreId ) AS Policies
    ON Policies.StoreId = Groups.StoreId
 WHERE Groups.GroupName='{$_SESSION['GroupNameReport']}' 
   AND Groups.StoreStatus='ACTIVE' 
ORDER 
    BY Groups.StoreId
     , tblpolicies.DateReceived
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
join help, mysql

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