tags:

views:

43

answers:

1
A: 

You can combine both queries by joining them on membershipPackType.

The INNER JOIN with your first query takes care of the hardcoded WHERE clause.

SQL Statement

SELECT  COUNT(*) AS theCount
        , stockItem 
FROM    membershipPackItems 
        LEFT JOIN membershipPacks ON membershipPackNumber = membershipPackType 
        LEFT JOIN stock ON stockNumber = membershipPackItemNo 
        INNER JOIN (
          SELECT  COUNT(*) AS theCount
                  , membershipPackType
          FROM    inputBatches
                  LEFT JOIN inputActions ON inputActionID = inputBatchAction
                  LEFT JOIN members ON memberID = inputBatchMemberID
                  LEFT JOIN membershipPacks ON membershipPackType = memberMembershipType
                  LEFT JOIN memPack ON memPackInputBatch = inputBatchID
          WHERE   memPackBookedOut = 'U' 
                  AND inputActionAbbr <> 'E'
          GROUP BY 
                  membershipPackType
        ) cnt ON cnt.membershipPackType = membershipPackItems.membershipPackType
GROUP BY stockItem 
ORDER BY stockNumber;

Edit

I have trimmed down the scripts and altered your first query as below in an effort to try and understand what is what.

If you provide some scripts to fill the tables with some data (preferably the data that should become the outputs you've posted), I am willing to have another look at it, otherwise, this is as far as I am prepared to go with this, sorry.

Note: that you should definitly strive for a consistent naming scheme. membershipPackItemNo in one table is stockNumber in an other, Packtype links with PackNumber, memPackInputBatch links with inputBatchID. If not for your own sanity, it would make it much easier for us to figure out what is what.

Note: the scripts have been changed for SQL Server. I don't have a MySQL running.

CREATE TABLE membershipPackItems (
  membershipPackNumber INTEGER NOT NULL,
  membershipPackItemNo varchar(6) NOT NULL,
) 

CREATE TABLE membershipPacks (
  membershipPackType INTEGER default NULL,
) 

CREATE TABLE stock (
  stockNumber INTEGER NOT NULL,
  stockItem varchar(50) NOT NULL,
) 

CREATE TABLE inputBatches (
  inputBatchID INTEGER NOT NULL IDENTITY(1, 1),
  inputBatchMemberID INTEGER NOT NULL,
  inputBatchAction INTEGER NOT NULL,
) 

CREATE TABLE members (
  memberID INTEGER NOT NULL IDENTITY(1, 1),
  memberMembershipType INTEGER NOT NULL,
) 

CREATE TABLE memPack (
  memPackBookedOut varchar(1) NOT NULL,
  memPackInputBatch INTEGER NOT NULL,
) 

SELECT  COUNT(*) AS theCount, st.stockItem 
FROM    stock st
        LEFT OUTER JOIN membershipPackItems mpi ON mpi.membershipPackItemNo = st.stockNumber
        LEFT OUTER JOIN membershipPacks mp ON mp.membershipPackType = mpi.membershipPackNumber
WHERE   mpi.membershipPackNumber = 11
GROUP BY stockItem
Lieven
That combines the two queries nicely but the output is the same as my original second query, it's only giving me the quantity of each stock item required for one of each of the different membership packs.
AndrewDFrazier
I'm not sure I know what you mean but you could add MIN(theCount) from the inner query to the result. If that is not the result you are after, please post the result you want (it's to much for my poor brain).
Lieven
If you look at the image that I linked to, the top result shows counts for seven different membership pack types, and these counts add up to 62.The result that I want will show the total number of each stock item needed to make up those 62 packs. The result that I have shows me the number of each stock item needed to make one of each of the seven different types.The desired result would look like this:http://www.kidderminsterharriers.com/images/query5.png
AndrewDFrazier
Still not sure what you need. It looks like you are trying to output two (loosely) unrelated resultsets as one? Wouldn't it be possible to take the output from two queries and union them together? If you like to peruse this further, could you post the table scripts and data that make up your results?
Lieven
I've added the data structure if it helps.The membershipPackItems table lists the stock items that go into each membership pack.The stock table is the stock record for each item.The inputBatches table is the list of new members or renewals or extensions as they are added.The members table is the full record for each member.The memPack table relates to the inputBatches table, the memPackBookedOut field is the status flag where the 'U' that we have in the query is 'Unissued'The join to inputActions can be removed changing inputActionAbbr <>'E' to inputBatchAction <>'2' in the query
AndrewDFrazier