Using Snowflake, I want to get the daily stock for the last 7 days.
Columns I have in this table are: product_ID, date, and quantity
my desired out put should look like the following:
product_ID DATE Quantity
82471 2022-07-14 40
82471 2022-07-15 35
82471 2022-07-16 34
82471 2022-07-17 50
82471 2022-07-18 53
82471 2022-07-19 51
82471 2022-07-20 40
Any ideas how to reach this output? :)
I don't know if you need to aggregate your data, but based on your input, something like this may work:
select product_id, date, Quantity
from mytable
where "DATE" > dateadd( 'days', -7, current_date )
order by product_id, date DESC;
Related
I have sample data as below:
Date
Index
26-07-2022
26
26-06-2022
23
24-07-2022
12
19-06-2022
16
26-04-2022
01
26-05-2022
10
26-07-2022
12
I want to select data of latest day from each month. For example if today's date is 26-07-2022 then I want to select all records where date is 26th.
So my output should look like below:
Date
Index
26-07-2022
26
26-06-2022
23
26-04-2022
01
26-05-2022
10
26-07-2022
12
Do anybody know how can I achieve this. Thanks.
Assuming that the data type of the column [Date] is DATE, use the function DAY():
SELECT *
FROM tablename
WHERE DAY([Date]) = DAY(GETDATE())
AND [Date] <= GETDATE(); -- you may remove this if it is not needed
See the demo.
Two separate tables. Need the find the date that is >= in Table A based on a date in Table B. Only TransactionCode 59 in Table A should be considered.
From the example tables below my return in table B First_Tran_Date should be "01/22/2022." Table A contains over 35 million records with thousands of AccountNumber's and grows each day.
Need T-SQL to take Table B ChangeDate "01/21/2022" and find the first time Table A shows a TransactionDate on or after that date and only TransactionCode 59 counts. All other TransactionCode dates should not be evaluated for the return.
Table A:
AccountNumber TransactionDate TransactionCode
xxxx310 2/3/2022 40
xxxx310 1/19/2022 40
xxxx310 1/22/2022 59
xxxx310 1/10/2022 59
xxxx310 3/15/2022 40
xxxx310 1/25/2022 59
xxxx310 1/30/2022 40
xxxx310 1/31/2022 59
xxxx310 1/31/2022 62
xxxx310 3/8/2022 59
Table B:
Account ChangeDate First_Tran_Date COUNT_OF_DAYS
xxxx310 01/21/2022 **RESULT NEEDED** (Calculated First_Tran_Date - ChangeDate = COUNT_OF_DAYS)
I have tried the following without getting a correct result:
T-SQL example...
Created a VIEW…
WITH added_row_number AS (
SELECT
*,
ROW_NUMBER() OVER(
PARTITION BY AccountNumber
ORDER BY
TransactionDate
) AS row_number
FROM dbo.LoanTransactions
)
SELECT
*
FROM added_row_number
WHERE row_number = 1
AND TransactionDate >= '2022-03-01'
AND TransactionCode IN ('59', '61', '70', '77', '82') Used a
SELECT
from that VIEW …
SELECT
DISTINCT Account,
Prod_CD,
OldValue,
NewValue,
Acct_Open_DT,
ChangeDate,
LOSVIEW_All_Transactions_From_CORE1.TransactionDate AS First_Tran_Date,
LastTransactionDate,
CASE
WHEN Prod_CD IN ('L50', 'L51', 'L54', 'L77') THEN DATEDIFF(
DAY,
ChangeDate,
LOSVIEW_All_Transactions_From_CORE1.TransactionDate
)
ELSE DATEDIFF(DAY, Acct_Open_DT, ChangeDate)
END AS COUNT_OF_DAYS
FROM dbo.R_InsuranceCodeChanges
LEFT JOIN dbo.LOSVIEW_All_Transactions_From_CORE AS LOSVIEW_All_Transactions_From_CORE1
ON dbo.R_InsuranceCodeChanges.Account = LOSVIEW_All_Transactions_From_CORE1.AccountNumber
WHERE
dbo.R_InsuranceCodeChanges.ChangeDate >= '2022-01-01'
AND dbo.R_InsuranceCodeChanges.NewValue <> '0'
If I understand correctly you want to look up two dates, and also calculate the number of days between them?
Those dates should be the first and last time a row with TransactionCode 59 appears, for a given account, and only including records on or after a given date?
So for the data in your example, the missing date should be 2022-01-22? Then the number of days would be 52 days?
For that I would use OUTER APPLY; which allows you to effectively run a query once for each input row...
SELECT
*,
DATEDIFF(DAY, table_a.MinTransactionDate, table_a.MaxTransactionDate)
FROM
table_b
OUTER APPLY
(
SELECT
MIN(TransactionDate) AS MinTransactionDate,
MAX(TransactionDate) AS MaxTransactionDate
FROM
table_a
WHERE
AccountNumber = table_b.Account
AND TransactionDate >= table_b.ChangeDate
AND TransactionCode = 59
)
AS table_a
Should be an index on tableA.TransactionDate. But I just translated your words to SQL and this is what I got:
select min(TransactionDate) as minDate
from tableA
where TransactionCode = 59
and TransactionDate >= (select max(TransactionDate) from tableB)
In order to find the Sum of data(CallsAnswered in my case) for a Week but a specific Hours between 6 PM to 12 AM., I have below set of a query, but it is skipping one-day data in my sample data. Could you please validate my query and help me out me to find where I'm missing?
Create Query:
Create Table tblSingleBox (ScanDate DateTime, SkillTargetID Varchar(10), CallsAnswered int)
Insert Query
Insert into tblSingleBox
Values ('2018-02-18 19:17:01', '10888', 32),
('2018-02-18 23:59:59', '10888', 01),
('2018-02-19 00:10:01', '10888', 15),
('2018-02-19 17:59:59', '10889', 12),
('2018-02-19 20:59:59', '10889', 90),
('2018-02-25 21:59:59', '10889', 40)
Query:
SELECT convert(varchar(10),ScanDate,101)ScanDate,SkillTargetID, SUM(CallsAnswered)CallsAnswered
FROM tblSingleBox (nolock)
WHERE DATEPART(Hour, scandate) between 18 and 24
and (scandate between '2018-02-18' and '2018-02-25')
GROUP BY convert(varchar(10),ScanDate,101) ,SkillTargetID
order by convert(varchar(10),ScanDate,101)
Expected Output:
ScanDate SkillTargetID CallsAnswered
18-02-2018 10888 33
18-02-2018 10889 11
19-02-2018 10889 90
25-02-2018 10889 40
Actual Output I'm getting:
ScanDate SkillTargetID CallsAnswered
02/18/2018 10888 33
02/18/2018 10889 11
02/19/2018 10889 90
scandate has time in it and 2018-02-25 21:59:59 is greater than 2018-02-25 (which is treated as 2018-02-25 00:00:00 when compared to DATETIME).
So cast scandate to DATE (to cutoff time) or change
scandate between '2018-02-18' and '2018-02-25'
to
scandate >= '2018-02-18' and scandate < '2018-02-26'
I have a table called 'Days' set up like this (The Day column is type DateTime):
User_ID | Day
----------------------------------
39 | 2010-05-16 00:00:00.000
39 | 2009-05-16 00:00:00.000
40 | 2008-06-12 00:00:00.000
40 | 2008-10-07 00:00:00.000
41 | 2010-04-10 00:00:00.000
41 | 2010-03-02 00:00:00.000
42 | 2010-08-12 00:00:00.000
42 | 2011-09-15 00:00:00.000
What I'm trying to do is, when given a User_ID and a certain date, get the most recent "Day" IF and only IF that user does not have any "Day" equal to or after the given date.
For example, given User_ID = 39 and date = 2010-05-15, nothing should be returned since one of the "Day" dates listed for User_ID = 39 is after the given date 2010-05-15. However, if User_ID = 40, it would return 2008-10-07, since that is the most recent date that is NOT equal to OR after the given date 2010-05-15.
I have this so far, but I have no clue how to add in the "if" clause. Below just returns the most recent date for the given user.
SELECT MAX(Day) from Days WHERE User_ID = 39
After that it would be something like if MAX(Day) < 2010-05-15
What's the correct way to do this?
Try this:
SELECT MAX([Day]) as TheDay FROM Days
WHERE User_ID = 39
AND [Day] < '20100515'
AND NOT EXISTS (SELECT 1 FROM Days WHERE User_ID = 39 AND [DAY] >= '20100515')
With CTE:
with CTE(User_ID, [Day]) as
(
SELECT * FROM Days WHERE User_ID = 39
)
SELECT MAX([Day]) as TheDay FROM CTE
WHERE [Day] < '20100515'
AND NOT EXISTS (SELECT 1 FROM CTE WHERE [DAY] >= '20100515')
Untested:
General Logic: Generate a set of data having the max date for each user then limit that set to be only those records with dates less than date desired and for desired user.
One approach is to use an inline view and a self join. In my example I called the inline view B. in it I get max date and user from days.
Then, join back to days based on user and day listed to only return a user's max day. We then limit to user and max date desired. If max date is less than date desired then a record would be returned. If max date is >= date provided it will return no record for the user.
SELECT d.day
FROM days d
INNER join (SELECT user_ID, max(date) mdate
FROM days
GROUP BY user_ID) B
on d.user_ID = b.user_ID
and b.mdate = d.day
WHERE d.day < 'inputdate'
and d.user_ID = 'userid'
or perhaps more simply...
Since you want the max date for each user, but only if their date is not greater than a desired dates... simply return the max date for any user, then eliminate those > desired date for desired user.
SELECT mdate
FROM (SELECT user_ID, max(date) mdate
FROM days
GROUP BY user_ID) B
WHERE mdate <= 'datedesired'
and user_ID = 'user_ID_Desired'
You need to add day condition in WHERE Clause
SELECT MAX(Day) from Days WHERE User_ID = 39 AND Day < yourInputDate
My table is having data e.g. empcode designation code and promotion date, I want to get what was an employee's designation on some given date. for eg.
EmpCode DesignationCode PromotionDate
101 50 2010-01-25
101 10 2014-01-01
101 11 2015-01-01
102 10 2009-10-01
103 15 2015-01-01
now if I check designation as on 2014-02-01 it should give result as following
EmpCode DesignationCode PromotionDate
101 10 2014-01-01
102 10 2009-10-01
Can anyone please tell what query should I write ?
Thanks in Advance.
You can try:
SELECT DISTINCT ON (EmpCode) EmpCode, DesignationCode, PromotionDate
FROM mytable
WHERE PromotionDate <= '2014-02-01'
ORDER BY EmpCode, PromotionDate DESC
The query first filters out any records having a PromotionDate that is past given date, i.e. '2014-02-01'.
Using DISTINCT ON (EmpCode) we get one row per EmpCode. This is the one having the most recent PromotionDate (this is achieved by placing PromotionDate DESC in the ORDER BY clause).
Demo here