How To Determine If Item Was Available During A Date Range
Nov 5, 2015
I have a table that contains the SKU and a period of time the SKU was available to purchase (StartDtKey and EndDtKey). I am trying to write a query determine which skus were available during a given date range. The DtKeys correspond to a date in our calendar table and are in order. So DtKey 5 is 4 days after DtKey 1.
Code:
CREATE TABLE #SKU(
[SKU] varchar(10) NULL,
[StartDtKey] int NULL,
[EndDtKey] int NULL);
INSERT INTO #SKU VALUES ('Sku1',1,9)
,('Sku2',2,6)
,('Sku3',10,20)
,('Sku4',5,9)
,('Sku5',25,30)
Example:
Which SKUs were available on at least one day between DtKey 7 and DtKey 9?
I have included the create table statements, sample data and a stored procedure.
I have a stored procedure that walks the associations so that I can assemble all the nodes of the tree. I think this is working properly.
I have two requests/needs/problems that I need to solve: (1) I want to create a NEW stored procedure that takes a WhoID and an AssocID. This new stored procedure would show the shortest or closest path between WhoID and AssocID.
For example, if I pass in 6,1 to this new proc I want the stored procedure to return the hierarchy with the closest path between them. Based on the sample data below, the path would be 6 - 8 8 - 2 2 - 3 3 - 1
(2) I might also need a proc that accepts two inputs (again, lets use 6,1) but would show all paths / branches of the tree where person 1 exists. Based on the stored procedure procCTE_Assoc 6, this solution would exclude the "fred and jeff" node but would include the other two nodes because person 1 is within the other two nodes.
Please provide your suggestions, comments, or solutions!
CREATE TABLE dbo.Who( WhoID bigint IDENTITY(1,1) NOT NULL, FName varchar(50) NOT NULL ) GO CREATE TABLE dbo.Assoc( WhoID bigint NOT NULL, AssocID bigint NOT NULL ) ON PRIMARY
SET IDENTITY_INSERT dbo.Who ON GO INSERT INTO Who (WhoID,FName) VALUES (1,'user1') INSERT INTO Who (WhoID,FName) VALUES (2,'user2') INSERT INTO Who (WhoID,FName) VALUES (3,'admin1') INSERT INTO Who (WhoID,FName) VALUES (4,'admin2') INSERT INTO Who (WhoID,FName) VALUES (5,'admin3') INSERT INTO Who (WhoID,FName) VALUES (6,'Keith') INSERT INTO Who (WhoID,FName) VALUES (7,'fred') INSERT INTO Who (WhoID,FName) VALUES (8,'Joe') INSERT INTO Who (WhoID,FName) VALUES (9,'Jack') INSERT INTO Who (WhoID,FName) VALUES (10,'Doug') INSERT INTO Who (WhoID,FName) VALUES (11,'Mark') INSERT INTO Who (WhoID,FName) VALUES (12,'Al') INSERT INTO Who (WhoID,FName) VALUES (13,'Jeff') INSERT INTO Who (WhoID,FName) VALUES (14,'Brandon') INSERT INTO Who (WhoID,FName) VALUES (15,'Ashley') INSERT INTO Who (WhoID,FName) VALUES (16,'Lisa') INSERT INTO Who (WhoID,FName) VALUES (17,'Julie') INSERT INTO Who (WhoID,FName) VALUES (18,'Brian') go SET IDENTITY_INSERT dbo.Who OFF GO INSERT INTO Assoc (WhoID,AssocID) VALUES (1,4) INSERT INTO Assoc (WhoID,AssocID) VALUES (2,1) INSERT INTO Assoc (WhoID,AssocID) VALUES (2,3) INSERT INTO Assoc (WhoID,AssocID) VALUES (6,7) INSERT INTO Assoc (WhoID,AssocID) VALUES (6,8) INSERT INTO Assoc (WhoID,AssocID) VALUES (7,13) INSERT INTO Assoc (WhoID,AssocID) VALUES (8,2) INSERT INTO Assoc (WhoID,AssocID) VALUES (14,8) INSERT INTO Assoc (WhoID,AssocID) VALUES (6,15) INSERT INTO Assoc (WhoID,AssocID) VALUES (15,18) INSERT INTO Assoc (WhoID,AssocID) VALUES (15,17) INSERT INTO Assoc (WhoID,AssocID) VALUES (17,5) INSERT INTO Assoc (WhoID,AssocID) VALUES (5,1)
go create proc dbo.procCTE_Assoc @WhoID bigint AS
WITH GroupsCTE (WhoID, AssocID, FName, FNameAssoc, Lvl, Sort, GroupWithIndent) AS ( -- Anchor Who definition SELECT M.WhoID, MA.AssocID, M.FName, AssocWho.FName, 0 AS Lvl, CONVERT(varchar(2000),AssocWho.FName) AS Sort, CONVERT(varchar(255), AssocWho.FName) FROM Who M INNER JOIN Assoc MA ON M.WhoID = MA.WhoID INNER JOIN Who AssocWho ON AssocWho.WhoID = MA.AssocID WHERE M.WhoID = @WhoID UNION ALL -- Recursive Who definition SELECT g1.WhoID, MA1.AssocID, g1.FName, g2.FName, Lvl + 1, CONVERT(varchar(2000),RTRIM(Sort) + '|' + g2.FName), CONVERT(varchar(255), REPLICATE ('| ' , Lvl+1) + g2.FName) FROM Who g1 INNER JOIN Assoc MA1 ON g1.WhoID = MA1.WhoID INNER JOIN Who g2 ON g2.WhoID = MA1.AssocID INNER JOIN GroupsCTE CTE ON MA1.WhoID = CTE.AssocID )
I have 2 tables, one is table A which stores Resources Assign to work for a certain period. The structure is as below
Name StartDate EndDate Tan 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000 Max 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000 Alan 2015-04-01 16:30:00.000 2015-04-02 00:30:00.000
The table B stores the item process time. The structure is as below
Item ProcessStartDate ProcessEndDate V 2015-04-01 09:30:10.000 2015-04-01 09:34:45.000 Q 2015-04-01 10:39:01.000 2015-04-01 10:41:11.000 W 2015-04-01 11:44:00.000 2015-04-01 11:46:25.000 A 2015-04-01 16:40:10.000 2015-04-01 16:42:45.000 B 2015-04-01 16:43:01.000 2015-04-01 16:45:11.000 C 2015-04-01 16:47:00.000 2015-04-01 16:49:25.000
I need to select the item which process in 2015-04-01 16:40:00 and 2015-04-01 17:30:00. Beside that I need to know how many resource is assigned to process the item in that period of time. I only has the start date is 2015-04-01 16:40:00 and end date is 2015-04-01 17:30:00. How I can select the data from both tables. There is no need for JOIN, just seperate selections.
Another item process time is in 2015-04-01 10:00:00 and 2015-04-04 11:50:59.
The result expected is
Table A
Name StartDate EndDate Alan 2015-04-01 16:30:00.000 2015-04-02 00:30:00.000
Table B
Item ProcessStartDate ProcessEndDate A 2015-04-01 16:30:10.000 2015-04-01 16:32:45.000 B 2015-04-01 16:33:01.000 2015-04-01 16:35:11.000 C 2015-04-01 16:37:00.000 2015-04-02 16:39:25.000
Scenario 2 expected result
Table A
Name StartDate EndDate Tan 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000 Max 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000
I am attempting to write a SQL query that retrieves info processed between two times (ie. 2:00 pm to 6:00 pm) during a date range (ie. 8/1/06 to 8/14/06)... I am new to SQL and am perplexed... I have referenced several texts, but have not found a solution. Even being pointed in the right direction would be greatly appreciated!!
My goal is to select values from the same date range for a month on month view to compare values month over month. I've tried using the date trunc function but I'm not sure what the best way to attack this is. My thoughts are I need to somehow select first day of every month + interval 'x days' (but I don't know the syntax).In other words, I want to see
Select Jan 1- 23rd feb 1-23rd march 1-23rd april 1-23rd ,value from table
I have a QA Deployment Date field that is being returned in a custom report I created. I also found a sample date range parameter:
What I want to accomplish:
I want to select a From and To Date and filter the report to only display the rows that have the QA Deployment Date within the selected range.
For example.. I want to select From Date (8/1/2105) and To Date (8/31/2015) and I only want to return only the results that have a QA Deployment date between that selected range.
Date parameter. I created a report that allows a drop down for a date range to be selected. However, whenever I preview the report, I get an error. I know my error stems from my date fields being in this format "201301" , and the "date/ time" in SSRS being mm/dd/yyyy on the drop down calendar in SSRS.
I know the direction I want to go in, but just a little confused on where would I use the convert or cast function. Would it be in the data parameter itself, or a part of the query before the @start date and @End date?
I would like to be able to search by a single date, @StartDate, or by a date range , between @StartDate and @EndDate. I am having a hard time with the logic on this for a report in SSRS.
I have to display the last temperature reading from an activity table for all the dates in a selected date range.So if I select the date range from 09/01/2012 to 09/30/2012, the results should look like this:
Date Temperature 09/01/2012 73.5 09/02/2012 75.2 09/03/2012 76.3 09/04/2012 73.3 09/05/2012 77.0 09/06/2012 74.5 and so on.
I am using this to get the dates listed: WITH CTE_DatesTable AS ( SELECT CAST('20120901' as date) AS [Date] UNION ALL SELECT DATEADD(dd, 1, [Date]) FROM CTE_DatesTable WHERE DATEADD(dd, 1, [Date]) <= '20120930' ) SELECT [Date] FROM CTE_DatesTable
How could I get the temperature if I did a sub-query here?
I have a report that I need to run on 2 different date ranges.
Both report's data is 2 days behind today's date. so... WHERE reportdate between dateadd('d',date(),-2) and dateadd('d',date(),-2) OR SOMETHING LIKE THAT, NO BIGGIE HERE
The 2nd report is a month to date report. This is the 1 I can't figure out. WHERE reportdate between (the first day of this month) and dateadd('d',date(),-2)
So that would look like WHERE reportdate between 1/1/2007 and 1/21/2007
My problem is, if today is the 1st day of the month... how can I get my critiera to NOT do this WHERE reportdaye between 2/1/2007 and 1/30/2007
Hi Group!I am struggling with a problem of giving a date range given the startdate.Here is my example, I would need to get all the accounts opened betweeneach month end and the first 5 days of the next month. For example, inthe table created below, I would need accounts opened between'5/31/2005' and '6/05/2005'. And my query is not working. Can anyonehelp me out? Thanks a lot!create table a(person_id int,account int,open_date smalldatetime)insert into a values(1,100001,'5/31/2005')insert into a values(1,200001,'5/31/2005')insert into a values(2,100002,'6/02/2005')insert into a values(3,100003,'6/02/2005')insert into a values(4,100004,'4/30/2004')insert into a values(4,200002,'4/30/2004')--my query--Select *[color=blue]>From a[/color]Where open_date between '5/31/2005' and ('5/31/2005'+5)
Have seen other questions here about modifying date pickers supplied by reports created in BIDS. The answer is usually NO. But this does not involve a format change, simply want to limit say to a specific year. Any ideas?
Item Date A 2015-01-02 A 2015-03-01 A 2015-02-17 B 2015-01-07 B 2015-03-20 C 2015-01-02 C 2015-03-17 C 2015-01-14 C 2015-02-02 D 2015-03-25 E 2015-02-09 E 2015-02-09
Is it possible to get a list with only one of each item and then the latest date possible?
We received a Payment from a customer on '10/10/2007 10:30:00'. i am trying to calculate the commission we would receive from that payment. the commission rate can be edited. so i have to find what the commission rate was when that payment was received.
I have a CommisionAudit table that tracks changes in commission rate with the following values.
ID | Commission Change | UpdatedOn ---------------------------------------------- 1 | Change from 20->25 | 03/07/2007 09:00:00 ---------------------------------------------- 2 | Change from 25->35 | 10/09/2007 17:00:00 ---------------------------------------------- 3 | Change from 35->20 | 01/10/2007 16:00:00 ---------------------------------------------- 4 | Change from 20->26 | 11/10/2007 10:00:00 ----------------------------------------------
with this payment, as the commission rate had been changed on 01/10/2007 it would obviously be 20%(ID 3). But I need to write sql to cover all eventualities i.e. Before the first and after the last. any help would be most welcome.
I am trying to establish the date a database was last used.
At first i checked out sp_helpfile and sysfiles to see if there was a date last modified, i also tried using xp_cmdshell to check the date on the actual .mdf or .ldf on the o/s but if the file has not grown the date will be the create date.
I need to establish the date a database was last used, either the last time someone logged into it or the last time any sort of command was run against the database, i cannot use sysprocesses as there are no connections to the database, so i need to determine the last time there actually were any processes run against this database.
And i need to use tsql, checking audit logs etc is not viable, can anyone help ?
I am having a hard time creating a view with some complex math to chow availability for rental items. My tables are as follows:
OrderHeader
OrderID int, Site int, StartDate datetime, EndDate datetime
OrderDetail
OrderDetailID int, Site int, OrderID int, ItemName varchar(30), Qty int
ShipHeader
ShippingID int, Type int, ActionDate datetime
ShipDetail
ShipDetailID int , ShippingID int, OrderDetailID int, Qty int
Transaction
TransactionID int, Type int
PurchaseHeader
PurchaseHeaderID int, Site int PickupDate datetime, ReturnDate datetime,
PurchaseDetails
PurchaseDetailsID int, PurchaseHeaderID int, ItemName varchar(30), Qty int
I need a view that shows how many items will be available on a particular day for a specified item, date range and a specified €œSite€? (Office location. For example, NY, LA, CA, etc.)
The quantity actually owned is calculated from the Transaction table. Type, Qty are the columns. (type 1 is a purchase or addition and type 2 is a sale or subtraction. The quantity owned is the sum of all type 1s minus the sum of all type 2s.
The ShipHeader table also shows types. Type 1 is ship, Type 2 is return and Type 3 is lost.
Initially, all availability is calculated based on the FromDate and ToDate of our order headers. However, once the order has shipped, the availability should change to our ship and return dates in our ShipDetails table. Additionally, there is a PurchaseDetails table that shows items to be €œSub-Rented€? for a time frame. The PurchaseHeader table contains the FromDate and ToDate for these sub-rented items to be added to availability. If an item is kept beyond the due date set in the OrderHeader, then the item should show as unavailable from the FromDate through all future dates (as there is no way of telling when a late item will be returning. Once, the item is either returned or marked as lost, it should become available again as of that date.
The view should list dates in each row with the number of units available on that date.
Also, a second view needs to be created that shows the details for the dates listed in the first view. this is essentially a list of the orders or puchase orders that were used to calculate the number available.
I think this covers it.
As I said, this is a bit complicated. i understand what needs to be done but need assistance assembling the code to make it happen.
I'm trying to get a list of items by there last invoice date, but I keep getting errors. I have to join two table to get all the information needed to match a date with an item number.
select i.[Invoice-date], l.[Item-no] from [Invoice] as i join [invoice-line] as l on i.[Seq-no] = l.[Seq-no]  (Select MAX(I2.[Invoice-date]) as LastDate, l2.[ITEM-No]   from [invoice] AS I2   JOIN [invoice-line] AS L2   on i2.[Seq-no] = l2.[Seq-no]   GROUP BY L2.[Item-no]) ITEMNUMBER WHERE i.[invoice-date] = lastdate.[invoice-date] and l.[item-no] = itemnumber.[item-no]
However, I keep getting this..
Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'Select'. Msg 102, Level 15, State 1, Line 9 Incorrect syntax near ')'.
if the getdate() < term start date then R if the getdate() > term date date and getdate() < term end date then C if the getdate() > term end date then H
I have come up with the following type of case statement that will allow me to determine the first two values (to a degree), however , I need to evaluate the end date as well.
Is there a way to look at multiple columns in a case statement?
CASE WHEN DATEDIFF(DD,GETDATE(),TRM_BEGIN_DTE) >0 THEN 'R' else 'C' end
Hi All,I have to a few backups restored from a set of backup files. Thebackup files have been removed from the drive because of some storageconstrains...For some data comparison reasons, I need to figure out the actual dateof these backup files that is used to restore these databases. Isthere any system table I can query to figure the actual backup timesof these databases...Any help will be much appreciated.. Thanks in advance.- Aravin
I need to determine the actual date/time that a message was placed on the queue. In my "activated" procedure I want to log this information and pass it along to further processing routines. From what I can tell, the Queue table itself does not have this information captured.
I have a BOM table with all finished item receipes and semi items recipes. create a query where semi item materials are also listed in finished item recipe.
Here's my problem. I have 2 tasks defined in my Control Flow tab:
EXECUTE SQL--------->EXECUTE DTS 2000 PACKAGE
When I attempt to run it, by right-clicking the EXECUTE SQL task, and selecting "Execute Task", it only runs the EXECUTE SQL part (successfully), and does not "kick off" the EXECUTE DTS 2000 PACKAGE, after it is done running (even though it completes successfully, as shown by the green box).
Yes, they are connected by a dark green arrow, as indicated in my diagram above.
Why is this?? Am I missing something here? Need help.
I have a order table which has a orderdate and despatchdate i want to write a query in such a way that i want to get all the details from the table for a range specified date as the oderdate and despatchdate are user interactable. I search the google to solve this problem but could not find a answer
What is the best way to do a where clause that includes a date range. Ex. WHERE date1 BETWEEN @Begin Date AND @EndDate. I want to include all of the @EndDate.
I am working on a report for staff productivity, and have to get a summary figure for how much productivity was expected for a date range. The problem is that the amount expected from an employee can change if they move from full time to part time etc.
So I have a view that has the begin date, end date, expected daily production # by employee, and have to figure out how to get the multiplication to work correctly.
Example:
Employee 1 had a daily production # of 10 from 1/1/07-3/31/07 and daily production of 5 from 4/1/07 - now
If I run the production report for 1/1/07 - 6/30/07 what I want is one summary figure of for the entire range which would be 10*Datediff(d,1/1/07,3/31/07)+5*Datediff(4/1/07,6/30/07) or 890+450=1340.
Of course the actual date range for the report will be a variable, and the dates for the begin and end of a production date range will be all over the place.
I am very new to SQL and have a question, hopefully someone can answer.
I have a table of data, one of the fields is a date.
What i want to do is be able to have a query that can check if the date falls within a certain range - ie fiscal year and output in another column the fiscal year "code".
Ie: dates between 01/06/05 and 31/05/06 is fiscal year 0506 dates between 01/06/06 and 31/05/07 is fiscal year 0607
Could this query be dynamic so if a new fiscal year begins it would know to make the output the next fiscal year code???
I have this particular problem....I need to develop a stored proc where in a parameter checks the dates between JAN 1st and DEC 31st of a particular year. The parameter is declared for year..
I'll mention a small example below...
USE PUBS declare @year as varchar set @year = 1993
select * from employee where hire_date >= '@year-01-01' and hire_date<='@year-12-31'
This is just an example to show wat i want... the year is prompted to the user...he/she can select any year
I get the following error message....
Server: Msg 241, Level 16, State 1, Line 1 Syntax error converting datetime from character string.
I have a function that takes in a week number and returns data based on that week number, for one client we have a table that tells what the weeks are since they are not on a normal calendar, but their fiscal calendar. We now have another client that want the same function, but for it to return the data based on the normal calendar week. So, how can I determine the date range of a week, based on the week number?