Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Trouble getting a distinct count in SQL command Post Reply Post New Topic
Author Message
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Topic: Trouble getting a distinct count in SQL command
     Posted: 18 Nov 2010 at 9:21am
I have this query:

select
 sop10100.soptype,
 sop10100.sopnumbe,
 sop10100.orignumb,
 sop10100. reqshipdate,
 sop10100.custnmbr,
 sop10100.custname,
 sop10100.cstponbr,
 sop10100.city,
 sop10100.state,
 sop10100.miscamnt,
 sop10100.docamnt,
 sop10100.creatddt,
 sop10200.sopnumbe,
 sop10200.itemnmbr,
 sop10200.itemdesc,
 sop10200.unitprce,
 sop10200.xtndprce,
 sop10200.quantity,
 iv00103.itemnmbr,
 iv00103.vendorid,
 pm00200.vendorid,
 pm00200.vendname,
 pm00200.city,
 pm00200.state,
 iv00101.itmshnam
from
 sop10100
left outer join
 sop10200
on
 sop10100.sopnumbe=sop10200.sopnumbe
inner join
 iv00103
on
 sop10200.itemnmbr=iv00103.itemnmbr
inner join
 pm00200
on
 iv00103.vendorid=pm00200.vendorid
inner join
 iv00101
on
 sop10200.itemnmbr=iv00101.itemnmbr
where
 sop10100.soptype=3
and
 sop10100.voidstts=0
and
 (
  sop10100.custnmbr like 'swa%' or
  sop10100.custnmbr like 'swc%' or
  sop10100.custnmbr like 'dur%' or
  sop10100.custnmbr like 'swn%'
 
 )


The field Sopnumbe is an invoice number.  My report is sorted by Customer Number (custnmbr) and then by invoice number.  I want to add further layer of grouping outside customer number that sorts the data based on if the customer has more than one invoice.

In CR I can very easily get this value with a formula or running total, however I cannot group based on this (because the data is not evaluated yet).  My assumption is that I need to add a new field in the SQL command that does a distinct count, but I can't get it to work.  Shouldn't it be something like:

(SELECT COUNT(DISTINCT sopnumbe) as "invoicecount"
FROM sop10100
group by custnmbr)

Is it the issue that I am trying to add a field for a table from which I need to get other fields from?

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 19 Nov 2010 at 4:38am
to do it all in one select (without temp tables) you would probably want to add another join something like:
JOIN (SELECT COUNT(DISTINCT sopnumbe) as "invoicecount", custnmbr FROM sop10100
group by custnmbr) AS ss
 ON ss.custnmbr = sop10100. custnmbr
 
 
HTH
IP IP Logged
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Posted: 30 Nov 2010 at 3:54am
I put it in the query, but the field "invoicecount" is not available, any idea why?

select
 sop10100.soptype,
 sop10100.sopnumbe,
 sop10100.orignumb,
 sop10100. reqshipdate,
 sop10100.custnmbr,
 sop10100.custname,
 sop10100.cstponbr,
 sop10100.city,
 sop10100.state,
 sop10100.miscamnt,
 sop10100.docamnt,
 sop10100.creatddt,
 sop10200.sopnumbe,
 sop10200.itemnmbr,
 sop10200.itemdesc,
 sop10200.unitprce,
 sop10200.xtndprce,
 sop10200.quantity,
 iv00103.itemnmbr,
 iv00103.vendorid,
 pm00200.vendorid,
 pm00200.vendname,
 pm00200.city,
 pm00200.state,
 iv00101.itmshnam
from
 sop10100

JOIN (SELECT COUNT(DISTINCT sopnumbe) as "invoicecount", custnmbr FROM sop10100
group by custnmbr) AS ss
 ON ss.custnmbr = sop10100. custnmbr

left outer join
 sop10200
on
 sop10100.sopnumbe=sop10200.sopnumbe
inner join
 iv00103
on
 sop10200.itemnmbr=iv00103.itemnmbr
inner join
 pm00200
on
 iv00103.vendorid=pm00200.vendorid
inner join
 iv00101
on
 sop10200.itemnmbr=iv00101.itemnmbr
where
 sop10100.soptype=3
and
 sop10100.voidstts=0
and
 (
  sop10100.custnmbr like 'swa%' or
  sop10100.custnmbr like 'swc%' or
  sop10100.custnmbr like 'dur%' or
  sop10100.custnmbr like 'swn%'
  )
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 30 Nov 2010 at 11:54am
2 things...
1) what was I thinking, you don't put quotes around an alias...silly me
  should be just: SELECT COUNT(DISTINCT sopnumbe) as invoicecount,
 
2) you need to select the value as well.  Add ss.invoicecount to the list of fields to be selected.
 
HTH
IP IP Logged
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Posted: 02 Dec 2010 at 9:34am
That worked perfectly, thank you so much!
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum