Re: SQL join count field by Erlandas
Erlandas
Fri Jan 30 06:47:53 CST 2004
MS SQL 2000, VFP 8
This example working within VFP, but incorrect syntax with MSSQL remote view
SUM(IIF(NVL(ChildTbl.Checked, .T.), 0, 1)) AS nCheckCount.
Same error with SUM(CASE ch.Checked WHEN 0 THEN 0 ELSE 1) AS nCheckCount.
something wrong here.
do you have another suggestion?
"Cindy Winegarden" <cindy.winegarden@mvps.org> wrote in message
news:%23dqeVQo5DHA.2696@TK2MSFTNGP09.phx.gbl...
> In news: bvan4j$17j$07$1@news.t-online.com,
> Christian Ehlscheid <ehlscheid-no-spam@edv-ermtraud.de> wrote:
> > SELECT .....SUM(CASE ch.Checked WHEN 0 THEN 0
> > ELSE 1) AS nCheckCount ....
> >
> > there one thing to mention ..
> > if the number of records in the child table is 0 the checked count
> > will not be 0 but .NULL. since no sum can be build
>
>
> USE the FoxPro NVL() or the SQL Server ISNULL(). This assumes that when a
> child record is not present then you don't count the missing record as
.F.:
>
> CREATE CURSOR ParentTbl(Pk I)
> INSERT INTO ParentTbl VALUES (1)
> INSERT INTO ParentTbl VALUES (2)
> CREATE CURSOR ChildTbl(Fk I, Checked L)
> INSERT INTO ChildTbl VALUES(1, .F.)
> INSERT INTO ChildTbl VALUES(1, .T.)
>
> SELECT ;
> ParentTbl.Pk, ;
> SUM(IIF(NVL(ChildTbl.Checked, .T.), 0, 1)) AS nCheckCount ;
> FROM ParentTbl ;
> LEFT JOIN ChildTbl ;
> ON ParentTbl.Pk = ChildTbl.Fk ;
> GROUP BY ParentTbl.Pk
>
> --
> Cindy Winegarden MCSD, Microsoft Visual FoxPro MVP
> cindy.winegarden@mvps.org www.cindywinegarden.com
>
>
>