472,126 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 20 '05 #1
7 1889
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 20 '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?


SELECT tblInvoiceA.InvoiceID, tblInvoiceA.Item, tblInvoiceA.ItemPrice
FROM tblInvoice AS tblInvoiceA
WHERE (((tblInvoiceA.ItemPrice)
IN
(SELECT MIN(ItemPrice)
FROM tblInvoice
WHERE InvoiceID = tblInvoiceA.InvoiceID)));

or an = would do just as well as the IN.

Problem - what if you have 2 items on the same invoice with the same price,
which is also the lowest price on that invoice? Which do you want to choose?
You could try this:

SELECT tblInvoiceA.InvoiceID, First(tblInvoiceA.Item) AS FirstOfItem,
tblInvoiceA.ItemPrice
FROM tblInvoice AS tblInvoiceA
GROUP BY tblInvoiceA.InvoiceID, tblInvoiceA.ItemPrice
HAVING (((tblInvoiceA.ItemPrice)
IN
(SELECT MIN(ItemPrice)
FROM tblInvoice
WHERE InvoiceID = tblInvoiceA.InvoiceID)));

Which will give you one 'lowest price' item.

But your table design is wrong. You want one table tblInvoices, in a one to
many relationship with tblInvoiceItems. Or something. The way you've got it
at the moment I can't see what your Primary Key is. InvoiceID + Item? What
if they buy 2 of them on the same invoice?

Yours, Mike MacSween

Jul 20 '05 #3
"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 20 '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

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

Thanks
Hoque
Jul 20 '05 #5
"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 20 '05 #6
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 20 '05 #7
Mike,

Thank you so much for your reply. Your solution is exactly what I was
looking for. You definitely know your SQL.

Alex

"Mike MacSween" <mi***********************@btinternet.com> wrote in message
news:3f***********************@pubnews.gradwell.ne t...
"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?


SELECT tblInvoiceA.InvoiceID, tblInvoiceA.Item, tblInvoiceA.ItemPrice
FROM tblInvoice AS tblInvoiceA
WHERE (((tblInvoiceA.ItemPrice)
IN
(SELECT MIN(ItemPrice)
FROM tblInvoice
WHERE InvoiceID = tblInvoiceA.InvoiceID)));

or an = would do just as well as the IN.

Problem - what if you have 2 items on the same invoice with the same

price, which is also the lowest price on that invoice? Which do you want to choose? You could try this:

SELECT tblInvoiceA.InvoiceID, First(tblInvoiceA.Item) AS FirstOfItem,
tblInvoiceA.ItemPrice
FROM tblInvoice AS tblInvoiceA
GROUP BY tblInvoiceA.InvoiceID, tblInvoiceA.ItemPrice
HAVING (((tblInvoiceA.ItemPrice)
IN
(SELECT MIN(ItemPrice)
FROM tblInvoice
WHERE InvoiceID = tblInvoiceA.InvoiceID)));

Which will give you one 'lowest price' item.

But your table design is wrong. You want one table tblInvoices, in a one to many relationship with tblInvoiceItems. Or something. The way you've got it at the moment I can't see what your Primary Key is. InvoiceID + Item? What
if they buy 2 of them on the same invoice?

Yours, Mike MacSween

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gary Stollman | last post: by
5 posts views Thread by Alex | last post: by
1 post views Thread by Tom Rahav | last post: by
7 posts views Thread by Buck Rogers | last post: by
6 posts views Thread by zaina | last post: by
reply views Thread by leo001 | last post: by

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.