?
Solved

About SQL!

Posted on 2001-09-05
9
Medium Priority
?
151 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 400 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 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

864 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