Thread: Group by failing on Null values

Group by failing on Null values

From
Caleb Simonyi-Gindele
Date:
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt">I have a SELECT
querythat basically adds up my sales, removes credit adjustments (eg. Returns) and gives the net
figure.</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt"> </span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt">This is the query:</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt">SELECT (SELECT SUM(cli_tran_amount) FROM
vetpmardetWHERE cli_credit_adj_trans_no IN (SELECT sys_tran_number from vetpmardet WHERE cli_tran_trans_date BETWEEN
'2004-07-11'AND '2004-07-17' )) + SUM(cli_tran_amount) AS amount FROM vetpmardet WHERE cli_tran_trans_date BETWEEN
'2004-07-11'AND '2004-07-17' AND sys_transaction_type LIKE 'C' </span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt">Thesubquery in there is necessary to link the credit back to the original transaction. This
queryworks well as far as I’m concerned.</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt"> </span></font><pclass="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt">WhereI run into problems is when I try to break the report down by staff codes. Unfortunately,
myaccounting s/w does not put a staff code with the credit adjustment, but I do want to be able to see the net sales by
staffcode. So I modify the query like this: (add one select column and a group by clause) </span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt"> </span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt">SELECT dat_staff_code, (SELECT SUM(cli_tran_amount) FROM vetpmardet
WHEREcli_credit_adj_trans_no IN (SELECT sys_tran_number from vetpmardet WHERE cli_tran_trans_date BETWEEN '2004-07-11'
AND'2004-07-17' )) + SUM(cli_tran_amount) AS amount FROM vetpmardet WHERE cli_tran_trans_date BETWEEN '2004-07-11' AND
'2004-07-17'AND sys_transaction_type LIKE 'C' GROUP BY dat_staff_code</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt"> </span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt">But this query will not produce a row for dat_staff_code when it’s value is
null.It only produces grouped rows where dat_staff_code is not null, and the sum of those rows does not equal the value
ofthe single row returned in the first query above. How do I get it to produce a row even when dat_staff_code is
null?</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt"> </span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt">TIA</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt">Caleb</span></font></div> 

Re: Group by failing on Null values

From
Rosser Schwarz
Date:
while you weren't looking, Caleb Simonyi-Gindele <csg@bordervet.ca> wrote:

> How do I get it to produce a row even when dat_staff_code is null?

try something like

SELECT coalesce(dat_staff_code, 0)...GROUP BY dat_staff_code

That will substitute a zero for any NULL value in that column, which
GROUP BY should then pick up.

/rls

-- 
:wq


locks and triggers. give me an advice please

From
sad
Date:
Good day.

often, I am turning triggers off and on to perform a mass operation on a
table, and i am interested how should i care of another user operations.

the scene is:
table t1 with user defined triggers
and many tables reference t1, (so FK triggers defined on t1)

the operation i want to perform on t1 makes a great load to a server
and have no use in triggers at all.
the best way to perform this operation is to delete all records, modify, and
insert them back without changing any adjuscent table.
(this way takes a few seconds.)
so i turn off triggers on t1 completely (updating pg_class.reltriggers)
operate
and turn on triggers on t1.

it works fine.

the question is:

what should i do to prevent other users of data modification on the t1 and the
adjuscent tables while triggers is off ?

thnx.


P.S.
...what about TEXT to REGCLASS casting ?




Re: locks and triggers. give me an advice please

From
Steve Crawford
Date:
> often, I am turning triggers off and on to perform a mass operation
> on a table, and i am interested how should i care of another user
> operations.
>
> the scene is:
> table t1 with user defined triggers
> and many tables reference t1, (so FK triggers defined on t1)
>
> the operation i want to perform on t1 makes a great load to a
> server and have no use in triggers at all.
> the best way to perform this operation is to delete all records,
> modify, and insert them back without changing any adjuscent table.
> (this way takes a few seconds.)
> so i turn off triggers on t1 completely (updating
> pg_class.reltriggers) operate
> and turn on triggers on t1.
>
> it works fine.
>
> the question is:
>
> what should i do to prevent other users of data modification on the
> t1 and the adjuscent tables while triggers is off ?

If I understand your question correctly you should use a transaction 
and lock the table;

begin transaction;
lock t1 in access exclusive mode;

Turn off triggers and do your updates.
(Note, "truncate t1" is faster than "delete from t1" followed by a 
"vacuum full" and you might consider running "reindex table t1" after 
your mass update or if appropriate drop your indexes, load the data, 
then recreate them.)

Re-establish triggers.

commit; --end of transaction unlocks the table


Cheers,
Steve



Re: locks and triggers. give me an advice please

From
sad
Date:
thnx.

i try to sound the idea to ensure myself that you are right.

> begin transaction;
> lock t1 in access exclusive mode;
>
> Turn off triggers and do your updates.
> (Note, "truncate t1" is faster than "delete from t1" followed by a
> "vacuum full" and you might consider running "reindex table t1" after
> your mass update or if appropriate drop your indexes, load the data,
> then recreate them.)
>
> Re-establish triggers.
>
> commit; --end of transaction unlocks the table

in case another user inserts a record into an adjuscent table with the value
of reference field NOT IN t1.
the constraint causes reading of t1 to look up FK value IN t1.
so my EXCLUSIVE lock prevents even reading and this user operation will be
queued.