SELECT T1.*
FROM [b][i]MyTable[/i][/b] T1
INNER JOIN
(
SELECT TOP 12 T2.[b][i]MyDateField[/i][/b]
FROM [b][i]MyTable[/i][/b] T2
WHERE Weekday(T2.[b][i]MyDateField[/i][/b]) NOT IN (1,7)
ORDER BY T2.[b][i]MyDateField[/i][/b] DESC
) T2 ON T1.[b][i]MyDateField[/i][/b] = T2.[b][i]MyDateField[/i][/b];
Try something like the following (substitute highlighted table and field names as appropriate):
Code:SELECT T1.* FROM [B][I]MyTable[/I][/B] T1 INNER JOIN ( SELECT TOP 12 T2.[B][I]MyDateField[/I][/B] FROM [B][I]MyTable[/I][/B] T2 WHERE Weekday(T2.[B][I]MyDateField[/I][/B]) NOT IN (1,7) ORDER BY T2.[B][I]MyDateField[/I][/B] DESC ) T2 ON T1.[B][I]MyDateField[/I][/B] = T2.[B][I]MyDateField[/I][/B];
I am also only using one table. I am simply using two instances of the same table within a single query. The first instance is to display all of the fields from the table. The second instance is used in an inline subquery to filter the dates selected to the 12 most recent, excluding Saturdays and Sundays.
Can you post an example from your database, the table data and the query results so that we can see how the results are not correct?
I am also only using one table. I am simply using two instances of the same table within a single query. The first instance is to display all of the fields from the table. The second instance is used in an inline subquery to filter the dates selected to the 12 most recent, excluding Saturdays and Sundays.
Can you post an example from your database, the table data and the query results so that we can see how the results are not correct?
sushmitha said:I cannot use inline query in my situation. I need to write a single sql query as I need to display in Business Objects report
Also, the query that you are running please Because I'm sure that you must have had to modify his to make it work for you.
Here is my query
SELECT *
FROM tbl_Log AS T1 INNER JOIN [SELECT TOP 12 tbl_Log.cdate FROM tbl_Log T2
WHERE Weekday(T2.cdate) NOT IN (1,7)
ORDER BY T2.cdate DESC
]. AS T2 ON T1.cdate = T2.cdate;
SELECT *
FROM tblTransactions AS T1 INNER JOIN [SELECT TOP 12 T2.TRdate FROM tblTransactions T2
WHERE Weekday(T2.TRdate) NOT IN (1,7)
ORDER BY T2.TRdate DESC
]. AS T2 ON T1.TRdate = T2.TRdate;
MSAccessRookie said:I modified it to work with my transactions table, and it works fine with the exception that it returned all records from the top 12 dates, not the top 12 records.
sushmitha said:How to get the previous 12days (only weekdays.Exclude weekends)
by selecting a date using query group by each day
I need to get the previous 12 days excluding holidays
No, you are quite correct. You need a separate table with dates to be excluded from the search. In this case, those dates would need to include Saturdays, Sundays and Holidays.boblarson said:I don't think any of the code here will exclude holidays. If you need that to occur as well, you are going to need a table of holidays to also exclude against (correct me if I'm wrong).
SELECT T1.*
FROM [b][i]MyTable[/i][/b] T1
INNER JOIN
(
SELECT TOP 12 T2.[b][i]MyDateField[/i][/b]
FROM [b][i]MyTable[/i][/b] T2
WHERE T2.[b][i]MyDateField[/i][/b] NOT IN
(SELECT T3.[b][i]ExcludeDate[/i][/b]
FROM [b][i]MyExcludeTable[/i][/b] T3
)
ORDER BY T2.[b][i]MyDateField[/i][/b] DESC
) T2 ON T1.[b][i]MyDateField[/i][/b] = T2.[b][i]MyDateField[/i][/b];
That's what sushmitha asked for, the top 12 days:
sushmitha, the query looks right. Can you also post a sample of the results you are getting, and of the source table data?