Question

I have an excel sheet that contains invoices of orders Each invoice has the following data

ID = a unique id for each ID

CUSTOMER = The name of the ID

AMT = The total value of the invoices

DATE = The date of the purchase

For the following data

CID     CUSTOMER    AMT     DATE
1       James           100     1/1/2012
2       Mark            110     1/1/2012
3       John            110     2/1/2012
1       James           200     2/1/2012
3       John            140     2/1/2012
2       Mark            120     3/1/2012

I need to select records from excel sheet so that i have this output

CID     Customer    INVCOUNT    TotalValue
1       James       2       300     
1       John        2       250     
1       Mark        2       230 

This is the sql i tried

Select
  i.[CID],
  i.[CUSTOMER],
  Count(i.[CID]) as INVCOUNT,
  sum(i.AMT) as TotalValue
From
  [Invoices] i
Where
  i.[DATE] >= #2/1/2012# And
  i.[DATE] <= #3/1/2012#
Group By
  i.[CID], i.[CUSTOMER]
Having
  Count(i.[CID]) > 1

pls this is an excel query, not mysql. The tag i used earlier was a mistake.

WHat am i doing wrong?

Was it helpful?

Solution

To get the output you specified:

Select
  1 as CID,
  i.[CUSTOMER],
  Count(i.[CID]) as INVCOUNT,
  sum(i.AMT) as TotalValue
From
  [Invoices] i
Where
  i.[DATE] >= #1/1/2012# And
  i.[DATE] <= #3/1/2012#
Group By
  i.[CID], i.[CUSTOMER]
Having
  Count(i.[CID]) > 1

To get the output I think you actually want:

Select
  i.[CID],
  i.[CUSTOMER],
  Count(i.[CID]) as INVCOUNT,
  sum(i.AMT) as TotalValue
From
  [Invoices] i
Where
  i.[DATE] >= #1/1/2012# And
  i.[DATE] <= #3/1/2012#
Group By
  i.[CID], i.[CUSTOMER]
Having
  Count(i.[CID]) > 1

This should generate

CID     Customer    INVCOUNT    TotalValue
1       James       2       300     
2       John        2       250     
3       Mark        2       230 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top