• asaini (5/13/2013)


    Sorry about that, this is an idea of what I have:

    Sales Order #InvoiceIDInvoiceDate InvoiceAmount

    SO-100 INV-382224/12/2013 $7

    SO-100 INV-382224/12/2013 $4

    SO-100 INV-382224/12/2013 $4

    SO-100 INV-382224/12/2013 $7

    SO-101 INV-48254/14/2013 $10

    SO-101 INV-48254/14/2013 $8

    SO-101 INV-48274/15/2013 $3

    SO-102 INV-48304/20/2013 $8

    SO-102 INV-48304/20/2013 $8

    SO-102 INV-48304/20/2013 $8

    SO-102 INV-48314/21/2013 $63

    SO-102 INV-48324/22/2013 $26

    In this, I have some sales orders with only 1 invoice. On the other hand, there are other orders that have multiple invoice IDs, meaning they were shipped in different chunks. I am looking to develop a query that could tell me which sales orders have multiple invoice IDs and how many, along with the invoice date. Been running into some dead ends so far. Any help would be much appreciated.

    This is not really the create table and insert statements I asked for and pointed you to in the article. since you brand new around here I cobbled this together based on what you did post so you can see what I mean.

    if OBJECT_ID('tempdb..#Sales') is not null

    drop table #Sales

    create table #Sales

    (

    SalesOrder char(6),

    Invoice varchar(10),

    InvoiceDate datetime,

    InvoiceAmount int

    )

    insert #Sales

    select 'SO-100', 'INV-38222', '4/12/2013', 7 union all

    select 'SO-100', 'INV-38222', '4/12/2013', 4 union all

    select 'SO-100', 'INV-38222', '4/12/2013', 4 union all

    select 'SO-100', 'INV-38222', '4/12/2013', 7 union all

    select 'SO-101', 'INV-4825', '4/14/2013', 10 union all

    select 'SO-101', 'INV-4825', '4/14/2013', 8 union all

    select 'SO-101', 'INV-4827', '4/15/2013', 3 union all

    select 'SO-102', 'INV-4830', '4/20/2013', 8 union all

    select 'SO-102', 'INV-4830', '4/20/2013', 8 union all

    select 'SO-102', 'INV-4830', '4/20/2013', 8 union all

    select 'SO-102', 'INV-4831', '4/21/2013', 63 union all

    select 'SO-102', 'INV-4832', '4/22/2013', 26

    I know this doesn't seem like it is so hard but if you post the ddl and sample data there are two major advantages for you. First, we don't have to guess at column names and more importantly datatypes. The second advantage is that since everyone who posts here is a volunteer, if you post everything in a readily consumable format you are much more likely to get more people helping you.

    So now to pull this data like you stated, it is nothing more than a group by. Please note that some of the SalesOrders will show up multiple times because they were invoiced and shipped on different days. And since you want the invoice date in the output you either show the values or you have to aggregate them (MIN, MAX, etc)

    select SalesOrder, InvoiceDate, count(Invoice) as NumShipments

    from #Sales

    group by SalesOrder, InvoiceDate

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/