Laravel 5.7: Get the value with greater than 0 with and condition - laravel-5.7

I have project and the technology were using is Laravel. I have table purchase_entries where my columns are: debit & credit amount etc.
I want to display only greater than to 0 of debit & credit.
If both 0.00 debit and credit, it wont display.
Model
public static function getPurchaseInvoiceItemsEntries($data)
{
return DB::table('purchase_entries')
->where('purchase_id', $data)
->where('debit_amount','>=', 0.00 && 'credit_amount','>=', 0.00)
//->where('credit_amount','>=', 0.00)
->get()->toArray();
}
Example:
Debit Credit
1 5,000 0.00
2 0.00 5,000
3 0.00 0.00
3 will not display because its both 0.
Question: How can I fetch the debit & credit with greater than value to 0?

Related

Complex select in SQLite view

I have two tables where Security holds the access bit mask for a given NTFS file system scan and FileSystemRights which equates to the string representations for the well known bit masks. I need to create a view which exposes the expected (not just proper) string representations for a given bit mask. The problem is several enum values composite and contain combinations of lower values, so the desired idea is not to repeat the implicit values.
For example, a value of 1179817 (Security.Id = 24) should only report ReadAndExecute and Synchronize, excluding ExecuteFile, ListDirectory, Read, ReadAttributes, ReadData, ReadExtendedAttributes, ReadPermissions and Traverse, as those are all part of ReadAndExecute (eg. ReadAndExecute & Read == Read). Its obviously correct to show them all, but a user wants only to see the non implicit values.
I'm lost within the constraints of SQL to produce a join that behaves like this without some abysmal nested case that would be a nightmare to look at.
Does a better programmatic approach exist?
FileSystemRights
================
Id Name Value
-- ---- -----
1 None 0
2 ListDirectory 1
3 ReadData 1
4 WriteData 2
5 CreateFiles 2
6 CreateDirectories 4
7 AppendData 4
8 ReadExtendedAttributes 8
9 WriteExtendedAttributes 16
10 ExecuteFile 32
11 Traverse 32
12 DeleteSubdirectoriesAndFiles 64
13 ReadAttributes 128
14 WriteAttributes 256
15 Write 278
16 Delete 65536
17 ReadPermissions 131072
18 Read 131209
19 ReadAndExecute 131241
20 Modify 197055
21 ChangePermissions 262144
22 TakeOwnership 524288
23 Synchronize 1048576
24 FullControl 2032127
25 GenericAll 268435456
26 GenericExecute 536870912
27 GenericWrite 1073741824
28 GenericRead 2147483648
Security
========
Id FileSystemRights IdentityReference
-- ---------------- -----------------
20 2032127 BUILTIN\Administrators
21 2032127 BUILTIN\Administrators
22 2032127 NT AUTHORITY\SYSTEM
23 268435456 CREATOR OWNER
24 1179817 BUILTIN\Users
25 4 BUILTIN\Users
26 2 BUILTIN\Users
MyView
======
SELECT s.Id AS SecurityId,
f.Name
FROM Security s
JOIN FileSystemRights f
ON CASE f.Value
WHEN 0 THEN s.FileSystemRights = f.Value
ELSE (s.FileSystemRights & f.Value) == f.Value
END
ORDER BY s.Id, f.Name;
Add the actual value of the name to the query.
Then wrap another query around that to filter out values for the same entry that are a subset of another value:
WITH AllValues(SecurityId, Name, Value) AS (
SELECT s.Id,
f.Name,
f.Value
FROM Security s
JOIN FileSystemRights f
ON CASE f.Value
WHEN 0 THEN s.FileSystemRights = f.Value
ELSE (s.FileSystemRights & f.Value) == f.Value
END
)
SELECT SecurityId,
Name
FROM AllValues
WHERE NOT EXISTS (SELECT *
FROM AllValues AS AV2
WHERE AV2.SecurityId = AllValues.SecurityId
AND (AV2.Value & AllValues.Value) != 0
AND AV2.Value > AllValues.Value
)
ORDER BY 1, 2;

convert percentage to range [-1;1]

I am working on a project in PowerBI.
I want to convert a column of decimals (percentages) to a range from -1 to 1.
This is the table I'm using right now:
FirstName | Score
Jack | 0.75
John | 0.50
Reese | 0.00
Mike | 1.00
And the desired result is:
FirstName | Score
Jack | 0.50
John | 0.00
Reese | -1.00
Mike | 1.00
I am strugling with the math behind this. If I got that part, I could make it by using m-language in PowerBI.
Any ideas, suggestions?
Those scores don't actually look like percentages, they're more like fractions - for example, the percentage for 0.75 would be 75.
So, if you're talking about mapping 0..1 to -1..1, it's a simple matter to apply the transformation:
newVal = oldVal * 2 - 1
The multiplication by two first scales it into the range 0..2 while the subtraction then shifts it into the range -1..1.
I think it's very entitled of you to present no work at all and ask for a free solution. Any basic statistics text will describe how to rebase and scale a set of data. However, this will do what you ask
use strict;
use warnings 'all';
use List::Util qw/ min max /;
my #data = <DATA>;
my ($min, $max, $delta);
{
my #n = map /([\d.]+)\s*\z/, #data;
( $min, $max ) = ( min(#n), max(#n) );
$delta = $max - $min;
}
for ( #data ) {
s{ ( [\d.]+ ) (?= \s*\z ) }{ sprintf '%.2f', ( $1 - $min ) * 2 / $delta - 1 }xe;
print;
}
__DATA__
FirstName | Score
Jack | 0.75
John | 0.50
Reese | 0.00
Mike | 1.00
output
FirstName | Score
Jack | 0.50
John | 0.00
Reese | -1.00
Mike | 1.00

How can the invoice round off amount be shown in the purchase register report for only one of the invoice lines?

In the purchase register report, I'm displaying all the invoice lines.
I have included one column called Roundoff (invoice round off amount). This column is based on a display method. Currently the round off amount is displayed for each invoice line:
VocherId InvoiceID ItemCode Roundoff TotValue
VC-00001 INV-001 ITM-001 0.50 120.50
VC-00001 INV-001 ITM-002 0.50 146.00
VC-00001 INV-001 ITM-003 0.50 221.00
VC-00002 INV-002 ITM-002 0.25 175.75
VC-00002 INV-002 ITM-003 0.25 167.00
But it should only be displayed for one of the invoice lines like:
VocherId Invoice ID ItemCode Roundoff TotValue
VC-00001 INV-001 ITM-001 0.50 120.50
VC-00001 INV-001 ITM-002 0.00 146.00
VC-00001 INV-001 ITM-003 0.00 221.00
VC-00002 INV-002 ITM-002 0.25 175.75
VC-00002 INV-002 ITM-003 0.00 167.00
How can this be achieved?
Add variables to report's classDeclaration:
real roundOff;
VendInvoiceAccount currentInvoiceId;
Add display method to the section where the roundoff column is located:
display real getRoundOff()
{
return roundOff;
}
Set DataMethod property of the field to getRoundOff.
And override executeSection for this section:
public void executeSection()
{
if (purchLine.InvoiceAccount == currentInvoiceId)
{
roundOff = 0;
}
else
{
roundOff = purchLine.RoundOff;
currentInvoiceId == purchLine.InvoiceAccount;
}
super();
}

"My trace file shows me "CPU_TIME" longer than "ELAPSED_TIME"

I'm doing TEST my own SQL on 11g RAC with CENTOS.
I ran a sql with this script.
alter session set timed_statistics = true;
alter session set STATISTICS_LEVEL = all;
alter session set EVENTS '10046 TRACE NAME CONTEXT FOREVER, LEVEL 12';
alter session set sql_trace=true;
and got trace file like below...
********************************************************************************
select ISSU_ID
,BUYR_CORP_NO
,SELR_CORP_NO
,BROK_CORP_NO
,ISSU_DATE
,ISSU_TIME
,REGS_DATE
,INSERT_DATE
,INSERT_TIME
,SERV_ID
,TAX_TYPE
,POPS_CODE
,MODY_CODE
,IMPT_NO
,ACEP_STAT_DATE
,ACEP_END_DATE
,ISSU_SND_CODE
,STAT_CODE
,ISSU_SND_VAL
,ITEM_QUANT
,CHRG_AMT
,TAX_AMT
,TOTL_AMT
,BILL_SEQ_NO
,USER_ID
,SEND_MSG
,RECB_MSG
,ACCP_YN
,RECP_CODE
,MULT_ISSU_ID
,REQ_CHNEL
,REPT_USER_ID
,BFO_ISSU_ID
,MAIL_ACP_CODE
,ERR_CODE
,ERR_MSG
,URL_ID
,MGR_ID1
,MGR_ID2
,MGR_ID3
,NOTE1
,NOTE2
,NOTE3
,DOC_TYPE
,CERT_NTS
,BILL_TYPE
from EGDB_ENC_MSTR
where rownum<10000000
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 666668 93.85 78.70 424374 1494818 0 9999999
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 666670 93.85 78.70 424374 1494818 0 9999999
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 112 (RTAXBILL)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
---------- ---------- ---------- ---------------------------------------------------
9999999 9999999 9999999 COUNT STOPKEY (cr=1494818 pr=424374 pw=0 time=22043507 us)
9999999 9999999 9999999 TABLE ACCESS FULL EGDB_ENC_MSTR (cr=1494818 pr=424374 pw=0 time=18632205 us cost=122336 size=3529999647 card=9999999)
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
9999999 COUNT (STOPKEY)
9999999 TABLE ACCESS MODE: ANALYZED (FULL) OF 'EGDB_ENC_MSTR' (TABLE)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 666668 0.00 1.58
enq: KO - fast object checkpoint 3 0.00 0.00
reliable message 1 0.00 0.00
direct path read 2 0.01 0.01
SQL*Net message from client 666668 0.01 2399.08
SQL*Net more data to client 2 0.00 0.00
********************************************************************************
I have 3 questions.
Why CPU_TIME is longer than ELAPSED_TIME?
I read a doc telling me that ELAPSED_TIME includes wait event time. If it's true, Why CPU_TIME is longer than ELAPSED_TIME?
When I run a query on the client, It returns result sets in few milliseconds though, my trace file says ELAPSED_TIME is 78.70 SEC. Is this relevant to FETCH CALL? If it's true, ELAPSED_TIME in trace file is the amount time for fetching all result-set not for ArraySize. Is this right?
In wait event section, It says total wait time of SQL*Net message from client is 2399 SEC though, ELAPSED_TIME is much shorter than that. Why is this?
I'm so curious about the stats in trc file but I'm new to oracle database actually. Any advice would be very appreciated.

Dictionary with a running total

This is for a homework I am doing.
I have a .txt file that looks like this.
11
eggs
1.17
milk
3.54
bread
1.50
coffee
3.57
sugar
1.07
flour
1.37
apple
.33
cheese
4.43
orange
.37
bananas
.53
potato
.19
What I'm trying to do is keep a running total, when you type in the word "Eggs" then the word "bread" it needs to add the cost of both and keep going until "EXIT" also I'm going to run into a 'KeyError' and need help with that also.
def main():
key = ''
infile = open('shoppinglist.txt', 'r')
total = 0
count = infile.readline()
grocery = ''
groceries = {}
print('This program keeps a running total of your shopping list.')
print('Use \'EXIT\' to exit.')
while grocery != 'EXIT':
grocery = input('Enter an item: ')
for line in infile:
line = line.strip()
if key == '':
key = line
else:
groceries[key] = line
key = ''
print ('Your current total is $'+ groceries[grocery])
main()
Does the file contain the prices of each of the different groceries?
The user input statement should have a .strip() at the end too as sometimes line ending characters can be included from user input.
You should only need to read the file once, not in the loop.
When the user enters a grocery item it should as you say check that it exists:
if grocery in groceries:
...
else:
#grocery name not recognised
I think you should have a separate dictionary to store the counts of each grocery something like this: http://docs.python.org/library/collections.html#collections.Counter
import collections
quantitiesWanted = collections.Counter()
Then any grocery can be asked for like this quantitiesWanted['eggs'] which will return 0 by default. Doing something like quantitiesWanted['eggs'] += 1 will increase it to 1 and so on.
To get the current total you can do:
total = 0
for key, value in quantitiesWanted:
total += groceries[key] * value

Resources