Database query to select the maximum row with user_id - codeigniter

i need to select the maximum database entry row from the table with the user_id
table name: cd_production
code_cd etaireia etos
______________________
400400 ODEON 1987
400401 ODEON 1986
400412 COLUMBIA 1990
400420 COLUMBIA 1990
400657 LYRA 1965
410000 COLUMBIA 1962
410001 COLUMBIA 1964
410003 PHILIPS 1979
410005 PHILIPS 1971
420430 ODEON 2002
420440 LYRA 2005
420450 COLUMBIA 2009
420460 ODEON 2007
420470 PHILIPS 2008
420480 ODEON 2002
420490 COLUMBIA 2010
500500 SONY 1968
500510 SONY 1972
600601 COLUMBIA 1962
600602 COLUMBIA 1963
600603 ODEON 1964
670670 PHILIPS 1983
asad

You should try something like this
$result=this->db->query("select max(user_id) as maxValue form tableName")->result_array();
$maxValue=$result[0]['maxValue']; //or $result['maxValue'];
echo $maxValue;
$result2=this->db->query("select * from tableName user user_id=$maxValue")->result_array();
var_dump($result2);

Related

Using a 0 rather than skipping the group entirely if nothing is found

How can I use a 0 as a "placeholder" here?
=QUERY(Data!A1:G578, "select G, sum(C) where D='Income' group by G label sum(C) 'Total Income'")
So that
Month
Total Income
August 2021
$784.59
October 2021
$200.00
November 2021
$312.74
becomes
Month
Total Income
August 2021
$784.59
September 2021
$0.00
October 2021
$200.00
November 2021
$312.74
The quickest hacky fix would be just to have an entry of 0$ for September in the data somewhere, but I am wondering if there is a more ""professional"" way to do it

How to categorize data in a ORACLE table based on a primary key?

I have a table TRAVEL with column TRIP_ID as primary key.
SOURCE DATA
TRIP_ID PERSON_NAME DESTINATION SOURCE TRANSACTION_ID TRIP_COUNT
100 Mike London Zurich 1000B112 1
101 Mike Paris Capetown 1000B112 1
102 Mike Moscow Madrid 1000B112 1
103 John Delhi Moscow 1100A110 1
104 John Toronto Zurich 1100A110 1
105 Mary Chennai Madrid 1100A111 1
106 Mary Berlin Zurich 1100A111 1
EXPECTED RESULTS:
when I do select * from TRAVEL where TRANSACTION_ID = 1100A111 it returns below two rows as below .
so I want my data to be categorized based on the transaction_ID on a run time.I dont want to hardcode the value for transaction-ID each time as above but i want to group it in such a way that it should fetch me the above expected results.I mean it should return all the data which are corresponding to the TransactionID in the table and it should not sum up the TRIP COUNT.It should return me the rows as below in my table.I am ok to create view .Please suggest
TRIP_ID PERSON_NAME DESTINATION SOURCE TRANSACTION_ID TRIP_COUNT
105 Mary Chennai Madrid 1100A111 1
106 Mary Berlin Zurich 1100A111 1
Can someone suggest a query in ORACLE to handle this ? I donot want to hardcode transaction ID
Regards
Sameer

How to create a before or after Trigger that can catch DUP_VAL_ON_INDEX on Oracle PL/SQL

I have a table like this
KD_C KD_PLA KD_T GABUNG BERAKHIR GAJI_PL BUYOUT
---- ------ ---- ---------- ---------- ---------- ----------
C001 MA001 T006 2003 2014 50000 5200000
C002 SC001 T006 2012 2016 65000 20280000
C003 TW001 T006 2005 2018 90000 46800000
C004 TV001 T006 2008 2017 60000 24960000
C005 PC001 T001 2003 2016 80000 24960000
C006 AC001 T001 1996 2014 90000 9360000
C007 DB001 T001 2010 2016 65000 20280000
C008 EH001 T001 2011 2018 85000 44200000
C009 JC001 T002 1996 2014 60000 6240000
C010 SG001 T002 1998 2016 87000 27144000
C011 LS001 T002 2010 2018 81000 42120000
C012 PR001 T002 2004 2016 60000 18720000
C013 JH001 T003 2005 2018 72000 37440000
C014 GC001 T003 2003 2015 65000 13520000
C015 ED001 T003 2010 2018 100000 52000000
C016 GB001 T003 2010 2016 80000 24960000
C017 DG001 T004 2011 2018 73000 37960000
C018 RG001 T004 1992 2014 90000 9360000
C019 PJ001 T004 2011 2018 80000 41600000
C020 RP001 T004 2012 2017 92000 38272000
C021 GB002 T005 2006 2018 102000 53040000
C022 EA001 T005 2011 2015 70000 14560000
C023 HL001 T005 2012 2018 65000 33800000
C024 KW001 T005 2009 2017 67000 27872000
C025 MA001 T005 2017 2022 50000 26000000
C028 MA001 T001 2016 2018 15000 3120000
C029 MA001 T001 2016 2018 15000 3120000
C030 MA001 T001 2016 2018 15000 3120000
And then I tried to make a Trigger for updating instead of insert to the table when duplicate a primary key. The primary key is the first coloumn 'KD_CONTRACT'.
CREATE OR REPLACE TRIGGER INSERT_TABEL_CONTRACT
BEFORE INSERT ON CONTRACT
FOR EACH ROW
DECLARE
KODE VARCHAR2(20);
TEMPCARIKODE NUMBER;
BEGIN
SELECT LPAD(TO_NUMBER(NVL(SUBSTR(MAX(KD_CONTRACT),2),0))+1,3,'0') INTO KODE
FROM CONTRACT;
SELECT COUNT(KD_CONTRACT) INTO TEMPCARIKODE
FROM CONTRACT
WHERE KD_CONTRACT = :NEW.KD_CONTRACT;
IF(TEMPCARIKODE = 0) THEN
:NEW.KD_CONTRACT := 'C'||KODE;
:NEW.GABUNG := TO_NUMBER(TO_CHAR(SYSDATE,'YYYY'));
:NEW.BERAKHIR := :NEW.GABUNG + :NEW.BERAKHIR;
:NEW.BUYOUT := (:NEW.GAJI_PL * 52) * (:NEW.Berakhir-:NEW.Gabung) * 2;
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('ERROR at Line 1:');
DBMS_OUTPUT.PUT_LINE('ORA-20003: Data sudah ada, data akan diupdate!');
END;
/
show err;
anyway maybe the code show that I tried not to update when the primary key is duplicated. But please the until end first.
it show no error... So the trigger successfully created.
then I tried to insert a duplicated primary key.
INSERT INTO CONTRACT VALUES('C001','MA001','T001',2013,2,15000,2500000);
the result
INSERT INTO CONTRACT VALUES('C001','MA001','T001',2013,2,15000,2500000)
*
ERROR at line 1:
ORA-00001: unique constraint (TUGAS.PK_CONTRACT) violated
It did raise ORA-00001 but it never touched the DUP_VAL_ON_INDEX and showed nothing or the custom message.
Anyone have Idea? When it never even touch DUP_VAL_ON_INDEX then how could I event paste an update query at the when case...
Thanks...
* anyway my oracle is XE or express edition.
The EXCEPTION block from your trigger will never be executed since your trigger doesn't contain any INSERT or UPDATE statements.
You have to consider INSTEAD OF Trigger instead.

Creating dataset outputs..where to start with this one?

The variables in the data set are: Event, EventType, FName, LName, Age, Gender, Score.
I am trying to create a report that gives me the Lowest 5 scores per event/event type per each Age(18-65) per Gender.
For example, I want the 5 lowest scores for everyone who participated in EventA EventtypeB who were 18 year old females then I want all the 19 year old females and so on.. For each gender. A side note- Not all ages have 5 participants.. For example there may be no 20 year olds who participated and there may only be 2 21 year olds.
I originally tried to tackle this by making a bunch of separate data sets for each age, but I know there must be a better way to do it. I would appreciate any help thank you I am pretty new to SAS but I have introductory experience in all aspects.
Here is some sample input:
Mile Sprint John Smith 19 Male 15.31
Mile Sprint Alex Doe 19 Male 13.21
Mile Sprint Ian Sore 19 Male 23.51
Mile Sprint Sean Lae 19 Male 12.34
Mile Sprint Mike Rai 19 Male 17.27
Mile Sprint Connor Te 19 Male 11.23
Mile Sprint Simon Doe 19 Male 15.21
Mile Long Jane Joy 37 Female 35.12
Mile Long Victoria K 37 Female 27.31
Mile Long Chris Li 25 Male 23.43
For the Mile Sprint 19 Males I would want it to return:
Mile Sprint Connor Te 19 Male 11.23
Mile Sprint Sean Lae 19 Male 12.34
Mile Sprint Alex Doe 19 Male 13.21
Mile Sprint Simon Doe 19 Male 15.21
Mile Sprint John Smith 19 Male 15.31
For the Mile Long 37 Female I would want it to just return this due to there not being 5 participants:
Mile Long Victoria K 37 Female 27.31
Mile Long Jane Joy 37 Female 35.12
With the sample input shown I am trying to get the 5 lowest scores for Mile Sprint for Males age 19. Then the same for age 20-65. Then the same for Mile Long for all males. Vice versa for females. With the assumption that there may not be 5 participants in a race or there may be more than 5. Is there anyway to do all of this in one or two dataset outputs?
/* Creating Sample dataset */
data input_dataset;
infile datalines dlm=",";
input Event : $10.
EventType : $ 10.
FName : $10.
LName : $10.
Age : 8.
Gender : $10.
Score : 8.
;
datalines;
Mile,Sprint,John,Smith,19,Male,15.31
Mile,Sprint,Alex,Doe,19,Male,13.21
Mile,Sprint,Ian,Sore,19,Male,23.51
Mile,Sprint,Sean,Lae,19,Male,12.34
Mile,Sprint,Mike,Rai,19,Male,17.27
Mile,Sprint,Connor,Te,19,Male,11.23
Mile,Sprint,Simon,Doe,19,Male,15.21
Mile,Long,Jane,Joy,37,Female,35.12
Mile,Long,Victoria,K,37,Female,27.31
Mile,Long,Chris,Li,25,Male,23.43
;
run;
/* Sorting based on desired parameters - event EventType age Gender */
proc sort data = input_dataset;
by event EventType age Gender score;
run;
/*Picking the lowest five scores based on above parameters */
data input_dataset_1(drop=num);
set input_dataset;
retain num;
by event EventType age Gender score;
if first.gender then num=1 ; else num=num+1;
if num<=5;
run;

i want Oracle Query for Formatted Output

Following is my table deign
Ecode Degree YOQ
7654321 SSC 2000
7654321 HSC 2002
7654321 Bcom 2006
7654321 MCA 2010
7654322 SSC 2002
7654322 HSC 2004
7654322 Bcom 2007
7654323 SSC 2002
7654323 HSC 2004
7654323 BE 2008
i want following formatted output using oracle query
Ecode Degree year
7654321 SSC,HSC,Bcom,MCA 2000,2002,2006,2010
7654322 SSC,HSC,Bcom 2002,2004,2007
7654323 SSC,HSC,BE 2002,2004,2008
Thanks in Advance
Vivek shah
SELECT ecode,wm_concat(DEGREE),wm_concat(yoQ) FROM TABLE_NAME GROUP BY ecode

Resources