Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Cartesius
Starting Member
4 Posts |
Posted - 2008-02-12 : 05:04:51
|
| Hi:In a regular table structure like this (--< is for 1:M):customer --< invoice --< invoiceitem >-- productI want to get a list of each customers ordered products, but only the latest order of product. Not the latest order/invoice date per se, but the latest order/invoice for each particular invoiceitem i.e product, with details so that I can get that IncoiceItem record. Ineffect the invoiceitem_PK .like this:Customer Product DateLastPurchased InvoiceItem_pk InvoiceItem_quantitycustA prodX 2006-12-02 1000 000 001 10 custA prodY 2006-11-24 1000 000 102 12custB prodX 2006-10-29 1000 000 203 100custB prodZ 2007-08-13 1000 000 302 3etc.I can easily get a list of the three first columns by using a GROUP BY clause - and in my data the first three columns actually completely defines an invoiceItem row - so I have tried to use this output but when I try to join this table to a query I never get it right.Any help would be much appreciated! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-12 : 05:08:38
|
| You can form a dervived table out of this query and use it to join to other queries.like.SELECT * FROM(your current group by query) djoin other queries q1on d.field=q1.field.....if you want exact solution, please post your other queries and also sample data from tables. |
 |
|
|
Cartesius
Starting Member
4 Posts |
Posted - 2008-02-12 : 08:02:50
|
| Thanks for the swift reply. I have tried that in several combinations, but not gotten it to work, unfortunately. I am doing something wrong somewere. The only thing that gets the right rows, is the below, but then I get no details, of course. Here is the detailed info you requested:The grouped query is:select cu.customer_pk, ii.product_fk, max(i.invoicedatetime) from customer cu, invoice i, invoiceitem ii, product p where cu.customer_pk = i.customer_fk AND i.invoice_pk = ii.invoice_fk and ii.product_fk = p.product_pk group by cu.customer_pk, ii.product_fk;Tables are:create table "DBADMIN"."customer"( "Customer_pk" int, "ContactPerson" varchar(100), "OrgName" varchar(50) not null collate CASE_INSENSITIVE, "ModDateTime" timestamp not null, _C0000001241 primary key ("Customer_pk"),);create table "DBADMIN"."invoice"( "Invoice_pk" int, "Customer_fk" int not null, "InvoiceDateTime" timestamp, "InvoiceNumber" int, "OurContact" varchar(100), "ModDateTime" timestamp not null, _C0000001284 primary key ("Invoice_pk"), _C0000001285 foreign key ("Customer_fk") references "customer" ("Customer_pk") deferrable,);create table "DBADMIN"."invoiceitem"( "Invoiceitem_pk" int, "invoice_fk" int not null, "product_fk" int not null, "NumberOfItems" int, "RebateOnPrice" int, "ModDateTime" timestamp not null, _C0000001302 primary key ("Invoiceitem_pk"), _C0000001303 foreign key ("invoice_fk") references "invoice" ("Invoice_pk") deferrable,);create table "DBADMIN"."product"( "Product_pk" int, "ProductName" varchar(100), "ProductGroup" varchar(100), "ProductPrice" int, "ModDateTime" timestamp not null, _C0000001269 primary key ("Product_pk"),);Sample data:CustomerCustomer_pk contactperson orgname moddatetime1000001 John Doe Widgets, Inc 10 feb 2008 22.261000002 John Smith ACME, inc. 11 feb 2008 02.41Productproduct_pk productnamen productgroup productprice moddatetime1000003 Pencils Office Supplies 10 11 feb 2008 01.481000005 Paper, Copier, A4, White, 500 pcs Office Supplies 50 11 feb 2008 01.491000006 Computer Mouse Computer Equiptment 256 11 feb 2008 01.50Invoiceinvoice_pk customer_fk invoicedatetime invoicenumber ourcontact moddatetime1000001 1000001 2 feb 2008 00.00 1 OC 11 feb 2008 01.131000003 1000001 6 feb 2008 00.00 2 OC 11 feb 2008 01.361000004 1000001 7 feb 2008 00.00 3 FV 11 feb 2008 02.381000005 1000002 10 feb 2008 00.00 5 gg 11 feb 2008 15.391000006 1000001 11 feb 2008 00.00 6 oc 11 feb 2008 15.411000007 1000001 11 feb 2008 00.00 7 OC 11 feb 2008 15.43InvoiceIteminvoiceitem_pk invoice_fk product_fk numberofitems rebateonprice moddatetime1000007 1000001 1000001 10 0 11 feb 2008 01.351000008 1000001 1000001 12 0 11 feb 2008 01.351000010 1000001 1000001 12 0 11 feb 2008 01.131000012 1000001 1000003 12 0 11 feb 2008 02.371000013 1000003 1000005 3 0 11 feb 2008 02.381000014 1000004 1000006 6 0 11 feb 2008 02.391000015 1000005 1000003 100 20 11 feb 2008 15.391000016 1000006 1000003 150 30 11 feb 2008 15.401000017 1000007 1000006 2 0 11 feb 2008 15.43TIA! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-12 : 09:31:43
|
Try like this. remember to put only reqd fields in place of * SELECT *FROM customer cINNER JOIN invoice iON i.Customer_fk=c.Customer_pkINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY invoice_fk,product_fk ORDER BY moddatetime DESC)AS RowNo,invoiceitem_pk,invoice_fk,product_fk,numberofitems,rebateonprice,moddatetimeFROM invoiceitem)itON it.invoice_fk=i.invoice_pkAND it.RowNo=1INNER JOIN product pON it.product_fk=p.product_pk |
 |
|
|
Cartesius
Starting Member
4 Posts |
Posted - 2008-02-12 : 10:20:34
|
| Hello, visakh:I am very new to SQL Server, so my understanding of the T-SQL is limited, and I will also need to use other SQL databases, so is there a way to express this in more regular (SQL92) fashion, without the T-SQL specific commands (row-number(), partition by)?Also: My main objective here is to refer to the InvoiceDateTime as the indicator of last order made of a product. This complicates things since there is no direct relation between product and InvoiceDateTime.Best regards |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-12 : 11:08:55
|
Sorry i'm not much familiar with SQL92 syntax. I started my career with T-SQL and never had a chance to move from it.This is all what i could alter:-SELECT c.ContactPerson AS Customer, p.ProductGroup AS product,it.InvoiceDateTime AS DateLastPurchased,it.InvoiceItem_pk,it.numberofitems AS InvoiceItem_quantityFROM customer cINNER JOIN invoice iON i.Customer_fk=c.Customer_pkINNER JOIN invoiceitem itON it.invoice_fk=i.invoice_pkINNER JOIN (SELECT MAX(invoiceitem_pk) AS maxinvoice,invoice_fk,product_fk FROM invoiceitem GROUP BY invoice_fk product_fk)MaxProdON MaxProd.maxinvoice=it.invoiceitem_pk INNER JOIN product pON it.product_fk=p.product_pk |
 |
|
|
Cartesius
Starting Member
4 Posts |
Posted - 2008-02-12 : 11:19:59
|
| Thanks, I will try this!Best regards and thanks a million. |
 |
|
|
|
|
|
|
|