Tables Summary into single table in laravel - laravel

I have two tables named 'Customers' and 'Committeetables' in mysql database. They have following columns
Table: Customer
nd Name FatherName Cell# AdvanceAmount
25 Waseem Asghar 0302 2500
30 Ramzan Khan 0303 3500
35 Rana Ali 0307 2000
Table: CommitteeTables
nd Amount RecievingDate DrawDate
25 1500 2-10-15 10-10-15
30 1500 2-10-15 10-10-15
25 1500 3-10-15 10-11-15
30 1500 3-10-15 10-11-15
35 1500 3-10-15 10-11-15
A Column 'nd' in 'Customers' table is primary key and in 'CommitteeTables' table is foreign key. It means 'Customers' table joins with 'ComitteeTables' table through 'nd' column.
Expected Result table should be as under:
nd Name Father Name Cell# Advance Amount Balance
25 Waseem Asghar 0302 25000 3000 22000
30 Ramzan Khan 0303 35000 3000 32000
32 Rana Ali 0307 20000 1500 18500
----------------------------------------------------------------------------
total 80000 7500 72500
I am using Laravel 5 and HTML, Bootstrap. I need this result in Laravel 5. I shall be really thankful if someone help me to solve my problem.
Thanks
Waseem

This is not at all a Laravel question. It's a general SQL question.
Something like this:
SELECT nd, Name, FatherName, Cell#, AdvanceAmount as Advance,
(SELECT SUM(Amount) FROM CommitteeTables WHERE CommitteeTables.nd=Custoemr.nd) as Amount, Advance-Balance AS Balance
FROM Customer;
I'm not entirely sure that's correct but you can try it and fix any errors. As Bogdan suggests you should try something first then come back here with any problems that you have.

Related

Delete unused records from oracle table

DB version : 11.2.0.4
OS Solaris 5.10
REQUIREMENT : Delete unused records from table only after finding if the record is being accessed or not.
We have a table employee, which has 100,000 records, if anyone select a particular record/records from employee table then, the respective record's STATUS column should get updated to 'ACTIVE' value.
This is required for auditing purpose, 60 days later we will delete all the records from employee table whose STATUS column value is is NULL. how can this be achieved?
My understanding so far, correct me if I am wrong:
1) Trigger can't be used as there is no SELECT event, we only have UPDATE, INSERT, DELETE event.
2) Oracle FGA (Fine grain auditing) may not solve the purpose. or may be I am not aware, is it doable with FGA?
Table:
CREATE TABLE EMPLOYEE
(
EMPID NUMBER,
NAME VARCHAR2(20 BYTE),
SALARY NUMBER,
DEPART VARCHAR2(100 BYTE),
STATUS VARCHAR2(100 BYTE)
)
sample records:
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH
106 ROMEO 5000 TECH
107 TERI 5000 TECH
108 ROBERT 5000 TECH
Example:
If any one issue below statements
query 1: SELECT * from EMPLOYEE where name='MESSI';
the auditing should update the STATUS='ACTIVE' of empid=105
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH 'ACTIVE'
106 ROMEO 5000 TECH
107 TERI 5000 TECH
108 ROBERT 5000 TECH
query 2: SELECT * from EMPLOYEE where DEPART='TECH';
The auditing should update the STATUS='ACTIVE' for empid=105,106,107,108
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH 'ACTIVE'
106 ROMEO 5000 TECH 'ACTIVE'
107 TERI 5000 TECH 'ACTIVE'
108 ROBERT 5000 TECH 'ACTIVE'
The solution suggested by Oracle to achieve what to want is Known as ILM or Information Lifecycle Management.
It allows you to archive, delete, move or do specific actions depending on some criterias (Such as last access).
But be aware, it requires an additional license.
I have a suggestion :
Enable SELECT audit on the target table
Create an AFTER INSERT TRIGGER that will get the inserted line in DBA_AUDIT_TRAIL et extract SQL_TEXT if the OBJECT_NAME was your table (+others checks)
Inside this trigger, do some string operations to get the WHERE CONDITION & use it for update.
Sounds like an ambiguous requirement to me. What happens if one does select * or select count(*)? That under the specification would make all employee records used.
The view v$SQL would contain "all" SQL statements issued, you could always look in there (going through v$plan_table with the object). Then use those to reverse engineer if the record was touched.
You may try FGA ( fine-grained Audit ) using the DBMS_FGA Package. Try Running the code block below in your schema. If it executes successfully , check if audits are lodged periodically in the DBA_COMMON_AUDIT_TRAIL or DBA_AUDIT_STATEMENT table ( check documentation ) while running a select query. Schedule a program that runs periodically to check the audit table and update the status column accordingly. I have never tried anything like this, However!.
BEGIN
dbms_fga.Add_policy(object_schema => 'HR', object_name => 'EMPLOYEE',
policy_name => 'STATUS_UPDATE', audit_condition => 'SELECT',
audit_column => 'NAME ,DEPART', handler_schema => NULL,
handler_module => NULL,
ENABLE => TRUE);
END;
/

Transpose without PIVOT in ORACLE

currently I am using pl/sql Developer(Oracle). I am told to convert a Row wise arranged data into columns but without the use of PIVOT. Since the Table I am working on dynamically changes, I am not able to use DECODE too.
POLICY SEQS INVDATE SUM(AMT)
-------- ------ ----------- ----------
policA 123 01-JAN-10 40
policA 123 01-FEB-10 50
policA 123 01-MAR-10 60
policA 456 01-JAN-10 360
policA 456 01-FEB-10 450
policA 456 01-MAR-10 540
policA 789 01-FEB-10 1000
polcA 789 01-MAR-10 1000
I have to re-arrange the dates and the sum of amounts column wise. So that the Single Policy and Single SEQS will have the dates and its amount column wise in a line.
"POLICY","SEQS","INST1","INST1SUM","INST2","INST2SUM","INST3","INST3SUM"
"policA","123","01-JAN-10","40","01-FEB-10","50","01-MAR-10","60"
"policA","456","01-JAN-10","360","01-FEB-10","450","01-MAR-10","540"
"policA","789","01-FEB-10","1000","01-MAR-10","1000"
Some Policy might not be starting from Jan, so the INST1 must be from feb, INST2 must be Mar and INST3 and corresponding INSTSUM must be NULL.
Is there any way that this can be done using CROSS JOINS or using xml function?
Can I use xmlagg with alternative data (INST and SUM)?
I have done some research and am not able to solve this out. Can you please help me with this?

sql server Min, Max range

I have a huge table
startip endip country
1111 2000 in
2001 3000 in
3001 4000 in
4001 5000 chi
5001 6000 chi
I want to merge all these rows like
startip endip country
1111 4000 in
4001 6000 chi
I want do this in sqlserver.
Min(start_ip) , Max(end_ip) for DISTINCT(country)
Any help ?
Use GROUP BY.
For example:
SELECT country, MIN(start_ip), MAX(end_ip)
FROM a_table
GROUP BY country
Not sure what your table schema looks like but I think what you're trying to achieve will be accomplished using GROUP BY:
SELECT country, MIN(start_ip), MAX(end_ip) FROM your_table
GROUP BY country

Aggregating the table from many rows to one

I have a table with data as follows:
Name opening receipt transfer closing
abcd 1000 40 30 1010
efg 256 109 219 146
hjk 9356 210 210 9356
mnp 2000
I need is a result like this -
opening_abcd receipt_abcd transfer_abcd closing_abcd opening_efg receipt_efg .....
1000 40 30 1010 256 109 .....
What I have tried till now is to create multiple views one by one having the one row values like -
select opening opening_abcd, receipt receipt_abcd, transfer transfer_abcd, closing closing_abcd
from this_table
where name = 'abcd'
and similarly for others and then I have merged them together by using JOIN.
Is there a solution possible if I can select all of them in one query or any other way which is better than mine?

Hive: Joining two tables with different keys

I have two tables like below. Basically i want to join both of them and expected the result like below.
First 3 rows of table 2 does not have any activity id just empty.
All fields are tab separated. Category "33" is having three description as per table 2.
We need to make use of "Activity ID" to get the result for "33" category as there are 3 values for that.
could anyone tell me how to achieve this output?
TABLE: 1
Empid Category ActivityID
44126 33 TRAIN
44127 10 UFL
44128 12 TOI
44129 33 UNASSIGNED
44130 15 MICROSOFT
44131 33 BENEFITS
44132 43 BENEFITS
TABLE 2:
Category ActivityID Categdesc
10 billable
12 billable
15 Non-billable
33 TRAIN Training
33 UNASSIGNED Bench
33 BENEFITS Benefits
43 Benefits
Expected Output:
44126 33 Training
44127 10 Billable
44128 12 Billable
44129 33 Bench
44130 15 Non-billable
44131 33 Benefits
44132 43 Benefits
It's little difficult to do this Hive as there are many limitations. This is how I solved it but there could be a better way.
I named your tables as below.
Table1 = EmpActivity
Table2 = ActivityMas
The challenge comes due to the null fields in Table2. I created a view and Used UNION to combine result from two distinct queries.
Create view actView AS Select * from ActivityMas Where Activityid ='';
SELECT * From (
Select EmpActivity.EmpId, EmpActivity.Category, ActivityMas.categdesc
from EmpActivity JOIN ActivityMas
ON EmpActivity.Category = ActivityMas.Category
AND EmpActivity.ActivityId = ActivityMas.ActivityId
UNION ALL
Select EmpActivity.EmpId, EmpActivity.Category, ActView.categdesc from EmpActivity
JOIN ActView ON EmpActivity.Category = ActView.Category
)
You have to use top level SELECT clause as the UNION ALL is not directly supported from top level statements. This will run total 3 MR jobs. ANd below is the result I got.
44127 10 billable
44128 12 billable
44130 15 Non-billable
44132 43 Benefits
44131 33 Benefits
44126 33 Training
44129 33 Bench
I'm not sure if I understand your question or your data, but would this work?
select table1.empid, table1.category, table2.categdesc
from table1 join table2
on table1.activityID = table2.activityID;

Resources