473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

multiple records, only want one

I have a query that returns multiple identical records, however it should
only return one. Indeed there is only one record for the OrderActionTypecode
of 'P' yet there are two orderactions so it returnd both.

Oddly the second query returns only the desired single record but wihtout
all the additional fields I need. Does the Inner join somehow mess with the
query?

Thanks

Query 1
SELECT Orders.OrderID, Orders.TicketID, A.AttemptID, E.EventID,
E.AssetCode AS Asset, Orders.FK_Prod_Alias AS Alias, Orders.ContractCode,
L.OrderLegCode AS OrderLeg, M.MarketActionName AS
Action, Orders.OrderVolume AS Volume, Orders.OrderPrice AS Price,
T.OrderTypeName AS OrderType, S.StatusName AS Status,
Orders.FilledVolume AS Filled, Orders.OriginalOrderDateTime,
Orders.PlaceOrderDateTime,
Orders.MonitorFlag
FROM Orders INNER JOIN
OrderAction OA ON Orders.OrderID = OA.OrderID INNER
JOIN
Attempt A ON Orders.AttemptID = A.AttemptID INNER JOIN
Event E ON A.EventID = E.EventID INNER JOIN
MarketAction M ON Orders.MarketActionCode =
M.MarketActionCode INNER JOIN
OrderLegs L ON Orders.OrderLegCode = L.OrderLegCode
INNER JOIN
OrderType T ON Orders.OrderTypeCode = T.OrderTypeCode
INNER JOIN
OrderState S ON Orders.Status = S.Status AND
Orders.OrderID =
(SELECT OrderID
FROM ORDERACTION
WHERE ORDERACTIONID =
(SELECT
MAX(ORDERACTIONID)
FROM
ORDERACTION
WHERE
OrderActionTypeCode = 'P'))
Query 2

SELECT OrderID, TicketID
FROM Orders
WHERE (OrderID =
(SELECT OrderID
FROM ORDERACTION
WHERE ORDERACTIONID =
(SELECT
MAX(ORDERACTIONID)
FROM
ORDERACTION
WHERE
OrderActionTypeCode = 'P')))

Jul 20 '05 #1
3 2325
[posted and mailed, please reply in news]

Fred (Fr**@hotmail.com) writes:
I have a query that returns multiple identical records, however it
should only return one. Indeed there is only one record for the
OrderActionTypecode of 'P' yet there are two orderactions so it returnd
both.

Oddly the second query returns only the desired single record but
wihtout all the additional fields I need. Does the Inner join somehow
mess with the query?


Without information about your tables, it is difficult to give a
precise answer. A lazy solution is of course to add a DISTINCT, and
take the cost of the extra sorting pass required.

However, looking at your query, I am not really sure that I understand
the purpose of this JOIN:

JOIN OrderAction OA ON Orders.OrderID = OA.OrderID

What happens if you take it out?

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
>> I have a query that returns multiple identical records [sic],
however it should only return one. Indeed there is only one record
[sic] for the OrderActionTypecode of 'P' yet there are two order
actions so it returned both.

Oddly the second query returns only the desired single record [sic]
but without all the additional fields [sic] I need. Does the INNER
JOIN somehow mess with the query? <<

The kludge is to use SELECT DISTINCT. The right answer is that your
DDL and schema design are probably all screwed. First of all, please
post DDL, so that people do NOT have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. What you are asking is impossible to answer right
now.

What you did post is awful. Let's get back to the basics of an RDBMS.
Rows are NOT records; fields are NOT columns; tables are NOT files.
You also need to read ISO-11179 and any book on data modeling.

The data element name "OrderActionTypecode" is almost identical to a
sarcastically bad example I use in a lecture. Please explain all the
logical differences among "OrderActionTypecode", "OrderActionType",
"OrderActionCode" and mere "OrderAction"? Could we also have a column
called "OrderActionTypeCodeValue" while we are at it? This much
improper attribute naming borders on parody; it just needs the table
name, datatype and usage (PK, FK) affixed to it to be a completely
wrong example.

I also see that you like to change the data element names in the
queries, so that the data dictionary is of little or no use to the
people maintaining the application or to the end users. It is a bad
attempt to make up for improper data element names, but it just makes
the code worse.

I also find after all these decades, that using the infixed join
syntax when you have a lot of tables actually hurts readability. You
might consider puting it all back into FROM.. WHERE.. syntax so you
can find the predicates easier.
Jul 20 '05 #3
Thank you for that most helpful reply.

Firstly, I will take your suggestion for posting additional information as
the appropriate thing to do in the future.

As for the verbose nature of the data element names. I did not design these
names, queries etc and have no experience with sql..... hence my visit to
this newsgroup.

I am merely trying to create a query that would let me view some information
over the web. In all honesty I could not care a less about the length of
'OrderactionTypeCode'. Yes it could have been merely defined as Type, but so
what... is it the end of the world as a result? (and yes, I am the one that
will need to maintain this database in the future)

Why am I not surprised that you are a university lecturer. I am sure that
you are a valuable contributor to this newsgroup so I will leave it at that.


"--CELKO--" <jc*******@earthlink.net> wrote in message
news:18**************************@posting.google.c om...
I have a query that returns multiple identical records [sic],

however it should only return one. Indeed there is only one record
[sic] for the OrderActionTypecode of 'P' yet there are two order
actions so it returned both.

Oddly the second query returns only the desired single record [sic]
but without all the additional fields [sic] I need. Does the INNER
JOIN somehow mess with the query? <<

The kludge is to use SELECT DISTINCT. The right answer is that your
DDL and schema design are probably all screwed. First of all, please
post DDL, so that people do NOT have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. What you are asking is impossible to answer right
now.

What you did post is awful. Let's get back to the basics of an RDBMS.
Rows are NOT records; fields are NOT columns; tables are NOT files.
You also need to read ISO-11179 and any book on data modeling.

The data element name "OrderActionTypecode" is almost identical to a
sarcastically bad example I use in a lecture. Please explain all the
logical differences among "OrderActionTypecode", "OrderActionType",
"OrderActionCode" and mere "OrderAction"? Could we also have a column
called "OrderActionTypeCodeValue" while we are at it? This much
improper attribute naming borders on parody; it just needs the table
name, datatype and usage (PK, FK) affixed to it to be a completely
wrong example.

I also see that you like to change the data element names in the
queries, so that the data dictionary is of little or no use to the
people maintaining the application or to the end users. It is a bad
attempt to make up for improper data element names, but it just makes
the code worse.

I also find after all these decades, that using the infixed join
syntax when you have a lot of tables actually hurts readability. You
might consider puting it all back into FROM.. WHERE.. syntax so you
can find the predicates easier.

Jul 20 '05 #4

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

Similar topics

7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
3
by: Steve | last post by:
I have a people table of about 25 fields. The table is initially created each year from 5 different sources. The records from each source are appended to the people table. Any person may be in one...
0
by: allyn44 | last post by:
HI--I have 2 tables Cut: cut ID, HistNumb, Block, date: Cut Id is the primary key, the other 3 fileds are indexed to be unique Slides: Cutid SlideID, and various other fields: there can be...
5
by: jhutchings | last post by:
Hello everyone, I have a database where I collect shipment data from various tables. However, I have a problem. Whenever I want to see shipping data for orders that were set to ship on or before...
18
by: Gleep | last post by:
I've searched google intensely on this topic and it seems noone really knows how to approch this. The goal I don't want clients to give out their usernames and passwords to friends, since the site...
6
by: ApexData | last post by:
I have 2 tables: Table1 and Table2. Neither one has a primary key because each table will only have 1-record. My form is a SingleForm unbound with tabs (my desire here). Using this form, in...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
8
by: jmarcrum | last post by:
Hello all, i have a continuous form that displays about 1000 records and opens when I click a button. When the form opens, the user has the option of checking a checkbox that is located beside...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.