473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sql-problem

Dear All,
To return the recordsset I wanted I had to use 3 view OR IS THERE A BETTER
SOLUTION ( 1 view, 1 SP ) ?????????
TABLE1= ORDER (ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY, ORD_PHTI_ID ....)
TABLE2: INVOICE( INV_CODE, INV_INVT_ID, .....)
TABLE3: INVOICE_ORDER_PHASE_MAP( INVOPM_INV_ID,
INVOPM_OP_ORD_ID,INVOPM_OP_SEQ_NR ) mapping orders and invoices per phase
(seq_nr)
I want all order WHERE (ORD_PHTI_ID = 17) AND
(ORD_REQUESTED_DELIVERY_DATE '2006-01-01')
and show the ORD_CLIENT_CODE, ORD_CREATION_DATE, ORD_REQUESTED_QUANTITY and
INV_CODE if the invoice is of type = 4 (Outgoing Invoice)
This are some rows of the resultset

a3 ORD_CLIENT_CODE ORD_CREATION_DATE ORD_REQUESTED_QUANTITY INV_CODE
INV_INVT_ID
PT_V06-0330_SV 12/07/2006 21 TMAL_IN_07_90222 4
PT_V06-0363_PT 22/08/2006 309 TMAL_IN_07_90223 4
PT_V06-0365_PT 24/08/2006 280 TMAL_IN_07_90223 4
PT_MAL_V06-0358_LaES 16/08/2006 592 TMAL_IN_07_90224 4
PT_V06-0497_FI 8/11/2006 60
PT_V07-0169_PL 16/03/2007 59
PT_V06-0655_ES 26/12/2006 54


View = 'a1'

SELECT ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY
FROM dbo. [ ORDER ]
WHERE (ORD_PHTI_ID = 17) AND (ORD_REQUESTED_DELIVERY_DATE >
'2006-01-01')

View = 'a2'

SELECT dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_OP_ORD_ID,
dbo.INVOICE.INV_CODE, dbo.INVOICE.INV_INVT_ID
FROM dbo.INVOICE INNER JOIN
dbo.INVOICE_ORDER_PHASE_MAP ON dbo.INVOICE.INV_ID =
dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_INV_ID
WHERE (dbo.INVOICE.INV_INVT_ID = 4)

View = 'a3' >Resultset

SELECT dbo.a1.ORD_CLIENT_CODE, dbo.a1.ORD_CREATION_DATE,
dbo.a1.ORD_REQUESTED_QUANTITY, dbo.a2.INV_CODE, dbo.a2.INV_INVT_ID
FROM dbo.a1 LEFT OUTER JOIN
dbo.a2 ON dbo.a1.ORD_ID = dbo.a2.INVOPM_OP_ORD_ID
THANKS˛,

Filip
May 3 '07 #1
3 1534
Filips Benoit wrote:
To return the recordsset I wanted I had to use 3 view OR IS THERE A BETTER
SOLUTION ( 1 view, 1 SP ) ?????????
[snip]
View = 'a1'

SELECT ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY
FROM dbo. [ ORDER ]
WHERE (ORD_PHTI_ID = 17) AND (ORD_REQUESTED_DELIVERY_DATE >
'2006-01-01')

View = 'a2'

SELECT dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_OP_ORD_ID,
dbo.INVOICE.INV_CODE, dbo.INVOICE.INV_INVT_ID
FROM dbo.INVOICE INNER JOIN
dbo.INVOICE_ORDER_PHASE_MAP ON dbo.INVOICE.INV_ID =
dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_INV_ID
WHERE (dbo.INVOICE.INV_INVT_ID = 4)

View = 'a3' >Resultset

SELECT dbo.a1.ORD_CLIENT_CODE, dbo.a1.ORD_CREATION_DATE,
dbo.a1.ORD_REQUESTED_QUANTITY, dbo.a2.INV_CODE, dbo.a2.INV_INVT_ID
FROM dbo.a1 LEFT OUTER JOIN
dbo.a2 ON dbo.a1.ORD_ID = dbo.a2.INVOPM_OP_ORD_ID
Try this:

select o.ORD_CLIENT_CODE,
o.ORD_CREATION_DATE,
o.ORD_REQUESTED_QUANTITY,
i.INV_CODE,
i.INV_INVT_ID
from ORDER o
left join INVOICE_ORDER_PHASE_MAP io
on o.ORD_ID = io.INVOPM_OP_ORD_ID
left join INVOICE i
on io.INVOPM_INV_ID = i.INV_ID and i.INV_INVT_ID = 4
where o.ORD_PHTI_ID = 17
and o.ORD_REQUESTED_DELIVERY_DATE {d '2006-01-01'}
May 3 '07 #2
On Thu, 03 May 2007 20:12:39 GMT, Filips Benoit wrote:
>Dear All,
To return the recordsset I wanted I had to use 3 view OR IS THERE A BETTER
SOLUTION ( 1 view, 1 SP ) ?????????
Hi Filips,

Below are some possibilities, but I couldn't test either of them. It's
much easier to post good answers if you use CREATE TABLE statements to
describe the table instead of just a column list, and INSERT statemenst
for the data instead of a printed table.

The easiest alternative is to just enclose the definitions of the first
two views in the third one:

SELECT a1.ORD_CLIENT_CODE, a1.ORD_CREATION_DATE,
a1.ORD_REQUESTED_QUANTITY, a2.INV_CODE, a2.INV_INVT_ID
FROM (SELECT ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY
FROM dbo.ORDER
WHERE ORD_PHTI_ID = 17
AND ORD_REQUESTED_DELIVERY_DATE '2006-01-01') AS a1
LEFT JOIN (SELECT iopm.INVOPM_OP_ORD_ID, i.INV_CODE, i.INV_INVT_ID
FROM dbo.INVOICE AS i
INNER JOIN dbo.INVOICE_ORDER_PHASE_MAP AS iopm
ON i.INV_ID = iopm.INVOPM_INV_ID
WHERE i.INV_INVT_ID = 4) AS a2
ON a1.ORD_ID = a2.INVOPM_OP_ORD_ID;

The second alternative is a quite straight derivation from the one
above. The unusual order of the joins (clarified with parentheses) makes
it a bit hard to grasp

SELECT o.ORD_CLIENT_CODE, o.ORD_CREATION_DATE,
o.ORD_REQUESTED_QUANTITY, a2.INV_CODE, a2.INV_INVT_ID
FROM dbo.ORDER AS o
LEFT JOIN ( dbo.INVOICE_ORDER_PHASE_MAP AS iopm
INNER JOIN dbo.INVOICE AS i
ON i.INV_ID = iopm.INVOPM_INV_ID)
ON iopm.INVOPM_OP_ORD_ID = o.ORD_ID
AND i.INV_INVT_ID = 4
WHERE o.ORD_PHTI_ID = 17
AND o.ORD_REQUESTED_DELIVERY_DATE '2006-01-01';

The third alternative is a shot in the dark - depending on the exact
table structure, constraints, and your data, this might or might not be
result in the same output as the original query:

SELECT o.ORD_CLIENT_CODE, o.ORD_CREATION_DATE,
o.ORD_REQUESTED_QUANTITY, a2.INV_CODE, a2.INV_INVT_ID
FROM dbo.ORDER AS o
LEFT JOIN dbo.INVOICE_ORDER_PHASE_MAP AS iopm
ON iopm.INVOPM_OP_ORD_ID = o.ORD_ID
LEFT JOIN dbo.INVOICE AS i
ON i.INV_ID = iopm.INVOPM_INV_ID
AND i.INV_INVT_ID = 4
WHERE o.ORD_PHTI_ID = 17
AND o.ORD_REQUESTED_DELIVERY_DATE '2006-01-01';

And finally, here's the fourth alternative that uses a standard trick to
change an awkward LEFT JOIN in a simple RIGHT JOIN:

SELECT o.ORD_CLIENT_CODE, o.ORD_CREATION_DATE,
o.ORD_REQUESTED_QUANTITY, a2.INV_CODE, a2.INV_INVT_ID
FROM dbo.INVOICE AS i
INNER JOIN dbo.INVOICE_ORDER_PHASE_MAP AS iopm
ON iopm.INVOPM_INV_ID = i.INV_ID
RIGHT JOIN dbo.ORDER AS o
ON o.ORD_ID = iopm.INVOPM_OP_ORD_ID
AND i.INV_INVT_ID = 4
WHERE o.ORD_PHTI_ID = 17
AND o.ORD_REQUESTED_DELIVERY_DATE '2006-01-01';

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
May 3 '07 #3
THANKS,
SELECT dbo.[ORDER].ORD_CLIENT_CODE, dbo.[ORDER].ORD_CREATION_DATE,
dbo.[ORDER].ORD_REQUESTED_QUANTITY, dbo.INVOICE.INV_CODE,
dbo.INVOICE.INV_INVT_ID
FROM dbo.INVOICE INNER JOIN
dbo.INVOICE_ORDER_PHASE_MAP ON dbo.INVOICE.INV_ID =
dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_INV_ID RIGHT OUTER JOIN
dbo.[ORDER] ON
dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_OP_ORD_ID = dbo.[ORDER].ORD_ID AND
dbo.INVOICE.INV_INVT_ID = 4
WHERE (dbo.[ORDER].ORD_PHTI_ID = 17) AND
(dbo.[ORDER].ORD_REQUESTED_DELIVERY_DATE '2006-01-01')

Forgot I have to use AND dbo.INVOICE.INV_INVT_ID = 4 in the join-part and
not in the where-part.

Filip

"Filips Benoit" <be***********@telenet.bewrote in message
news:Xm**********************@phobos.telenet-ops.be...
Dear All,
To return the recordsset I wanted I had to use 3 view OR IS THERE A BETTER
SOLUTION ( 1 view, 1 SP ) ?????????
TABLE1= ORDER (ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY, ORD_PHTI_ID ....)
TABLE2: INVOICE( INV_CODE, INV_INVT_ID, .....)
TABLE3: INVOICE_ORDER_PHASE_MAP( INVOPM_INV_ID,
INVOPM_OP_ORD_ID,INVOPM_OP_SEQ_NR ) mapping orders and invoices per phase
(seq_nr)
I want all order WHERE (ORD_PHTI_ID = 17) AND
(ORD_REQUESTED_DELIVERY_DATE '2006-01-01')
and show the ORD_CLIENT_CODE, ORD_CREATION_DATE, ORD_REQUESTED_QUANTITY
and INV_CODE if the invoice is of type = 4 (Outgoing Invoice)
This are some rows of the resultset

a3 ORD_CLIENT_CODE ORD_CREATION_DATE ORD_REQUESTED_QUANTITY INV_CODE
INV_INVT_ID
PT_V06-0330_SV 12/07/2006 21 TMAL_IN_07_90222 4
PT_V06-0363_PT 22/08/2006 309 TMAL_IN_07_90223 4
PT_V06-0365_PT 24/08/2006 280 TMAL_IN_07_90223 4
PT_MAL_V06-0358_LaES 16/08/2006 592 TMAL_IN_07_90224 4
PT_V06-0497_FI 8/11/2006 60
PT_V07-0169_PL 16/03/2007 59
PT_V06-0655_ES 26/12/2006 54


View = 'a1'

SELECT ORD_ID, ORD_CLIENT_CODE, ORD_CREATION_DATE,
ORD_REQUESTED_QUANTITY
FROM dbo. [ ORDER ]
WHERE (ORD_PHTI_ID = 17) AND (ORD_REQUESTED_DELIVERY_DATE >
'2006-01-01')

View = 'a2'

SELECT dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_OP_ORD_ID,
dbo.INVOICE.INV_CODE, dbo.INVOICE.INV_INVT_ID
FROM dbo.INVOICE INNER JOIN
dbo.INVOICE_ORDER_PHASE_MAP ON dbo.INVOICE.INV_ID =
dbo.INVOICE_ORDER_PHASE_MAP.INVOPM_INV_ID
WHERE (dbo.INVOICE.INV_INVT_ID = 4)

View = 'a3' >Resultset

SELECT dbo.a1.ORD_CLIENT_CODE, dbo.a1.ORD_CREATION_DATE,
dbo.a1.ORD_REQUESTED_QUANTITY, dbo.a2.INV_CODE, dbo.a2.INV_INVT_ID
FROM dbo.a1 LEFT OUTER JOIN
dbo.a2 ON dbo.a1.ORD_ID = dbo.a2.INVOPM_OP_ORD_ID
THANKS˛,

Filip

May 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
10
by: Dagwood | last post by:
Good morning: At least it's morning where I am. :) I have a rather newbie question I'm afraid. I have VisualStudio.NET, and have installed it along with SQL server. However I can't seem to...
2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
9
by: Grim Reaper | last post by:
My work let me put SQL Server 7.0 Enterprise Edition on my laptop. I have never setup a server from the beginning, so I am a little new at creating server groups. Alright, I am trying to create...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
1
by: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan...
1
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005...
16
by: Jeremy S. | last post by:
I'm about to install VS.NET 2005 and SQL Server 2005 on a new/clean development machine (XP Pro/SP2 etc). Is the order of installation important (i.e., which product should I install first)? ...
10
by: amjad | last post by:
how to connection sql server table with aspx like pulling data from table to grid view.... simple example to start .... thanks
10
by: MVChauhan | last post by:
Hi We are planning to move over to SQL Server 2005 in near future. At the moment Website is on a seperate server then the Database. OS for both the server is Window 2003 and currently our data...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.