473,416 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How To Select All The Dates Of of a given period Even Though There Are No Records?

Hi,

I have one SQL Table with 3 columns as below


Column1: ProductionDate - DateTime - Not NULL
Column2: Quantity - Int - Not NULL
Column3: ProductName -string - not null
Now There are 5 Records in Table

1-1-2007, 5,Pen
1-3-2007, 7,Stabler
1-3-2007,4,pen
1-5-2007,9,Pencil
1-5-2007,2,stabler


Output of Result should be as below

1-1-2007 5,pen
1-1-2007 0,stabler
1-1-2007 0,pencil
1-2-2007 5,pen
1-2-2007 0,stabler
1-2-2007 0,pencil
1-3-2007 4,pen
1-3-2007 7,stabler
1-3-2007 0,pencil
1-4-2007 4,pen
1-4-2007 7,stabler
1-4-2007 0,pencil
1-5-2007 4,pen
1-5-2007 2,stabler
1-5-2007 9,pencil

Means Query should return all the dates of Month with Quantity and productName and if there is no entry in Table then the previous days quantity for Quantity.

How to Do it? Please suggest with Query
Nov 11 '10 #1
4 1627
code green
1,726 Expert 1GB
A query is possible with a sub-query that gets MAX date less than returned date.
But I think this is a database design flaw.
Well it isn't actually a database, it is simply a flat file.
It doesn't store the data you need in a format that can be retrieved without doing SQL handsprings and will get worse.

Can't think what the design should at the moment
Nov 11 '10 #2
ck9663
2,878 Expert 2GB
Do you have a table that contains all the Product? Does it has Product ID that can be used to link the two tables?

~~ CK
Nov 11 '10 #3
It's been a long while since I've done much SQL, but if I remember correctly you'll need to use concepts such as an outer and inner join. It looks like it will end up being a very expensive operation as a query. Programattically (is that even a word?), the solution would be relatively simple and straight forward.
Nov 11 '10 #4
try This.... enjoy knowledge freedom ......

I've used one table,table20, and a function,test12() for this.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE [dbo].[test20](
  2.     [productionDate] [datetime] NULL,
  3.     [Quantity] [int] NULL,
  4.     [ProductName] [varchar](50) NULL
  5. GO
  6.  
  7. insert into test20 values('1-1-2007', 5,'Pen')
  8. insert into test20 values('1-3-2007', 7,'Stabler')
  9. insert into test20 values('1-3-2007',4,'pen')
  10. insert into test20 values('1-5-2007',9,'Pencil')
  11. insert into test20 values('1-5-2007',2,'stabler')
  12.  
  13. GO
  14. CREATE FUNCTION test12 ()
  15. RETURNS @Productdates TABLE (Productiondate DATETIME NOT NULL)
  16. AS
  17. BEGIN
  18. DECLARE @cnt INT;
  19. DECLARE @StartDate DATETIME;
  20. SET @cnt=0;
  21. SELECT @cnt= DATEDIFF(DAY,MIN(Productiondate),MAX(Productiondate)),@StartDate=MIN(Productiondate) FROM test20 
  22. WHILE (@cnt>0)
  23.     BEGIN
  24.     INSERT INTO @Productdates VALUES(DATEADD(DAY,@cnt,@StartDate))
  25.     SET @cnt=@cnt-1
  26.     END
  27. INSERT INTO @Productdates VALUES(@StartDate)
  28. RETURN
  29. END;
  30.  
  31. GO
  32.  
  33. -- Sql Query which returns Resultset
  34.  
  35. select    d.productiondate,
  36.         d.productname, 
  37.         isnull(quantity,isnull((select quantity from test20 where productname=d.productname and productiondate =(select max(productiondate) from test20 where productiondate<d.productiondate)) ,0) ) as Qty
  38. FROM test20 c
  39. right join 
  40. (
  41.     select a.productiondate,b.productname from test12() a
  42.     cross join 
  43.     (select distinct productname from test20) b 
  44. ) d 
  45.     on         c.productiondate=d.productiondate
  46.          and c.productname=d.productname
  47. order by d.productiondate
Nov 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: www | last post by:
Hi there, I need to calculate working days for a given period, "Date from", "Date To". Plus I want to be able to insert Public Holidays to exclude aswell. Your help will be greatly appreciated....
1
by: Abby Lee | last post by:
My page runs fine...I just get the yeld sign with a "!" in it. Ok, I asked this question in the VBscript group and they tell me it's a JavaScript issue even though I don't get the error until I...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
2
by: wajih.boukaram | last post by:
Hi I've been using asp.net for a couple of days now and i think I've gotten the hang of things I do have a problem when using HttpWebRequest: i request a page from a remote server and this...
1
by: DR | last post by:
mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true in tsql: select...
0
by: service0086 | last post by:
Calvin Klein undergarments have a prominent presence on its website. This brand is quite popular among modern men. They are made from finest and softest fabrics to give that comfort you desire for....
0
by: service0031 | last post by:
Housekeeping is an interesting job or career where things are always changing, and even though you may work in a less than clean environment, you are required to always look clean and professional....
4
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. ...
1
by: popovaa | last post by:
Hi: i need to get data for a time frame in a current year and same time frame in previous year. i found the below on this site which is almost what i need but i also need help on how to adjust the...
1
oranoos3000
by: oranoos3000 | last post by:
hi i read below text and i understand what say but i dont identify meaning of this setence preg_match may not fail on 4 and 5 octet sequences, even though they are not supported by the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.