Я делаю успехи в создании собственного сценария для моей программы:
// You might want to use a date range and specify the first Sunday and use that variable.
// Or, you may want to have a user request variable where they key in the starting Sunday #YYYY MM DD#
// Let us work out the current week number
$iStartWeekNumber = $Today#CUSTOMDATE[%W] + 1
TEXT "Current week number (1 to 52): $iStartWeekNumber"
EOL
// Let us work out the current year
$iThisYear = $Today#CUSTOMDATE[%Y]
TEXT "Current year: $iThisYear"
EOL
// Now we can do a manual for loop from iStartWeekNumber to the end of the year
TEXT "TODO: Work out how to show the date nicely formatted"
EOL
$datWeek = $Today
LOOP AS $iWeekNumber FROM $iStartWeekNumber TO 52
$datWeek = $datWeek + 7
TEXT "Week: $iWeekNumber"
EOL
TEXT "Date: {$datWeek#SHORTDATE}"
EOL
// Iterate all available speakers
LOOP FROM "Congregation Speakers" NODATERANGE WHERE "Congregation" IS "$LocalCong" AND "notavail" IS "0" SORTBY "Speaker"
// Requirement 1
// 1. He is not in program for a talk in our home or another congregation for that same WEEKEND (either saturday or sunday)
// Store the current speaker into a variable
VARIABLE_FIELD "Speaker" "$strCurrentSpeaker"
LOOP SQL "SELECT * FROM [Home Talks] WHERE ((DatePart('ww',[Last Given],2)=$iWeekNumber)) AND ((DatePart('yyyy',[Last Given])=$iThisYear)) AND [Speaker] = '$strCurrentSpeaker' AND [Congregation]='$LocalCong'" ROW_COUNT "$iNumHomeTalks"
END_LOOP
LOOP SQL "SELECT * FROM [Away Talks] WHERE ((DatePart('ww',[Talk Date],2)=$iWeekNumber)) AND ((DatePart('yyyy',[Talk Date])=$iThisYear)) AND [Brother] = '$strCurrentSpeaker'" ROW_COUNT "$iNumAwayTalks"
END_LOOP
IF "$iNumHomeTalks" IS "0" AND "$iNumAwayTalks" IS "0"
// Requirement 2
// 2. He should not give a talk more than once in a MONTH (so he is unavailable if he will be giving a talk during the month)
$iMonth = date_part($datWeek,"M")
LOOP SQL "SELECT * FROM [Home Talks] WHERE ((DatePart('m',[Last Given])=$iMonth)) AND ((DatePart('yyyy',[Last Given])=$iThisYear)) AND [Speaker] = '$strCurrentSpeaker' AND [Congregation]='$LocalCong'" ROW_COUNT "$iNumHomeTalksForMonth"
END_LOOP
LOOP SQL "SELECT * FROM [Away Talks] WHERE ((DatePart('m',[Talk Date])=$iMonth)) AND ((DatePart('yyyy',[Talk Date])=$iThisYear)) AND [Brother] = '$strCurrentSpeaker'" ROW_COUNT "$iNumAwayTalksForMonth"
END_LOOP
$iTotalTalksForMonth = $iNumHomeTalksForMonth + $iNumAwayTalksForMonth
IF "$iTotalTalksForMonth" IS "0"
// Requirement 3
// 3. That same weekend either one or more than x speakers are not out for a talk or unavailable
// TO DO
TEXT "$strCurrentSpeaker"
EOL
END_IF
END_IF
END_LOOP
BLANK *1
END_LOOP
Я хочу специально обратить ваше внимание на два набора SQL-запросов :
Набор 1:
LOOP SQL "SELECT * FROM [Home Talks] WHERE ((DatePart('ww',[Last Given],2)=$iWeekNumber)) AND ((DatePart('yyyy',[Last Given])=$iThisYear)) AND [Speaker] = '$strCurrentSpeaker' AND [Congregation]='$LocalCong'" ROW_COUNT "$iNumHomeTalks"
END_LOOP
LOOP SQL "SELECT * FROM [Away Talks] WHERE ((DatePart('ww',[Talk Date],2)=$iWeekNumber)) AND ((DatePart('yyyy',[Talk Date])=$iThisYear)) AND [Brother] = '$strCurrentSpeaker'" ROW_COUNT "$iNumAwayTalks"
END_LOOP
И установить 2:
LOOP SQL "SELECT * FROM [Home Talks] WHERE ((DatePart('m',[Last Given])=$iMonth)) AND ((DatePart('yyyy',[Last Given])=$iThisYear)) AND [Speaker] = '$strCurrentSpeaker' AND [Congregation]='$LocalCong'" ROW_COUNT "$iNumHomeTalksForMonth"
END_LOOP
LOOP SQL "SELECT * FROM [Away Talks] WHERE ((DatePart('m',[Talk Date])=$iMonth)) AND ((DatePart('yyyy',[Talk Date])=$iThisYear)) AND [Brother] = '$strCurrentSpeaker'" ROW_COUNT "$iNumAwayTalksForMonth"
END_LOOP
Это мой собственный язык сценариев. Но под капотом он берет необработанные SQL-запросы и запускает их в базе данных Microsoft Access, используя Jet.
Они отлично работают. Но мне интересно, можно ли объединить два набора запросов в один? Как видите, все, что я пытаюсь установить, это количество найденных записей. Поэтому, если бы я мог объединить запросы [Home Talks]
и [Away Talks]
, я бы получил только 2 запроса вместо четырех.
Мне кажется, мне нужно использовать UNION
, но в каждой таблице одно из полей отличается:
Так что я не могу просто вставить ключевое слово UNION
между двумя запросами.