Hi,
Well, my idea still doesn't give me the result I need.
I have attached a spreadsheet showing the query results. As you can see, I should have only one row for DDS account with the "promo" value in each of the week date column, all in one row. Instead, it is not grouping as I hoped and I have several rows.
I have qryPAG_0 first query that only set some fields variables that will help build the "thePromo" final variable.
The qryPAG_1 takes the qryPAG_0 and adds the "thePromo" variable.
Finally the resulting query takes the qryPAG_1 and set the weeks field (53) putting the "thePromo" variable if the dates match a specific range. The output is correct dates wise. Just that it isn't putting all in a single row.
Probably I could have only one query for qryPAG_0 and qryPAG_1, however I prefer dealing with prepared variable as I feel it makes my life easier in the long run. The performance impact for this specific result is of no importance.
Here is my final query:
SELECT qryPAG_1.Brand, qryPAG_1.[sanofi code] AS Prod, qryPAG_1.Account, IIf([Start Date (DP)]<#1/8/2013# And [End Date (DP)]>#1/1/2013#,[thePromo],"") AS [01/01/13], IIf([Start Date (DP)]<#1/15/2013# And [End Date (DP)]>#1/8/2013#,[thePromo],"") AS [08/01/13], IIf([Start Date (DP)]<#1/22/2013# And [End Date (DP)]>#1/15/2013#,[thePromo],"") AS [15/01/13], IIf([Start Date (DP)]<#1/29/2013# And [End Date (DP)]>#1/22/2013#,[thePromo],"") AS [22/01/13], IIf([Start Date (DP)]<#2/5/2013# And [End Date (DP)]>#1/29/2013#,[thePromo],"") AS [29/01/13], IIf([Start Date (DP)]<#2/12/2013# And [End Date (DP)]>#2/5/2013#,[thePromo],"") AS [05/02/13], IIf([Start Date (DP)]<#2/19/2013# And [End Date (DP)]>#2/12/2013#,[thePromo],"") AS [12/02/13], IIf([Start Date (DP)]<#2/26/2013# And [End Date (DP)]>#2/19/2013#,[thePromo],"") AS [19/02/13], IIf([Start Date (DP)]<#3/5/2013# And [End Date (DP)]>#2/26/2013#,[thePromo],"") AS [26/02/13], IIf([Start Date (DP)]<#3/12/2013# And [End Date (DP)]>#3/5/2013#,[thePromo],"") AS [05/03/13], IIf([Start Date (DP)]<#3/19/2013# And [End Date (DP)]>#3/12/2013#,[thePromo],"") AS [12/03/13], IIf([Start Date (DP)]<#3/26/2013# And [End Date (DP)]>#3/19/2013#,[thePromo],"") AS [19/03/13], IIf([Start Date (DP)]<#4/2/2013# And [End Date (DP)]>#3/26/2013#,[thePromo],"") AS [26/03/13], IIf([Start Date (DP)]<#4/9/2013# And [End Date (DP)]>#4/2/2013#,[thePromo],"") AS [02/04/13], IIf([Start Date (DP)]<#4/16/2013# And [End Date (DP)]>#4/9/2013#,[thePromo],"") AS [09/04/13], IIf([Start Date (DP)]<#4/23/2013# And [End Date (DP)]>#4/16/2013#,[thePromo],"") AS [16/04/13], IIf([Start Date (DP)]<#4/30/2013# And [End Date (DP)]>#4/23/2013#,[thePromo],"") AS [23/04/13], IIf([Start Date (DP)] Between #5/7/2013# And [End Date (DP)]>#4/30/2013#,[thePromo],"") AS [30/04/13], IIf([Start Date (DP)]<#5/14/2013# And [End Date (DP)]>#5/7/2013#,[thePromo],"") AS [07/05/13], IIf([Start Date (DP)]<#5/21/2013# And [End Date (DP)]>#5/14/2013#,[thePromo],"") AS [14/05/13], IIf([Start Date (DP)]<#5/28/2013# And [End Date (DP)]>#5/21/2013#,[thePromo],"") AS [21/05/13], IIf([Start Date (DP)]<#6/4/2013# And [End Date (DP)]>#5/28/2013#,[thePromo],"") AS [28/05/13], IIf([Start Date (DP)]<#6/11/2013# And [End Date (DP)]>#6/4/2013#,[thePromo],"") AS [04/06/13], IIf([Start Date (DP)]<#6/18/2013# And [End Date (DP)]>#6/11/2013#,[thePromo],"") AS [11/06/13], IIf([Start Date (DP)]<#6/25/2013# And [End Date (DP)]>#6/18/2013#,[thePromo],"") AS [18/06/13], IIf([Start Date (DP)]<#7/2/2013# And [End Date (DP)]>#6/25/2013#,[thePromo],"") AS [25/06/13], IIf([Start Date (DP)]<#7/9/2013# And [End Date (DP)]>#7/2/2013#,[thePromo],"") AS [02/07/13], IIf([Start Date (DP)]<#7/16/2013# And [End Date (DP)]>#7/9/2013#,[thePromo],"") AS [09/07/13], IIf([Start Date (DP)]<#7/23/2013# And [End Date (DP)]>#7/16/2013#,[thePromo],"") AS [16/07/13], IIf([Start Date (DP)]<#7/30/2013# And [End Date (DP)]>#7/23/2013#,[thePromo],"") AS [23/07/13], IIf([Start Date (DP)]<#8/6/2013# And [End Date (DP)]>#7/30/2013#,[thePromo],"") AS [30/07/13], IIf([Start Date (DP)]<#8/13/2013# And [End Date (DP)]>#8/6/2013#,[thePromo],"") AS [06/08/13], IIf([Start Date (DP)]<#8/20/2013# And [End Date (DP)]>#8/13/2013#,[thePromo],"") AS [13/08/13], IIf([Start Date (DP)]<#8/27/2013# And [End Date (DP)]>#8/20/2013#,[thePromo],"") AS [20/08/13], IIf([Start Date (DP)]<#9/3/2013# And [End Date (DP)]>#8/27/2013#,[thePromo],"") AS [27/08/13], IIf([Start Date (DP)]<#9/10/2013# And [End Date (DP)]>#9/3/2013#,[thePromo],"") AS [03/09/13], IIf([Start Date (DP)]<#9/17/2013# And [End Date (DP)]>#9/10/2013#,[thePromo],"") AS [10/09/13], IIf([Start Date (DP)]<#9/24/2013# And [End Date (DP)]>#9/17/2013#,[thePromo],"") AS [17/09/13], IIf([Start Date (DP)]<#10/1/2013# And [End Date (DP)]>#9/24/2013#,[thePromo],"") AS [24/09/13], IIf([Start Date (DP)]<#10/8/2013# And [End Date (DP)]>#10/1/2013#,[thePromo],"") AS [01/10/13], IIf([Start Date (DP)]<#10/15/2013# And [End Date (DP)]>#10/8/2013#,[thePromo],"") AS [08/10/13], IIf([Start Date (DP)]<#10/22/2013# And [End Date (DP)]>#10/15/2013#,[thePromo],"") AS [15/10/13], IIf([Start Date (DP)]<#10/29/2013# And [End Date (DP)]>#10/22/2013#,[thePromo],"") AS [22/10/13], IIf([Start Date (DP)]<#11/5/2013# And [End Date (DP)]>#10/29/2013#,[thePromo],"") AS [29/10/13], IIf([Start Date (DP)]<#11/12/2013# And [End Date (DP)]>#11/5/2013#,[thePromo],"") AS [05/11/13], IIf([Start Date (DP)]<#11/19/2013# And [End Date (DP)]>#11/12/2013#,[thePromo],"") AS [12/11/13], IIf([Start Date (DP)]<#11/26/2013# And [End Date (DP)]>#11/19/2013#,[thePromo],"") AS [19/11/13], IIf([Start Date (DP)]<#12/3/2013# And [End Date (DP)]>#11/26/2013#,[thePromo],"") AS [26/11/13], IIf([Start Date (DP)]<#12/10/2013# And [End Date (DP)]>#12/3/2013#,[thePromo],"") AS [03/12/13], IIf([Start Date (DP)]<#12/17/2013# And [End Date (DP)]>#12/10/2013#,[thePromo],"") AS [10/12/13], IIf([Start Date (DP)]<#12/24/2013# And [End Date (DP)]>#12/17/2013#,[thePromo],"") AS [17/12/13], IIf([Start Date (DP)]<#12/31/2013# And [End Date (DP)]>#12/24/2013#,[thePromo],"") AS [24/12/13], IIf([Start Date (DP)]<#1/7/2014# And [End Date (DP)]>#12/31/2013#,[thePromo],"") AS [31/12/13]
FROM qryPAG_1
GROUP BY qryPAG_1.Brand, qryPAG_1.[sanofi code], qryPAG_1.Account, IIf([Start Date (DP)]<#1/8/2013# And [End Date (DP)]>#1/1/2013#,[thePromo],""), IIf([Start Date (DP)]<#1/15/2013# And [End Date (DP)]>#1/8/2013#,[thePromo],""), IIf([Start Date (DP)]<#1/22/2013# And [End Date (DP)]>#1/15/2013#,[thePromo],""), IIf([Start Date (DP)]<#1/29/2013# And [End Date (DP)]>#1/22/2013#,[thePromo],""), IIf([Start Date (DP)]<#2/5/2013# And [End Date (DP)]>#1/29/2013#,[thePromo],""), IIf([Start Date (DP)]<#2/12/2013# And [End Date (DP)]>#2/5/2013#,[thePromo],""), IIf([Start Date (DP)]<#2/19/2013# And [End Date (DP)]>#2/12/2013#,[thePromo],""), IIf([Start Date (DP)]<#2/26/2013# And [End Date (DP)]>#2/19/2013#,[thePromo],""), IIf([Start Date (DP)]<#3/5/2013# And [End Date (DP)]>#2/26/2013#,[thePromo],""), IIf([Start Date (DP)]<#3/12/2013# And [End Date (DP)]>#3/5/2013#,[thePromo],""), IIf([Start Date (DP)]<#3/19/2013# And [End Date (DP)]>#3/12/2013#,[thePromo],""), IIf([Start Date (DP)]<#3/26/2013# And [End Date (DP)]>#3/19/2013#,[thePromo],""), IIf([Start Date (DP)]<#4/2/2013# And [End Date (DP)]>#3/26/2013#,[thePromo],""), IIf([Start Date (DP)]<#4/9/2013# And [End Date (DP)]>#4/2/2013#,[thePromo],""), IIf([Start Date (DP)]<#4/16/2013# And [End Date (DP)]>#4/9/2013#,[thePromo],""), IIf([Start Date (DP)]<#4/23/2013# And [End Date (DP)]>#4/16/2013#,[thePromo],""), IIf([Start Date (DP)]<#4/30/2013# And [End Date (DP)]>#4/23/2013#,[thePromo],""), IIf([Start Date (DP)] Between #5/7/2013# And [End Date (DP)]>#4/30/2013#,[thePromo],""), IIf([Start Date (DP)]<#5/14/2013# And [End Date (DP)]>#5/7/2013#,[thePromo],""), IIf([Start Date (DP)]<#5/21/2013# And [End Date (DP)]>#5/14/2013#,[thePromo],""), IIf([Start Date (DP)]<#5/28/2013# And [End Date (DP)]>#5/21/2013#,[thePromo],""), IIf([Start Date (DP)]<#6/4/2013# And [End Date (DP)]>#5/28/2013#,[thePromo],""), IIf([Start Date (DP)]<#6/11/2013# And [End Date (DP)]>#6/4/2013#,[thePromo],""), IIf([Start Date (DP)]<#6/18/2013# And [End Date (DP)]>#6/11/2013#,[thePromo],""), IIf([Start Date (DP)]<#6/25/2013# And [End Date (DP)]>#6/18/2013#,[thePromo],""), IIf([Start Date (DP)]<#7/2/2013# And [End Date (DP)]>#6/25/2013#,[thePromo],""), IIf([Start Date (DP)]<#7/9/2013# And [End Date (DP)]>#7/2/2013#,[thePromo],""), IIf([Start Date (DP)]<#7/16/2013# And [End Date (DP)]>#7/9/2013#,[thePromo],""), IIf([Start Date (DP)]<#7/23/2013# And [End Date (DP)]>#7/16/2013#,[thePromo],""), IIf([Start Date (DP)]<#7/30/2013# And [End Date (DP)]>#7/23/2013#,[thePromo],""), IIf([Start Date (DP)]<#8/6/2013# And [End Date (DP)]>#7/30/2013#,[thePromo],""), IIf([Start Date (DP)]<#8/13/2013# And [End Date (DP)]>#8/6/2013#,[thePromo],""), IIf([Start Date (DP)]<#8/20/2013# And [End Date (DP)]>#8/13/2013#,[thePromo],""), IIf([Start Date (DP)]<#8/27/2013# And [End Date (DP)]>#8/20/2013#,[thePromo],""), IIf([Start Date (DP)]<#9/3/2013# And [End Date (DP)]>#8/27/2013#,[thePromo],""), IIf([Start Date (DP)]<#9/10/2013# And [End Date (DP)]>#9/3/2013#,[thePromo],""), IIf([Start Date (DP)]<#9/17/2013# And [End Date (DP)]>#9/10/2013#,[thePromo],""), IIf([Start Date (DP)]<#9/24/2013# And [End Date (DP)]>#9/17/2013#,[thePromo],""), IIf([Start Date (DP)]<#10/1/2013# And [End Date (DP)]>#9/24/2013#,[thePromo],""), IIf([Start Date (DP)]<#10/8/2013# And [End Date (DP)]>#10/1/2013#,[thePromo],""), IIf([Start Date (DP)]<#10/15/2013# And [End Date (DP)]>#10/8/2013#,[thePromo],""), IIf([Start Date (DP)]<#10/22/2013# And [End Date (DP)]>#10/15/2013#,[thePromo],""), IIf([Start Date (DP)]<#10/29/2013# And [End Date (DP)]>#10/22/2013#,[thePromo],""), IIf([Start Date (DP)]<#11/5/2013# And [End Date (DP)]>#10/29/2013#,[thePromo],""), IIf([Start Date (DP)]<#11/12/2013# And [End Date (DP)]>#11/5/2013#,[thePromo],""), IIf([Start Date (DP)]<#11/19/2013# And [End Date (DP)]>#11/12/2013#,[thePromo],""), IIf([Start Date (DP)]<#11/26/2013# And [End Date (DP)]>#11/19/2013#,[thePromo],""), IIf([Start Date (DP)]<#12/3/2013# And [End Date (DP)]>#11/26/2013#,[thePromo],""), IIf([Start Date (DP)]<#12/10/2013# And [End Date (DP)]>#12/3/2013#,[thePromo],""), IIf([Start Date (DP)]<#12/17/2013# And [End Date (DP)]>#12/10/2013#,[thePromo],""), IIf([Start Date (DP)]<#12/24/2013# And [End Date (DP)]>#12/17/2013#,[thePromo],""), IIf([Start Date (DP)]<#12/31/2013# And [End Date (DP)]>#12/24/2013#,[thePromo],""), IIf([Start Date (DP)]<#1/7/2014# And [End Date (DP)]>#12/31/2013#,[thePromo],"");
Thanks for helping me with this issue. I am sure it is something simple but I can't put my fingers on it.