?
Solved

About SQL!

Posted on 2001-09-05
9
Medium Priority
?
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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