BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL query help needed

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Paweł Zalewski
Guest





PostPosted: Sat Dec 09, 2006 12:12 am    Post subject: SQL query help needed Reply with quote



Guys help needed,



I have a table X

DocNo | InvNo | VAT | Anount
----------------------------------------
1 | a1 | 7 | 100
1 | a1 | 22 | 200
2 | a2 | 0 | 50
3 | a3 | 22 | 300
3 | a3 | 7 | 10
3 | a3 | 0 | 30



I need to create a view which will look like this

DocNo | InvNo | VAT7 | Am7 | VAT22 | Am22 | VAT0 | Am0
-------------------------------------------------------------------------
1 | a1 | 7 | 100 | 22 | 200 | 0
| 0
2 | a2 | 0 | 0 | 0 | 0 |
0 | 50
3 | a3 | 7 | 10 | 22 | 300 | 0
| 30


Thanks
Paul
Back to top
Simon Owen
Guest





PostPosted: Mon Dec 11, 2006 9:10 am    Post subject: Re: SQL query help needed Reply with quote



Yu can do this using sub-queries, but they will be slow:

SELECT A.DocNo,
A.InvNo,
(SELECT B.VAT FROM TableX B Where B.DocNo = A.DocNo AND B.VAT
= 7) As VAT7,
(SELECT B.Amount FROM TableX B Where B.DocNo = A.DocNo AND
B.VAT = 7) As Am7,
(SELECT B.VAT FROM TableX B Where B.DocNo = A.DocNo AND B.VAT
= 22) As VAT22,
(SELECT B.Amount FROM TableX B Where B.DocNo = A.DocNo AND
B.VAT = 22) As Am22,
(SELECT B.VAT FROM TableX B Where B.DocNo = A.DocNo AND B.VAT
= 0) As VAT0,
(SELECT B.Amount FROM TableX B Where B.DocNo = A.DocNo AND
B.VAT = 0) As Am0
FROM
TableX A
GROUP BY
A.DocNo

The above is an example using Ansi-SQL query as you failed to specify which
SQL server you are using,, it assumes that the records are grouped by DocNo.
There are ways to optimize the above but it is dependent on the system.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.