Skip to content

Instantly share code, notes, and snippets.

@mjdominus
Last active December 13, 2015 19:18
Show Gist options
  • Save mjdominus/4961137 to your computer and use it in GitHub Desktop.
Save mjdominus/4961137 to your computer and use it in GitHub Desktop.
my %kv = map {$_ => [ $args{$_}, undef ]} @fields;
my $nullcount = join " + ", map "(me.$_ IS NULL)", @fields;
my $min_nulls = $self->base_rs->search(
\%kv,
{ 'select' => [ { "min" => \$nullcount } ],
});
my $rs = $self->base_rs->search(\%kv);
$rs = $rs->search(
{ $nullcount =>
{ "=", $min_nulls->as_query }});
----------------------------------
SELECT me.max_payment_id, me.location_id, me.department_id, me.client_id, me.payment_channel_id, me.payment_type, me.max_payment_amount FROM max_payments me
WHERE ( ( (me.payment_channel_id IS NULL) + (me.payment_type IS NULL) + (me.client_id IS NULL) + (me.department_id IS NULL) + (me.location_id IS NULL)
=
(SELECT MIN( (me.payment_channel_id IS NULL) + (me.payment_type IS NULL) + (me.client_id IS NULL) + (me.department_id IS NULL) + (me.location_id IS NULL) )
FROM max_payments me
WHERE ( ( ( client_id = ? OR client_id IS NULL ) AND
( department_id = ? OR department_id IS NULL ) AND
( location_id = ? OR location_id IS NULL ) AND
( payment_channel_id = ? OR payment_channel_id IS NULL ) AND
( payment_type = ? OR payment_type IS NULL ) ) ))
AND
( ( client_id = ? OR client_id IS NULL ) AND
( department_id = ? OR department_id IS NULL ) AND
( location_id = ? OR location_id IS NULL ) AND
( payment_channel_id = ? OR payment_channel_id IS NULL ) AND
( payment_type = ? OR payment_type IS NULL ) ) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment