I need to filter a table and show only result that are 3 months ago. So if it is August, to show me May.
Here is my query sql code:
SELECT tblAppointment.WorkID, tblAppointment.AppointmentDate, tblCustomer.CustomerID
FROM tblWork INNER JOIN (tblCustomer INNER JOIN tblAppointment ON tblCustomer.CustomerID = tblAppointment.CustomerID) ON tblWork.WorkID = tblAppointment.WorkID
GROUP BY tblAppointment.WorkID, tblAppointment.AppointmentDate, tblCustomer.CustomerID
HAVING (((tblAppointment.WorkID)=3) AND ((tblAppointment.AppointmentDate) Between Format(DateAdd("m",-3,Date()),"m") And Format(DateAdd("m",-4,Date()),"m")))
ORDER BY tblAppointment.AppointmentDate, tblCustomer.CustomerID;
I am getting a error. I am trying to fix HAVING part.
Please help.
Always handle dates as dates, not text:
SELECT
tblAppointment.WorkID,
tblAppointment.AppointmentDate,
tblCustomer.CustomerID
FROM
tblWork
INNER JOIN
(tblCustomer
INNER JOIN tblAppointment
ON tblCustomer.CustomerID = tblAppointment.CustomerID)
ON tblWork.WorkID = tblAppointment.WorkID
WHERE
tblAppointment.AppointmentDate
Between
DateSerial(Year(Date()), Month(Date())-3, 1)
And
DateSerial(Year(Date()), Month(Date())-2, 0)
GROUP BY
tblAppointment.WorkID,
tblAppointment.AppointmentDate,
tblCustomer.CustomerID
HAVING
tblAppointment.WorkID=3
ORDER BY
tblAppointment.AppointmentDate,
tblCustomer.CustomerID;
Based on your error, and the code, it looks like you are trying to compare dates to strings. Regardless of the GroupBy / Having constructs being necessary, change the Date comparison to:
and DatePart("m", Date()) - DatePart("m", tblAppointment.AppointmentDate)) = 3
You may need to modify to cover the case for the beginning / end of year.
AND (
(
Year(Date()) = Year(tblAppointment.AppointmentDate)
AND
DatePart("m", Date()) - DatePart("m", tblAppointment.AppointmentDate) = 3
)
OR
(
Year(Date()) > Year(tblAppointment.AppointmentDate)
AND
DatePart("m", Date()) + 12 - DatePart("m", tblAppointment.AppointmentDate) = 3
)
)
Related
I want to add add the Year to date component to this code. I have tried some other ways but I am not getting what I would like to see. Can someone please help me revised this to include the YTD in addition to the Month to date that is already there?
SELECT
COST__DESC,
ST.AD_SRV_MTN AS MONTH_OF_AD,
COUNT(DISTINCT CM.CM_NBR) AS CMS,
MEM_MO AS MBR_MTH,
CMS/MBR_MTH*1000 AS CMS_PER_1000
FROM XTR.FT_CM AS CM
JOIN XTR.FT_ST AS ST ON ST.CM_NBR = CM.CM_NBR
JOIN XTR.DIM_MED_CST AS MC ON ST.CST_CK = MCC.CST_CK
JOIN XTR.DIM_AF AS AFF ON ST.PRO_CK = AFF.AFF_CK
JOIN XTR.DIM_ADJDCTN_STAT AS A_S ON ST.ADJDCTN_STAT_CK = A_S.ADJDCTN_STAT_CK
JOIN XTR.DIM_ADJ_OT AS OT ON ST.ADJ_CK = OT.ADJ_CK
LEFT JOIN
(SELECT
CALENDAR_YEAR_MONTH as YEAR_MO,
SUM(MBR.COUNT_NBR) as MEM_MO
FROM XTR.FT_MBR_MONTHS MBR
INNER JOIN DIM_MBR_C ON MBR.DB_MBR_CK = DIM_MBR_C.DB_MBR_CK
AND MBR.DATE_CK BETWEEN DIM_MBR_C.DB_eff_date_ck
AND DIM_MBR_C.DB_END_DATE_CK
INNER JOIN DIM_DATE DT ON ELI_DATE_CK = DT.DATE_CK
WHERE MBR.F_C_CK = 500058321 AND YEAR_MO >= 201701
GROUP BY 1) MM ON ST.AD_SRV_MTN = MM.YEAR_MO
WHERE ST.F_C_CK = 500058321 AND ST.ST_START_DATE_CK >= 20200101
AND ST.AD_SRV_MTN > 201912 AND MC.MED_DESC IN ('Er', 'IP')
AND ST.AD_SRV_MTN < ((EXTRACT (YEAR FROM CURRENT_DATE) *100) +
EXTRACT (MONTH FROM CURRENT_DATE))
GROUP BY 1,2,4
ORDER BY 1,2
Honestly I don't really get your SQL and what is counted, but: Your can play with dates quite easy in Teradata, as Dates are stored (and can be used) internally as INTEGER. Just keep in mind year 1900 as year 0 and format YYYYMMDD.
So e.g. 16-Apr-2020 is in Format YYYYMMDD 20200416 and if you take 1900 as 0 you'll end up with 1200416 which is the internal format. Just try SELECT CURRENT_DATE (INT); - So if you want compare YearNumers you just have to divide by 10000.
With this your can implement YTD as SUM (CASE WHEN CURRENT_DATE/10000 = <YourDateField>/10000 THEN <YourKPI> else 0 END) as YourKPI_YTD. Counting can be done by SUM...THEN 1 ELSE 0 END....
I need to write a query which when executed at the beginning of the current month must generate a report which contains total transactions made in the previous month and I have written the below query:
SELECT
l.state AS Province
,l.taxing_entity AS TaxJurisdiction
,COUNT(DISTINCT t.trans_id) AS TotalTransCount
FROM TRANSACTION AS t
LEFT OUTER JOIN location AS l
ON t.location_id = l.location_id
LEFT OUTER JOIN trans_line AS tl
ON t.trans_id = tl.trans_id
LEFT OUTER JOIN contract as c
ON t.contract_id = c.contract_id
WHERE
l.chain_id = 10
AND c.issuer_id IN (156967)
AND t.extra_5 BETWEEN LAST_DAY(TODAY - 2 units month) + 1 units day AND LAST_DAY(TODAY - 1 units month)
AND tl.cat NOT IN ('DEF','DEFD','DEFC')
GROUP BY l.state, l.taxing_entity, Province
ORDER BY l.state,l.taxing_entity
But this query throws an error "It is not possible to convert between the specified types. ". I tried changing the date range as below:
t.extra_5 >= DATEADD(MONTH, DATEDIFF(MONTH, 31, CURRENT_TIMESTAMP), 0) AND t.extra_5 < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
and
t.extra_5 > DateAdd(WEEK, -1, GETDATE()+1) and t.extra_5<=GETDATE()
But nothing has worked out so far. The current format for the column "extra_5" is as below:
Can someone please suggest a way to get the total transactions from previous month.
I am trying to find 12 weeks average through same weekday.
Example:
weekday sale
So basically I need the average based on the number of stores having net > 0. for yellow color, it should be total/11 as there is no sale for 02-07-16
I have written this query but it doesn't work properly. Could you guys please help me?
SELECT Store_DateJoined.StoreKey, Store_DateJoined.RestaurantName, Store_DateJoined.Ownership, Store_DateJoined.DateKey,
Sales_Summary.Net, Sales_Summary.[LastWeek_Net],
(SELECT AVG(Net) AS AVGNET
FROM (SELECT TOP (12) Net
FROM DailySummary AS FB INNER JOIN
DimDate ON FB.DateKey = DimDate.DateKey
WHERE datename(WEEKDAY, TransactionDate) IN (select datename(WEEKDAY, TransactionDate) from [NandosDW].[dbo].[DailySummary]) and (FB.StoreKey = Store_DateJoined.StoreKey) AND (FB.DateKey <= Store_DateJoined.DateKey) AND
(DimDate.DayNameOfWeek = Store_DateJoined.DayNameOfWeek) AND (FB.DateKey >= (FB.DateKey - 84))
ORDER BY FB.DateKey, StoreKey DESC) AS WAVGNET) AS [Week12_Net],
Store_DateJoined.FullDate, Store_DateJoined.DayNameOfWeek, Store_DateJoined.FinancialWC, Store_DateJoined.strFinancialWeek
FROM Sales_Summary RIGHT OUTER JOIN Store_DateJoined LEFT OUTER JOIN
DimLFL ON Store_DateJoined.StoreKey = DimLFL.StoreKey AND Store_DateJoined.DateKey = DimLFL.DateKey ON
Sales_Summary.RedCatID = Sore_DateJoined.StoreKey AND
Sales_Summary.DateKey = Store_DateJoined.DateKey
I havent included the joins in this query, but they are present
Order Table Schema
My Access Database Query
Select * from
(
SELECT reseller.id, Max(orders.[order date]) as OrderDate
FROM Reseller INNER JOIN orders ON Reseller.ID = orders.ResellerID
group by reseller.id
)K
WHERE (((K.[OrderDate]) Not Between (Date()-1) And (Date()-18)))
To find those reseller that did not order for 18 Days.
But this is giving below records
Am I missing something ?
Try this query instead:
SELECT *
FROM
(
SELECT reseller.id, Max(orders.[order date]) AS OrderDate
FROM
Reseller
INNER JOIN
orders
ON Reseller.ID = orders.ResellerID
GROUP BY reseller.id
) K
WHERE DateDiff("d", K.[OrderDate], Date()) > 18
Try this
Select * from
(
SELECT reseller.id, Max(orders.[order date]) as OrderDate
FROM Reseller INNER JOIN orders ON Reseller.ID = orders.ResellerID
group by reseller.id
)
WHERE (((K.[OrderDate]) Not Between ( DateAdd("d", -1, Date())
And DateAdd("d", -18, Date())
)
I think when you do Date() - 1 it is subtracting 1 time internal until like subpart of second from the date, not what you intended to subtract 1 date
ALSO, did not change, but do you really want to make the top limits today - 1 day, or just today?
I am trying to get the sum of "Qty" in a Table A called "Quote_Items" based on a "Required_by_Date" from Table B called Quotes. Both tables have a common "Quote_No" The required date is one month ago.
I have used the following but it produces a NULL, but I cannot see why
select sum(Qty)
from quotes.Quote_Items_Quantities
left outer join quotes.Quotes on (Quote_Required_by_Date = Qty)
WHERE (DatePart(yy, Quote_Required_by_Date) = DatePart(yy, DateAdd(m,1,getdate()))) and
datepart(m,Quote_Required_by_Date) = datepart(m,dateadd(m,1,getdate()))
Any suggestions what I am doing wrong here.
Try this:
SELECT SUM(i.Qty)
FROM Quote_Items i
JOIN Quotes q on i.Quote_No = q.Quote_No
AND CONVERT(varchar, q.Required_by_Date, 112) = CONVERT(varchar, DATEADD(month, -1, getdate()), 112)
or this (equivalent without using JOIN)
SELECT SUM(i.Qty)
FROM Quote_Items i
WHERE EXISTS(SELECT 1 FROM Quotes WHERE Quote_No = i.Quote_No AND CONVERT(varchar, Required_by_Date, 112) = CONVERT(varchar, DATEADD(month, -1, getdate()), 112))
Your query is producing NULL because of the join condition. You have
on Quote_Required_by_Date = Qty
However, the date is not going to match the quantity. Instead, you need to match on the Quote_No, according to your question:
select sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
WHERE (DatePart(yy, Quote_Required_by_Date) = DatePart(yy, DateAdd(m,1,getdate()))) and
datepart(m,Quote_Required_by_Date) = datepart(m,dateadd(m,1,getdate()));
You can also simplify your query by using the month() and year() functions:
select sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
WHERE year(Quote_Required_by_Date) = year(DateAdd(m, 1, getdate()) and
month(Quote_Required_by_Date) = month(dateadd(m,1,getdate());
Finally, you mind find it useful to use group by and get the results for many months:
select year(Quote_Required_by_Date), month(Quote_Required_by_Date), sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
group by year(Quote_Required_by_Date), month(Quote_Required_by_Date)
order by 1 desc, 2 desc;