473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with SQL statement!

I am having difficulty writing a SQL statement and I was wondering if one of
you gurus could help me. I have a table that has three fields: InvoiceID,
Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am
trying to write a SQL statement that returns the record (all three fields)
for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs.
How would I write this SQL statement?

Thank you,

Alex
Jul 19 '05 #1
5 2992
D
I would like to humbly state that, if I understand you correctly that you
want the cheapest item of every order, this is no simple matter in a Totals
Query. I had a similar need once where I wanted the cheapest (Min) of a
product price but I also wanted all the details of that price (Order #,
Date, Vendor, etc...). The problem you will have is with the Item field, by
introducing this third field you will wind up getting all the items. I do
not claim to be an expert but this is the problem that I had. I therefore
decided to use a PivotChart instead and found it to be very nice and neat.
I hope this helps and maybe there is someone out there that knows a better
way.

Dave

"Nic" <nicpayre[junk]@sympatico.ca> wrote in message
news:qo*******************@news20.bellglobal.com.. .

"Alex" <sh****@yahoo.com> wrote in message
news:_5*********************@twister.austin.rr.com ...
I am having difficulty writing a SQL statement and I was wondering if one
of
you gurus could help me. I have a table that has three fields:

InvoiceID, Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am trying to write a SQL statement that returns the record (all three fields) for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs. How would I write this SQL statement?

What you need is a group function...i let you guess wich one it is.


Thank you,

Alex


Jul 19 '05 #2
"Alex" <sh****@yahoo.com> wrote in message news:_5*********************@twister.austin.rr.com ...
I am having difficulty writing a SQL statement and I was wondering if one of
you gurus could help me. I have a table that has three fields: InvoiceID,
Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am
trying to write a SQL statement that returns the record (all three fields)
for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs.
How would I write this SQL statement?

Thank you,

Alex


CREATE TABLE Invoices
(
invoice_id INT NOT NULL,
item INT NOT NULL,
item_price DECIMAL(6, 2) NOT NULL,
PRIMARY KEY (invoice_id, item)
)

-- Sample data
INSERT INTO Invoices (invoice_id, item, item_price)
VALUES (1, 1, 10)
INSERT INTO Invoices (invoice_id, item, item_price)
VALUES (1, 2, 15.50)
INSERT INTO Invoices (invoice_id, item, item_price)
VALUES (1, 3, 9.99)
INSERT INTO Invoices (invoice_id, item, item_price)
VALUES (2, 1, 99.99)
INSERT INTO Invoices (invoice_id, item, item_price)
VALUES (2, 2, 149.99)

SELECT I1.invoice_id, I1.item, I1.item_price
FROM Invoices AS I1
LEFT OUTER JOIN
Invoices AS I2
ON I1.invoice_id = I2.invoice_id AND
I2.item_price < I1.item_price
WHERE I2.item_price IS NULL
ORDER BY I1.invoice_id

invoice_id item item_price
1 3 9.99
2 1 99.99

Regards,
jag
Jul 19 '05 #3
"Alex" <sh****@yahoo.com> wrote in message news:<_5*********************@twister.austin.rr.co m>...
I am having difficulty writing a SQL statement and I was wondering if one of
you gurus could help me. I have a table that has three fields: InvoiceID,
Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am
trying to write a SQL statement that returns the record (all three fields)
for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs.
How would I write this SQL statement?

Thank you,

Alex

Hi,
I think you should try with Group by clauseand MIN function. it might help you.

Thanks
Hoque
Jul 19 '05 #4
"Alex" <sh****@yahoo.com> wrote in message news:<_5*********************@twister.austin.rr.co m>...
I am having difficulty writing a SQL statement and I was wondering if one of
you gurus could help me. I have a table that has three fields: InvoiceID,
Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am
trying to write a SQL statement that returns the record (all three fields)
for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs.
How would I write this SQL statement?

Thank you,

Alex


It's not clear what the best way to do this is, without knowing what
your table structure is, what your keys are etc. For example, what
happens if one invoice has two items with the same 'minimum' price -
which item should appear on the output?

If one invoice has many items, you would usually normalize your design
to have an Invoices table and an InvoiceItems table, with a foreign
key linking them. You'd probably also need to link to some sort of
Items table which has all the items that could appear on an invoice.
This is a lot more efficient - invoice tables are a common task, so
you can probably find some information on the web about implementing
them.

Having said all that, here's one query that _might_ work, but it won't
be consistent and may be inefficient, depending on what indexes you
have on the table. The real solution is almost certainly to redesign
your tables, but without more background information it's not clear.

select
it.InvoiceID,
it.Item,
it.ItemPrice
from
InvoiceTable it
join ( select InvoiceID, min(ItemPrice) as ItemPrice
from InvoiceTable
group by InvoiceID ) dt
on it.InvoiceID = dt.InvoiceID
and it.ItemPrice = dt.ItemPrice
order by
it.InvoiceID

Simon
Jul 19 '05 #5
Thank you all for your help, particularly Mike MacSween's.

"Alex" <sh****@yahoo.com> wrote in message
news:_5*********************@twister.austin.rr.com ...
I am having difficulty writing a SQL statement and I was wondering if one of you gurus could help me. I have a table that has three fields: InvoiceID,
Item, ItemPrice. Each InvoiceID is associated with one or more Items. I am
trying to write a SQL statement that returns the record (all three fields)
for the least expensive item for each invoice (so the total number of
records returned will be equal to the total number of unique Invoice IDs.
How would I write this SQL statement?

Thank you,

Alex

Jul 19 '05 #6

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

Similar topics

0
521
by: Gary Stollman | last post by:
My Father is a Clone//ETs plotting to overthrow the human race! On August 19, 1987 a gun-carrying Gary Stollman entered the studio of Los Angeles's KNBC television, crashing consumer reporter...
7
1952
by: Alex | last post by:
I am having difficulty writing a SQL statement and I was wondering if one of you gurus could help me. I have a table that has three fields: InvoiceID, Item, ItemPrice. Each InvoiceID is associated...
1
2120
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
1
3165
by: JP | last post by:
I just installed db2 V8.1 and I am trying to create a simple compound statement in the command center script area and every time I would get the following error. what am i doing wrong? (note: my...
7
2153
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
2
4707
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
14
1564
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
6
2964
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
2
8061
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; ...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
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
7171
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,...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4918
isladogs
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...
0
4599
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
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
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...

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.