Solved

About SQL!

Posted on 2001-09-05
9
143 Views
Last Modified: 2010-04-06
Table1?F
ID    starttime  overtime
A01   2001-9-4  2001-9-6
B01   2001-9-3  2001-9-5

Table2?F
ID    state  Num
A01    A     5
A01    B     3
B01    A     4
B01    B     2

Query result ?F

Time      state  Num
2001-9-3   A     4
2001-9-3   B     2
2001-9-4   A     9
2001-9-4   B     5
2001-9-5   A     9
2001-9-5   B     5
2001-9-6   A     5
2001-9-6   B     3

How to write this 'SQL' sentence ?
0
Comment
Question by:lingxin
9 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6459490
SQL.Add('SELECT Table1?F.ID, Table1?F.starttime, Table1?F.overtime, Table2?F.State, Table2?F.Num');
SQL.Add('FROM Table2?F INNER JOIN Table1?F ON Table2?F.Id = ''Table1?F.Id''');
SQL.Add('ORDER BY Table1?F.starttime);


The Crazy One
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 100 total points
ID: 6459508
Oops sorry I see you don't want the ID or the overtime. And made a few other mistakes so disregard my first comment.

with Query1 do begin
  Close;
     SQL.Clear;
     SQL.Add('SELECT Table1?F.starttime, Table2?F.State, Table2?F.Num');
     SQL.Add('FROM Table1?F INNER JOIN Table2?F ON Table1?F.Id = Table2?F.Id');
     SQL.Add('ORDER BY Table1?F.starttime);
     If not Prepared then Prepare;
     Open;
     First;
end;
0
 
LVL 2

Assisted Solution

by:lucika
lucika earned 100 total points
ID: 6459701
If the tables to set so:

Table1?F
ID    starttime  overtime
A01   2001-9-4  2001-9-7
B01   2001-9-3  2001-9-6

Table2?F
ID    state  Num
A01    A     5
A01    B     3
B01    A     4
B01    B     2

What result of query will be exact?
1) or 2)?

1)
Time      state  Num
2001-9-3   A     4
2001-9-3   B     2
2001-9-4   A     9
2001-9-4   B     5
2001-9-5   A     9
2001-9-5   B     5
2001-9-6   A     9
2001-9-6   B     5
2001-9-7   A     5
2001-9-7   B     3

2)
Time      state  Num
2001-9-3   A     4
2001-9-3   B     2
2001-9-4   A     9
2001-9-4   B     5
2001-9-6   A     9
2001-9-6   B     5
2001-9-7   A     5
2001-9-7   B     3

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 2

Expert Comment

by:lucika
ID: 6459872
SELECT T3.starttime, Table2?F.State, sum(Table2?F.Num)
FROM Table1?F t3, Table1?F INNER JOIN Table2?F ON Table1?F.Id = Table2?F.Id
where t3.starttime between Table1?F.starttime and Table1?F.overtime
group by t3.starttime, Table2?F.state

union

SELECT T3.overtime, Table2?F.State, sum(Table2?F.Num)
FROM Table1?F t3, Table1?F INNER JOIN Table2?F ON Table1?F.Id = Table2?F.Id
where t3.overtime between Table1?F.starttime and Table1?F.overtime
group by t3.overtime, Table2?F.state;
0
 
LVL 2

Expert Comment

by:lucika
ID: 6460287
It would be desirable to hear your commentings.
My answer obeys problems?
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 6461273
What's this got to do with Delphi?
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6461721
What database do you want to use? And a answer to lucika first comment is required...

Regards Jacco

0
 

Expert Comment

by:PhilatHome
ID: 6463938
Table1?F
ID    starttime  overtime
A01   2001-9-4  2001-9-7
B01   2001-9-3  2001-9-6

Table2?F
ID    state  Num
A01    A     5
A01    B     3
B01    A     4
B01    B     2

SQL will not produce a result from every record in table1, using INNER JOIN, if there is not an entry in Table2 for every possible ID.
So LEFT JOIN may be better

SELECT a.overtime,b.state,b.Num
FROM Table1 a
LEFT JOIN Table2 on a.ID = b.ID

Cheers
0
 
LVL 17

Expert Comment

by:geobul
ID: 9295533
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

split points between CrazyOne and lucika

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 171
Graphics32 under Delphi 10.1 Berlin 2 99
can i put an image from resource into image tag ? 6 27
DBCtrlGrid, Delphi, Scroll 7 26
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question