By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

Need help with SQL statement!

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.