Question : Need Query for this problem

I have 3 tables.... Customers, Campaigns, and Campaign Customers (Campaign Customers just contains the ID of Customers and Campaigns). I Need the query to produce the following info:

Select each customer who is in a campaign
          Order the data by last name, first name, and campaign name
          Return the following information:
              - last name
              - first name
              - campaign name
              - the sequence number of the customer in the list
              - the sequence number of the campaign for each customer
              - the number of campaigns to which each customer belongs

         Example:

 LastName   FirstName   CampaignName    Customer_Seq    Campaign_Seq    Campaign_Count
    --------          ---------   ------------            ------------                  ------------        --------------
    Doe             Jane        First Campaign              1                             1                          2
    Doe            Jane        Next Campaign               1                             2                          2
    Hungry        I             First Campaign                2                             1                          1
    Public          John        First Campaign              3                             1                           3
    Public          John        Last Campaign              3                              2                          3
    Public          John        Next Campaign              3                             3                           3
    Smith          Short       First Campaign               4                             1                           1

Answer : Need Query for this problem

tim0822,

I ended up reworking this to use temp tables.




SELECT IDENTITY(int, 1, 1) AS ID, cu.CustomerID, cu.LastName, cu.FirstName, COUNT(*) AS TotCamp
INTO #Cust
FROM Customers cu INNER JOIN
      CampaignCustomers cc ON cu.CustomerID = cc.CustomerID
WHERE cc.IsActive = 1
GROUP BY cu.CustomerID, cu.LastName, cu.FirstName
ORDER BY cu.LastName, cu.FirstName, cu.CustomerID

SELECT IDENTITY(int, 1, 1) AS ID, cu.ID AS CuID, cu.CustomerID, cu.LastName, cu.FirstName, ca.CampaignID,
      ca.CampaignName
INTO #Camp
FROM #Cust cu INNER JOIN
      CampaignCustomers cc ON cu.CustomerID = cc.CustomerID INNER JOIN
      Campaigns ca ON cc.CampaignID = ca.CampaignID
WHERE cc.IsActive = 1
GROUP BY cu.ID, cu.CustomerID, cu.LastName, cu.FirstName, ca.CampaignName, ca.CampaignID
ORDER BY cu.ID, cu.CustomerID, cu.LastName, cu.FirstName, ca.CampaignName, ca.CampaignID

SELECT cu.LastName, cu.FirstName, ca.CampaignName,
      (SELECT COUNT(cu2.ID)
      FROM #Cust cu2
      WHERE cu2.ID <= cu.ID) AS Customer_Seq,
      (SELECT COUNT(ca2.ID)
      FROM #Camp ca2
      WHERE ca2.CuID = ca.CuID AND ca2.ID <= ca.ID) AS Campaign_Seq,
      cu.TotCamp AS Campaign_Count
FROM #Cust cu INNER JOIN
      #Camp ca ON cu.CustomerID = ca.CustomerID
ORDER BY ca.ID

DROP TABLE #Cust
DROP TABLE #Camp




Patrick
Random Solutions  
 
programming4us programming4us