I have a query that returns multiple identical records, however it should
only return one. Indeed there is only one record for the OrderActionType code
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.Contract Code,
L.OrderLegCode AS OrderLeg, M.MarketActionN ame AS
Action, Orders.OrderVol ume AS Volume, Orders.OrderPri ce AS Price,
T.OrderTypeName AS OrderType, S.StatusName AS Status,
Orders.FilledVo lume AS Filled, Orders.Original OrderDateTime,
Orders.PlaceOrd erDateTime,
Orders.MonitorF lag
FROM Orders INNER JOIN
OrderAction OA ON Orders.OrderID = OA.OrderID INNER
JOIN
Attempt A ON Orders.AttemptI D = A.AttemptID INNER JOIN
Event E ON A.EventID = E.EventID INNER JOIN
MarketAction M ON Orders.MarketAc tionCode =
M.MarketActionC ode INNER JOIN
OrderLegs L ON Orders.OrderLeg Code = L.OrderLegCode
INNER JOIN
OrderType T ON Orders.OrderTyp eCode = T.OrderTypeCode
INNER JOIN
OrderState S ON Orders.Status = S.Status AND
Orders.OrderID =
(SELECT OrderID
FROM ORDERACTION
WHERE ORDERACTIONID =
(SELECT
MAX(ORDERACTION ID)
FROM
ORDERACTION
WHERE
OrderActionType Code = 'P'))
Query 2
SELECT OrderID, TicketID
FROM Orders
WHERE (OrderID =
(SELECT OrderID
FROM ORDERACTION
WHERE ORDERACTIONID =
(SELECT
MAX(ORDERACTION ID)
FROM
ORDERACTION
WHERE
OrderActionType Code = 'P'))) 3 2347
[posted and mailed, please reply in news]
Fred (Fr**@hotmail.c om) writes: I have a query that returns multiple identical records, however it should only return one. Indeed there is only one record for the OrderActionType code 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****@sommarsk og.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
>> 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 OrderActionType code 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 "OrderActionTyp ecode" is almost identical to a
sarcastically bad example I use in a lecture. Please explain all the
logical differences among "OrderActionTyp ecode", "OrderActionTyp e",
"OrderActionCod e" and mere "OrderActio n"? Could we also have a column
called "OrderActionTyp eCodeValue" 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.
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
'OrderactionTyp eCode'. 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*******@eart hlink.net> wrote in message
news:18******** *************** ***@posting.goo gle.com... 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 OrderActionType code 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 "OrderActionTyp ecode" is almost identical to a sarcastically bad example I use in a lecture. Please explain all the logical differences among "OrderActionTyp ecode", "OrderActionTyp e", "OrderActionCod e" and mere "OrderActio n"? Could we also have a column called "OrderActionTyp eCodeValue" 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
|
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 or multiple sources. For those in
multiple sources, the data for a person may not be complete in any source. For
example, a person is in three sources; source#2 has the SSN and source#3 has the
membership#. For those people who are in multiple...
|
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 multiple slide records per CutID.
This table is a subdatashet of the Cut table--so the CutId is stored in it
I have a form based on a query between the 2 tables for adding slide records
|
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 a certain date (in this case
January 30th) the database will return 2 rows for an order as you can
see below.
Order ID: Line: Due Date: Qty: Ship Qty: Part #: Shipped:
141285 1 1/30/2006 31 10 S15F-55
|
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
relies on subscrption fees.
Sessions ID's are matched between the browser and the server. So a users can login with same
username and password and those sessions are tracked individually.
Some suggest create table fields with the session ID...
| |
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 the Record Source I originally had the statement
TABLE1,
and all worked fine. Now I want to open the second table as well, and
this is where my problem is.
|
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 variations(combination of fields),
- then act on each group in some way ...eg ProcessRs (oRs as RecordSet)...
the following query will get me the distinct groups
|
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 there an equivalent property for the
DataGridView? I have searched, but have not found one.
I would like the user to be able to see all the columns of the table on one
screen - thus eliminating the need to use the horizontal scroll bar to view...
|
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) and the 'employee
name'. There is another table which assigns an ID to the Shifts, i.e.
1,2 and 3 for morn, eve & night shifts respectively. From the mother
table, the incentive is calculated datewise for each employee as per
his shift duty. In...
|
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 each record. The checkbox is labeled "Move" (for moving multiple records at a time to another year). Of course, the checkbox is set as a control "moveRecord" on a table that the query is pulling from. So if the user checks one record, then clicks to...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |