I'm trying to construct code that will return the last non-NULL value in a column containing daily records.
For E.G. I want to know what the LAST value of Description field when it is not NULL, AND the Date is within the range t=1 to t=5 => i.e. "Dog" in the below example:
When im using the below query im getting the output, but when i change the starting date to 2006 I'm not getting the data for 2007 though it falls between the 2006 and 2008 range...
select * From dbname..tbl where date>= '03/jan/2007' and date <= '11/feb/2008' and Status= 'C' and ID is not null
AND (ACCOUNT = '25869' or ACCOUNT = '0' + '25869' ) Check and post your comments ASAP...
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!!
For some reason this is just not "clicking" with me and the longer I stare at it the more I overcomplicate things and then I just confuse myself further...
Provided this relation showing where "pkey" and "skey" met on "ServiceDate":
I need to select out a list of distinct pkeys and skeys only when there was no meeting between the two in November 2005. In other words, this query would produce only one record - the starred record - when run against this sample table. This is because even though, for example, skey 124 / pkey 2 has an entry outside the desired range, it also has one inside the desired range. The same goes for the record outside our range for 123 and 2 - it also has a record inside our range.
At this point, I've come to the conclusion that I can first select all the distinct pkeys and skeys where the servicedate is not in 11/2005 then join it to a selection of distinct pkeys and skeys where the servicedate IS in the desired date range. Does that seem like the most straightforward way through this?
I don't get the impression that this is that complicated a problem, but it's one of those deals where I goofed up somewhere along the line, and now I think I'm really overthinking the problem, so I'd be much obliged if someone could give me a hearty slap to clear the ol' noggin.
I want to run a query that returns say 100 records...but I only want to return first 10 for first page on a web page, then on page 2 the webpage will return rows 11 to 20 of the same SQL statement...page 3 returns rows 21 to 30 rows etc....(eg. like Google or bulletin boards, browsing auctions in ebay etc.).
I could probably get my application logic to handle this (ASP.net), for instance I could possibly get a datareader to skip the first 10, output the next 10 then stop for page 2 (records between 11 amnd 20) but is there way to do this in SQL Server at the database level using an SQL Statement?
I can use TOP 10 to get the first set of records for the first page eg.
SELECT TOP 10* FROM Suppliers
...but how do I get between 11 and 20, 21 and 30 and so on?
I've already mentioned I could handle this in my application logic, but then each time the same SQL Statement is fetching all 100 records, even though the web page will only display a certain range. I'm building an intranet website that can potentially run queries that return 100'000s records, even though initially only top 20 or so records are display, each page they subsequently go to will rerun that same query that returns all 100'000 records. So handling this as part of the query would be better for performancr I reckon.
Data(id int, product_id int, property_id int, value float)
Data.id references Headers.id
Headers.id is a primary key, Data has clustered index (id, product_id, property_id)
Headers has several thousand rows, Data several million. I want to return all rows from Data for a given product_id and a given property_id such that Header.id is in a given range.
Right now I am doing
SELECT id, time, value FROM Headers H, Data D WHERE H.id = D.id AND H.time >= @StartTime AND H.time <= @EndTime AND D.product_id = @ProductID AND d.property_id = @PropertyID
This query can take 10+ seconds to run, though once I run it for a given product_id, queries for different values of property_id are much faster. Try a different product_id, and it takes longer. Given that there are millions of records in Data, is it reasonable for it to take this long? The index was suggested by Query Analyzer's Index Tuning Wizard, and I tried a couple variations on the query without any noticeable performance improvement. But, I'm no DBA...anyone have any tips? I googled a bit but couldn't figure out the right way to phrase my question to find any good info...thanks in advance
Hello all, thanks in advance for any help you might be able to give.
I'm familiar with the Top command but I need something else to help in a project I'm working on.
I would like to select rows 1 through 100000 from a specific table in one query then 100001 through 200000 in a second query and 200001 through 300000 in a third and so on until I have gone through all rows. There happens to be 424000 in the table I'm working on.
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 a table here.  I want  find a way of getting the latest date, when the code is the same.  If the Declined date is null.  Then I still want the latest date.  E.g. ID 3. Â
If the declined date is filled in. Â Then I want to get the row, when the Datein column value is greater then the declined date only.
I tried grouping it by max date, but  i got an error message when trying this out.  Against the code Â
WHERE MAX(Datein) > Declined
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. Â What do I need to do to get both my outputs working?Â
I have soma ado.net code that inserts 7 parameters in a database ( a date, 6 integers). I also use a self incrementing ID but the date is set as primary key because for each series of 6 numbers of a certain date there may only be 1 entry. Moreover only 1 entry of 6 integers is possible for 2 days of the week, (tue and fr). I manage to insert a row of data in the database, where the date is set as smalldatetime and displays as follows: 1/05/2007 0:00:00 in the table. I want to retrieve the series of numbers for a certain date that has been entered (without taking in account the hours and seconds). A where clause seems to be needed but I don’t know the syntax or don’t find the right function I use the following code to insert the row :
and the following code to get the row back (to put in arraylist):
“SELECT C1, C2, C3, C4, C5, C6 FROM Series WHERE (LDate = Today())� WHERE LDate = '" + DateTime.Today.ToString() + "'"
Which is the correct syntax? Is there a better way to insert and select based on the date?
I don’t get any error messages and the code executes fine but I only get an empty datatable in my dataset (the table isn’t looped for rows I noticed while debugging). Today’s date is in the database but isn’t found by my tsql code I think.
i need to perform some searching on 10 columns. Since it will return >1 records/lines, i need to filter, if @param1 match with col1, then abandon those Null value in column1 set, and find match @param2 to col1. @param1='SS', @param2='' (if '' i set to %) til 10
problem is when i want to search only if colX is 'something', it returns null value,.. my where clause
WHERE (d.RecvUDF1 LIKE @UDF1 OR d.RecvUDF1 IS NULL) AND(d.RecvUDF2 LIKE @UDF2 OR d.RecvUDF2 IS NULL) AND(d.RecvUDF3 LIKE @UDF3 OR d.RecvUDF3 IS NULL) AND(d.RecvUDF4 LIKE @UDF4 OR d.RecvUDF4 IS NULL) AND(d.RecvUDF5 LIKE @UDF5 OR d.RecvUDF5 IS NULL ) AND(d.RecvUDF6 LIKE @UDF6 OR d.RecvUDF6 IS NULL) AND(d.RecvUDF7 LIKE @UDF7 OR d.RecvUDF7 IS NULL ) AND(d.RecvUDF8 LIKE @UDF8 OR d.RecvUDF8 IS NULL) AND(d.RecvUDF9 LIKE @UDF9 OR d.RecvUDF9 IS NULL) AND(d.RecvUDF10 LIKE @UDF10 OR d.RecvUDF10 IS NULL )
-i wrote the OR NULL is because i need those nulls value when im not searching for that column.. -i replace '' to '%' because i need to list any other remaining unmatched columns when i found the matching column.
maybe it's quite ridiculous to understand my Q ...coz i couldnt find any other better way to explain thanks in advance
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?
I have a stored procedure that allows users to select addresses based on partially supplied information from the user. My procedure seems to work fine in all but a few cases. If a user decides to select all the rows for a particular country the procedure below does not return any rows even if the rows exist. I tracked this down to the fact that for Non US countries I set both the StateCode and Country Code to nulls. . Below is a partial version of my code. Can anyone show me how I can do a "Like" Search even if some of the fields in the row contain null values? Thanks 1 CREATE PROCEDURE dbo.Addresses_Like( @SendTo VarChar(50) = Null 2 , @AddressLine1 VarChar(50) = Null 3 , @AddressLine2 VarChar(50) = Null 4 , @City VarChar(50) = Null 5 , @StateCode VarChar(2) = Null 6 , @ZipCode VarChar(10) = Null 7 , @CountryCode VarChar(2) = Null) 8 9 10 Declare @SearchSendTo VarChar(50) 11 Declare @SearchAddressLine1 VarChar(50) 12 Declare @SearchAddressLine2 VarChar(50) 13 Declare @SearchCity VarChar(50) 14 Declare @SearchStateCode VarChar(2) 15 Declare @SearchZipCode VarChar(10) 16 Declare @SearchCountryCode VarChar(2) 17 18 If (@SendTo Is Null) 19 Set @SearchSendTo = "" 20 Else 21 Set @SearchSendTo = @SendTo 22 23 If (@AddressLine1 Is Null) 24 Set @SearchAddressLine1 = "" 25 Else 26 Set @SearchAddressLine1 = @AddressLine1 27 28 If (@AddressLine2 Is Null) 29 Set @SearchAddressLine2 = "" 30 Else 31 Set @SearchAddressLine2 = @AddressLine2 32 33 If (@City Is Null) 34 Set @SearchCity = "" 35 Else 36 Set @SearchCity = @City 37 38 If (@StateCode Is Null) 39 Set @SearchStateCode = "" 40 Else 41 Set @SearchStateCode = @StateCode 42 43 If (@ZipCode Is Null) 44 Set @SearchZipCode = "" 45 Else 46 Set @SearchZipCode = @ZipCode 47 48 If (@CountryCode Is Null) 49 Set @SearchCountryCode = "" 50 Else 51 Set @SearchCountryCode = @CountryCode 52 53 54 Select AddressID 55 , SendTo 56 , AddressLine1 57 , AddressLine2 58 , City 59 , StateCode 60 , ZipCode 61 , CountryCode 62 , RowVersion 63 , LastChangedDateTime 64 , OperID 65 From Addresses 66 Where SendTo Like RTrim(LTrim(@SearchSendTo)) + "%" 67 And AddressLine1 Like RTrim(LTrim(@SearchAddressLine1)) + "%" 68 And AddressLine2 Like RTrim(LTrim(@SearchAddressLine2)) + "%" 69 And City Like RTrim(LTrim(@SearchCity)) + "%" 70 And StateCode Like RTrim(LTrim(@SearchStateCode)) + "%" 71 And ZipCode Like RTrim(LTrim(@SearchZipCode)) + "%" 72 And CountryCode Like RTrim(LTrim(@SearchCountryCode)) + "%" 73 Order By CountryCode, City, AddressLine1, AddressLine2, SendTo
Hi All to Transact-SQL Community, I had a problem with NULL selecting, actually i had two tables like,
Transaction Table
ID Image Link INTID(ForeignKey) -------------------------------------------------------------------- 1 test test 2 2 test1 test1 2 3 test2 test2 NULL 4 test3 test3 NULL
Master Table
INTID Path Link ------------------------------------------------- 1 a.gif test 2 b.gif test
When i am selecting from transaction table, if column INTID has a integer value the it should do innerjoin with Mastertable on TransactionTable.INTID = MasterTable.INTID, and fetch rows TransactionTable and MasterTable, then result should be like this, 1 test test 2 test1 test1 2 b.gif test
and if NULL present then it should fetch only rows from transaction table. Can anyone help on how to do this.
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 want to run a query that selects rows from the table where a datetime column has null values; select * from Orders where IsNull(dClosedDate,'Null') = 'Null' However i get this error: Conversion failed when converting datetime from character string. Any help appreciated
Hi, i have something like this: SELECT a.boardname, a.description, a.threadcount, a.answercount, a.lastthreadid, b.username, b.subject, b.created FROM forum_board AS a, forum_topics AS b WHERE (a.forumid = @ID) AND (b.id = a.lastthreadid) The problem here is that lastthreadid does not always have a post linked to it, so sometimes its null. I want it to still return all the boards and then just returnnull values or "" in b.username, b.subject, b.createdThis returns nothing. How can i force it to select these values?
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.