473,385 Members | 1,919 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,385 software developers and data experts.

Problem with SQL query newbie

1
i got one for you

i have two tables in a data base.
the first one is called inspections it consits of

col1 Well_name vchar
col2 Date shortdate
col3 level numeric
grouped by well name and date is desc
second table is called delivery it consits of

col1 well_name vchar
col2 date shortdate
col3 amount numeric

what i need is to complie this data into the following table or view
col1 well_name group by
col2 date of first inspection
col3 level of first inspection
col4 date of second inspection
col5 level of second inspection
col6 sum of deliveies from the second table beteen the date in col1 and col2

this one as given me nightmares
Mar 11 '09 #1
1 1140
csenasa
10
Hi,

I was confused reading last line of your question!!!!! (col6 sum of deliveies from the second table beteen the date in col1 and col2
) because col1 contains well_name , So please redefine your question.

But in meanwhile see if you can get something out of the follwing query :)

IF EXISTS(SELECT Name FROM SysObjects WHERE Name = 'inspections ')
BEGIN
DROP TABLE inspections
END
GO
CREATE TABLE inspections
(
C1 VARCHAR(25),
C2 DATETIME,
C3 INT

)
GO
IF EXISTS(SELECT Name FROM SysObjects WHERE Name = 'delivery ')
BEGIN
DROP TABLE delivery
END
GO
CREATE TABLE delivery
(
C1 VARCHAR(25),
C2 DATETIME,
C3 INT

)
GO
INSERT INTO inspections(C1, C2 , C3 )
VALUES('A',GETDATE(),1)
GO
INSERT INTO inspections(C1, C2 , C3 )
VALUES('A',GETDATE()+1,2)
GO
INSERT INTO inspections(C1, C2 , C3 )
VALUES('B',GETDATE(),1)
GO
INSERT INTO inspections(C1, C2 , C3 )
VALUES('C',GETDATE(),1)
GO
INSERT INTO inspections(C1, C2 , C3 )
VALUES('D',GETDATE(),1)
GO

INSERT INTO delivery(C1, C2 , C3 )
VALUES('A',GETDATE()+1,1)
GO
INSERT INTO delivery(C1, C2 , C3 )
VALUES('B',GETDATE()+1,1)
GO
INSERT INTO delivery(C1, C2 , C3 )
VALUES('C',GETDATE()+1,1)
GO
INSERT INTO delivery(C1, C2 , C3 )
VALUES('D',GETDATE()+1,1)
GO

SELECT ins.C1 , ins.C2 , ins.C3 ,del.C2 , del.C3 , SUM(ins.C3) as SumOfdel
FROM delivery del ,
(SELECT ins.C1 , MIN(ins.C2) as MinDate , MAX(ins.C2) as MaxDate --, ins.C3 , del.C2 , del.C3
FROM inspections ins
GROUP BY ins.C1) inspect , inspections ins
WHERE del.C1 = inspect.C1 and ins.C1 = inspect.C1
and del.C2 BETWEEN inspect.MinDate AND inspect.MaxDate
GROUP BY ins.C1 , ins.C2 , ins.C3 ,del.C2 , del.C3
/*
col1 well_name group by
col2 date of first inspection
col3 level of first inspection
col4 date of second inspection
col5 level of second inspection
col6 sum of deliveies from the second table beteen the date in col1 and col2
*/
Mar 13 '09 #2

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

Similar topics

2
by: newbie_mw | last post by:
Hi, I need urgent help with a novice problem. I would appreciate any advice, suggestions... Thanks a lot in advance! Here it is: I created a sign-up sheet (reg.html) where people fill in their...
1
by: Antar | last post by:
Hi, I'm kind of a newbie on DB management but I have to deal with a huge DB used for real time operations. I got a temporal table where current data is stored to work with frecuently, and then a...
8
by: Thomas A. Anderson | last post by:
I have ran into a rut! I have google'd, Yahoo'd, but could not find the proper syntax. I have spent over two day trying to figure this out, and decided that I will have to ask for help from the...
11
by: ASP newbie | last post by:
I cannot run my asp.net application in w2k server. But the program works fine under w2k professional. Can anyone tell me is there any difference in the settings? Many thanks.
3
by: Marco van de Voort | last post by:
Hi, I'm a relative db newbie, and total SQL server newbie and I garbled up the following query ( :xxx are parameters that are filled in by the app) SELECT PERSON.*, CASE...
2
by: Arsalan Ahmad | last post by:
Hi, May be I am a newbie, or may be i dont have that much insight in following systems ..i.e. why i have some confusions as below: In many websites, when search is performed on some keywords...
8
by: Harry Strybos | last post by:
Visual Studio 2005 - SP1 - VB.Net on WinXP SP2 I add a typed dataset to my solution and get the following errors : Error 1 sub 'ReadXmlSerializable' cannot be declared 'Overrides' because it...
5
by: yoyoz | last post by:
Help!!!! i am newbie to php, i was trying to establish the connection to another machine (solaris) so that i can retrieve data from the database stored inside there using my own PC. for your...
6
Lensmeister
by: Lensmeister | last post by:
Hi, As a newbie here I hope someone cane help me. I have Acess 2003 and am making a database that holds records or football matches. I haven't created any relationships as yet. The main table...
9
ajhayes
by: ajhayes | last post by:
Hello everyone, This is my first time posting here and I'm hoping someone can help me out. I'm a relative newbie to Access and am pretty much learning as I go along, so please bear with me. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.