 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paweł Zalewski Guest
|
Posted: Sat Dec 09, 2006 12:12 am Post subject: SQL query help needed |
|
|
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
|
Posted: Mon Dec 11, 2006 9:10 am Post subject: Re: SQL query help needed |
|
|
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 |
|
 |
|
|
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
|
|