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
4 1627
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
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
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.
try This.... enjoy knowledge freedom ......
I've used one table,table20, and a function,test12() for this. - CREATE TABLE [dbo].[test20](
-
[productionDate] [datetime] NULL,
-
[Quantity] [int] NULL,
-
[ProductName] [varchar](50) NULL
-
)
-
GO
-
-
insert into test20 values('1-1-2007', 5,'Pen')
-
insert into test20 values('1-3-2007', 7,'Stabler')
-
insert into test20 values('1-3-2007',4,'pen')
-
insert into test20 values('1-5-2007',9,'Pencil')
-
insert into test20 values('1-5-2007',2,'stabler')
-
-
GO
-
CREATE FUNCTION test12 ()
-
RETURNS @Productdates TABLE (Productiondate DATETIME NOT NULL)
-
AS
-
BEGIN
-
DECLARE @cnt INT;
-
DECLARE @StartDate DATETIME;
-
SET @cnt=0;
-
SELECT @cnt= DATEDIFF(DAY,MIN(Productiondate),MAX(Productiondate)),@StartDate=MIN(Productiondate) FROM test20
-
WHILE (@cnt>0)
-
BEGIN
-
INSERT INTO @Productdates VALUES(DATEADD(DAY,@cnt,@StartDate))
-
SET @cnt=@cnt-1
-
END
-
INSERT INTO @Productdates VALUES(@StartDate)
-
RETURN
-
END;
-
-
GO
-
-
-- Sql Query which returns Resultset
-
-
select d.productiondate,
-
d.productname,
-
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
-
FROM test20 c
-
right join
-
(
-
select a.productiondate,b.productname from test12() a
-
cross join
-
(select distinct productname from test20) b
-
) d
-
on c.productiondate=d.productiondate
-
and c.productname=d.productname
-
order by d.productiondate
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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....
|
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....
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |