Solved

About SQL!

Posted on 2001-09-05
9
140 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:lucika
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

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

Expert Comment

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

Expert Comment

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

Regards Jacco

0
 

Expert Comment

by:PhilatHome
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now