I'm fairly new to SQL, so I'm not sure if my problem is something fairly simple or it's just not possible to do with SQL.
I have a query that gives me the information I want, but I need to limit the results based on an aggregate formula (I think that's what I want to do)
This is my present query:
SELECT
detail.TCONO as COMPANY,
detail.TJBNO as JOB_NUM,
detail.TCSTY as COST_TYPE,
Sum(detail.TAMAT) AS AMOUNT,
Sum(detail.TRGHR) AS REG_HOURS,
Sum(detail.TOVHR) AS OVT_HOURS,
Sum(detail.TOTHR) AS OTH_HOURS,
summary.DESCR AS DESCRIPTION
FROM detail INNER JOIN summary ON (detail.TJBNO = summary.GJBNO) AND (detail.TDVNO = summary.GDVNO) AND (detail.TCONO = summary.GCONO)
WHERE EXISTS (SELECT * FROM summary WHERE summary.GDTLU >= 20050101 AND detail.TJBNO = summary.GJBNO AND summary.GCONO = 1 AND summary.GDVNO = 0)
AND (detail.TCONO = 1 AND detail.TDVNO = 0)
GROUP BY detail.TCONO, detail.TJBNO, detail.TCSTY, summary.DESCR
Like I said, the present SQL works fine, but I need tweak it a little more.
I'm trying to limit my report to pull data ONLY for jobs that have a loss.
The Amount field is always a positive number.
The Cost Type determines whether the Amount is Income (CostType = "I") or Expense (CostType <> "I").
I know what I want to do, but I don't know how to write it in SQL.
Simply put, in English, I want to exclude jobs where:
(sum(Amount) where Cost Type = "I") < (sum(Amount) where Cost Type <> "I")
I don't know if this can even be translated into SQL or I'm trying to do something impossible.
This is what I came up with:
SELECT
detail.TCONO as COMPANY,
detail.TJBNO as JOB_NUM,
detail.TCSTY as COST_TYPE,
Sum(detail.TAMAT) AS AMOUNT,
Sum(detail.TRGHR) AS REG_HOURS,
Sum(detail.TOVHR) AS OVT_HOURS,
Sum(detail.TOTHR) AS OTH_HOURS,
summary.DESCR AS DESCRIPTION
FROM detail INNER JOIN summary ON (detail.TJBNO = summary.GJBNO) AND (detail.TDVNO = summary.GDVNO) AND (detail.TCONO = summary.GCONO)
WHERE EXISTS (SELECT * FROM summary WHERE summary.GDTLU >= 20050101 AND detail.TJBNO = summary.GJBNO AND summary.GCONO = 1 AND summary.GDVNO = 0 AND (Sum(detail.TAMAT) WHERE detail.TCSTY = 'I') < (Sum(detail.TAMAT) WHERE detail.TCSTY <> 'I'))
AND (detail.TCONO = 1 AND detail.TDVNO = 0)
GROUP BY detail.TCONO, detail.TJBNO, detail.TCSTY, summary.DESCR
When I parse the query, I get the following error message:
Connection or SQL sentence error: (DA00005)
[IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0199 - Keyword WHERE not expected. Valid tokens: < > = <> <= !< !> != >= ¬< ¬> ¬= IN IS NOT LIKE BETWEEN.-199
Like I said, I'm new to SQL and don't have a clue if this is possible with SQL or not. Apparently it definitely won't work the way I tried to do it.
Is there a way to do it?
I have a query that gives me the information I want, but I need to limit the results based on an aggregate formula (I think that's what I want to do)
This is my present query:
SELECT
detail.TCONO as COMPANY,
detail.TJBNO as JOB_NUM,
detail.TCSTY as COST_TYPE,
Sum(detail.TAMAT) AS AMOUNT,
Sum(detail.TRGHR) AS REG_HOURS,
Sum(detail.TOVHR) AS OVT_HOURS,
Sum(detail.TOTHR) AS OTH_HOURS,
summary.DESCR AS DESCRIPTION
FROM detail INNER JOIN summary ON (detail.TJBNO = summary.GJBNO) AND (detail.TDVNO = summary.GDVNO) AND (detail.TCONO = summary.GCONO)
WHERE EXISTS (SELECT * FROM summary WHERE summary.GDTLU >= 20050101 AND detail.TJBNO = summary.GJBNO AND summary.GCONO = 1 AND summary.GDVNO = 0)
AND (detail.TCONO = 1 AND detail.TDVNO = 0)
GROUP BY detail.TCONO, detail.TJBNO, detail.TCSTY, summary.DESCR
Like I said, the present SQL works fine, but I need tweak it a little more.
I'm trying to limit my report to pull data ONLY for jobs that have a loss.
The Amount field is always a positive number.
The Cost Type determines whether the Amount is Income (CostType = "I") or Expense (CostType <> "I").
I know what I want to do, but I don't know how to write it in SQL.
Simply put, in English, I want to exclude jobs where:
(sum(Amount) where Cost Type = "I") < (sum(Amount) where Cost Type <> "I")
I don't know if this can even be translated into SQL or I'm trying to do something impossible.
This is what I came up with:
SELECT
detail.TCONO as COMPANY,
detail.TJBNO as JOB_NUM,
detail.TCSTY as COST_TYPE,
Sum(detail.TAMAT) AS AMOUNT,
Sum(detail.TRGHR) AS REG_HOURS,
Sum(detail.TOVHR) AS OVT_HOURS,
Sum(detail.TOTHR) AS OTH_HOURS,
summary.DESCR AS DESCRIPTION
FROM detail INNER JOIN summary ON (detail.TJBNO = summary.GJBNO) AND (detail.TDVNO = summary.GDVNO) AND (detail.TCONO = summary.GCONO)
WHERE EXISTS (SELECT * FROM summary WHERE summary.GDTLU >= 20050101 AND detail.TJBNO = summary.GJBNO AND summary.GCONO = 1 AND summary.GDVNO = 0 AND (Sum(detail.TAMAT) WHERE detail.TCSTY = 'I') < (Sum(detail.TAMAT) WHERE detail.TCSTY <> 'I'))
AND (detail.TCONO = 1 AND detail.TDVNO = 0)
GROUP BY detail.TCONO, detail.TJBNO, detail.TCSTY, summary.DESCR
When I parse the query, I get the following error message:
Connection or SQL sentence error: (DA00005)
[IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0199 - Keyword WHERE not expected. Valid tokens: < > = <> <= !< !> != >= ¬< ¬> ¬= IN IS NOT LIKE BETWEEN.-199
Like I said, I'm new to SQL and don't have a clue if this is possible with SQL or not. Apparently it definitely won't work the way I tried to do it.
Is there a way to do it?