GET SUM OF TOTAL ATTENDANCE OF SINGLE EMPLOYEE IN ORACLE SQL DEVELOPER - oracle-sqldeveloper

INSERT INTO COD_HRM_ADVICED_MONTH_DETAIL
(EMPLOYEE_ID,MONTH_ID,TOTAL_PRESENT,TOTAL_ABSENT,TOTAL_LEAVE,ACTION_ON,ACTION_BY,ACTION_TYPE,PROJECT_ID,COMPANY_ID)
SELECT A.EMPLOYEE_ID,'25',
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='P' AND to_char(ATTENDANCE_DATE, 'mm')='08'
AND to_char(ATTENDANCE_DATE, 'yy')='21'
AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS PRESENT,
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='A' AND to_char(ATTENDANCE_DATE, 'mm')='08'
AND to_char(ATTENDANCE_DATE, 'yy')='21'
AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS ABSENT,
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='L' AND to_char(ATTENDANCE_DATE, 'mm')='' AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS LEAVE,
'10-Aug-21','1','Insert','4','2'
FROM COD_HRM_ATTENDANCE A GROUP BY EMPLOYEE_ID
I get the detail of all the present absent and leave employee in
different column now I want to sum of these column for every single
employee.
1:

SELECT A.EMPLOYEE_ID,
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='P' AND to_char(ATTENDANCE_DATE, 'mm')='08'
AND to_char(ATTENDANCE_DATE, 'yy')='21'
AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS PRESENT,
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='A' AND to_char(ATTENDANCE_DATE, 'mm')='08'
AND to_char(ATTENDANCE_DATE, 'yy')='21'
AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS ABSENT,
(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE ATTENDANCE_TYPE='L' AND to_char(ATTENDANCE_DATE, 'mm')='08' AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS LEAVE
,(SELECT COUNT(*) FROM COD_HRM_ATTENDANCE
WHERE to_char(ATTENDANCE_DATE, 'mm')='08' AND EMPLOYEE_ID=A.EMPLOYEE_ID) AS TOTAL
FROM COD_HRM_ATTENDANCE A GROUP BY EMPLOYEE_ID
i solved this problem by count all without giving any attendance type

Related

postgres how to insert values with 2 selects

I'm trying to do a query on Postgres but it's not working. I'd like to create an insert query with 2 select:
Example :
INSERT INTO table1 (id_1, id_2)
SELECT id from table_2 where code='01',
SELECT id from table_2 where code='02';
I don't find the good syntax for this.
I believe below query will works for your use case
INSERT INTO stats(totalProduct, totalCustomer, totalOrder)
VALUES(
(SELECT COUNT(*) FROM products),
(SELECT COUNT(*) FROM customers),
(SELECT COUNT(*) FROM orders)
);
you can changes query accordingly
You can add one more SELECT to achieve this
INSERT INTO table_1 (id_1, id_2)
SELECT
(SELECT id FROM table_2 WHERE code = '01') AS Id_1,
(SELECT id FROM table_2 WHERE code = '02') AS Id_2;
Or you may try with CASE expression:
INSERT INTO table1 (id_1, id_2)
SELECT MAX(CASE WHEN code = '01' THEN id ELSE 0 END) AS Id_1,
MAX(CASE WHEN code = '02' THEN id ELSE 0 END) AS Id_2
FROM table_2
Please refer to the working fiddle on db<>fiddle

outer apply how to using select from joined table

I have a stored procedure in which I cannot add GROUP BY. I need to somehow take the number of records from another table. From the back-end come filter parameters (samples) for one table from it, I want to get data on the number of records.
SELECT cei_mot_count.cnt FROM someTable AS st
LEFT JOIN CEI_Motivations AS cei_mot ON cei_mot.Id= st.ID
--LEFT OUTER JOIN (SELECT Id, StayingID, COUNT(*) as cnt FROM CEI_Motivations GROUP BY Id,StayingID) as cei_mot_count ON cei_mot_count.StayingID = cei_mot.StayingID
--LEFT OUTER JOIN (SELECT Id, COUNT(cei_mot.Id) as cnt FROM cei_mot GROUP BY Id) as cei_mot_count ON cei_mot_count.Id IS NOT NULL
OUTER APPLY (SELECT COUNT(mot.Id) as cnt
FROM cei_mot as mot
WHERE mot.Id IS NOT NULL
GROUP BY mot.Id) as cei_mot_count
i catch Exception: Invalid object name 'cei_mot'
You can't use a table alias from a join inside an apply. I think what you want is:
SELECT cei_mot_count.cnt FROM someTable AS st
LEFT JOIN CEI_Motivations AS cei_mot ON cei_mot.Id= st.ID
--LEFT OUTER JOIN (SELECT Id, StayingID, COUNT(*) as cnt FROM CEI_Motivations GROUP BY Id,StayingID) as cei_mot_count ON cei_mot_count.StayingID = cei_mot.StayingID
--LEFT OUTER JOIN (SELECT Id, COUNT(cei_mot.Id) as cnt FROM cei_mot GROUP BY Id) as cei_mot_count ON cei_mot_count.Id IS NOT NULL
OUTER APPLY (SELECT COUNT(mot.Id) as cnt
FROM CEI_Motivations as mot
WHERE mot.Id IS NOT NULL AND mot.Id= st.ID
GROUP BY mot.Id) as cei_mot_count
Note how this version uses CEI_Motivations twice independently.

PostgreSQL background process raising errors

At about 7:45 this morning, errors started appearing in our customer's PostgreSQL logs. Nobody from my company was working on their system at that time. The error messages have nothing to do with our client's database or the software that we provided. As far as I can tell, the software we provided is operating normally.
Can anyone tell me what might be happening and how to correct it?
This is an ancient PostgreSQL installation: version 8.1 running on Windows Server 2003. We've never updated it because it is still working.
2019-08-08 07:43:23 FATAL: unrecognized configuration parameter "application_name"
2019-08-08 07:43:32 FATAL: unrecognized configuration parameter "application_name"
2019-08-08 07:43:52 FATAL: unrecognized configuration parameter "application_name"
2019-08-08 07:43:52 ERROR: syntax error at or near "has_priviledge" at character 90
2019-08-08 07:43:52 STATEMENT:
SELECT
has_table_privilege(
'pgagent.pga_job', 'INSERT, SELECT, UPDATE'
) has_priviledge
WHERE EXISTS(
SELECT has_schema_privilege('pgagent', 'USAGE')
WHERE EXISTS(
SELECT cl.oid FROM pg_class cl
LEFT JOIN pg_namespace ns ON ns.oid=relnamespace
WHERE relname='pga_job' AND nspname='pgagent'
)
)
2019-08-08 07:43:53 ERROR: column "state" does not exist
2019-08-08 07:43:53 STATEMENT: /*pga4dash*/
SELECT 'session_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT count(*) FROM pg_stat_activity) AS "Total",
(SELECT count(*) FROM pg_stat_activity WHERE state = 'active') AS "Active",
(SELECT count(*) FROM pg_stat_activity WHERE state = 'idle') AS "Idle"
) t
UNION ALL
SELECT 'tps_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(xact_commit) + sum(xact_rollback) FROM pg_stat_database) AS "Transactions",
(SELECT sum(xact_commit) FROM pg_stat_database) AS "Commits",
(SELECT sum(xact_rollback) FROM pg_stat_database) AS "Rollbacks"
) t
UNION ALL
SELECT 'ti_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(tup_inserted) FROM pg_stat_database) AS "Inserts",
(SELECT sum(tup_updated) FROM pg_stat_database) AS "Updates",
(SELECT sum(tup_deleted) FROM pg_stat_database) AS "Deletes"
) t
UNION ALL
SELECT 'to_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(tup_fetched) FROM pg_stat_database) AS "Fetched",
(SELECT sum(tup_returned) FROM pg_stat_database) AS "Returned"
) t
UNION ALL
SELECT 'bio_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(blks_read) FROM pg_stat_database) AS "Reads",
(SELECT sum(blks_hit) FROM pg_stat_database) AS "Hits"
) t

getting distinct rows based on two column values

I am trying to get distinct rows from a temporary table and output them to an aspx page. I am trying to use the value of one column and get the last entry made into that column.
I have been trying to use inner join and max(). However i have been unsuccessful.
Here is the code i have been trying to do it with.
Declare #TempTable table (
viewIcon nvarchar(10),
tenderType nvarchar(20),
diaryIcon int,
customerName nvarchar(100),
projectName nvarchar(100),
diaryEntry nvarchar(max),
diaryDate nvarchar(20),
pid nvarchar(20)
)
insert into #TempTable(
viewIcon,
tenderType,
diaryIcon,
customerName,
projectName,
diaryEntry ,
diaryDate ,
pid
)
select p.viewicon,
p.[Tender Type],
1 diaryicon,
c.[Customer Name],
co.[Last Project],
d.Action,
co.[Diary Date],
p.PID
From Projects2 p Inner Join
(select distinct Pno, max(convert(date,[date of next call],103)) maxdate from ProjectDiary group by Pno
) td on p.PID = td.Pno
Inner Join contacts3 co on co.[Customer Number] = p.[Customer Number]
Inner Join Customers3 c on p.[Customer Number] = c.[Customer Number]
Inner Join ProjectDiary d on td.Pno = d.Pno
Where CONVERT(Date, co.[Diary Date], 103) BETWEEN GETDATE()-120 AND GETDATE()-60
DECLARE #contactsTable TABLE
(pid nvarchar(200),
diaryDate date)
insert into #contactsTable (t.pid, t.diarydate)
select distinct pid as pid, MAX(CONVERT(DATE, diaryDate, 103)) as diaryDate from # TempTable t group by pid
DECLARE #tempContacts TABLE
(pid nvarchar(200))
insert into #tempContacts(pid)
select pid from #contactsTable
DECLARE #tempDiaryDate TABLE (diaryDate date)
insert into #tempDiaryDate(diaryDate)
select distinct MAX(CONVERT(DATE, diaryDate, 103)) from #TempTable
select t.* from #TempTable t inner join (select distinct customerName, M AX(CONVERT(DATE, diaryDate, 103)) AS diaryDate from #TempTable group by customerName) tt on t t.customerName=t.customerName
where t.pid not in
(select Pno from ProjectDiary where convert(date,[Date Of Next Call],103) > GETDATE())
and t.viewIcon <> '098'
and t.viewIcon <> '163'
and t.viewIcon <> '119'
and t.pid in (select distinct pid from #tempContacts)
and CONVERT(DATE, t.diaryDate, 103) in (select distinct CONVERT(DATE, diaryDate, 103) f rom #tempDiaryDate)
order by CONVERT(DATE, tt.diaryDate, 103)
I am trying to get all the distinct customerName's using the max date to determine which record it uses.
Use a subquery. Without going through your entire sql statement, the general idea is:
Select [Stuff]
From table t
Where date = (Select Max(Date) from table
where customer = t.customer)

Postgres "NOT IN" operator usage

In my database when I run the following query, I get 1077 as output.
select count(distinct a_t1) from t1;
Then, when I run this query, I get 459.
select count(distinct a_t1) from t1
where a_t1 in (select a_t1 from t1 join t2 using (a_t1_t2) where a_t2=0);
The above is the same as, this query which also give 459:
select count(distinct a_t1) from t1 join t2 using (a_t1_t2) where a_t2=0
But when I run this query, I get 0 instead of 618 which I was expecting:
select count(distinct a_t1) from t1
where a_t1 not in (select a_t1 from t1 join t2 using (a_t1_t2) where a_t2=0);
I am running PostgreSQL 9.1.5, which really might not be necessary. Please point out my mistake in the above query.
UPDATE 1:
I created a new table and output the result of the subquery above into that one. Then, I ran a few queries:
select count(distinct a_t1) from t1
where a_t1 not in (select a_t1 from sub_query_table order by a_t1 limit 10);
And Hooray! now I get 10 as the answer! I was able to increase the limit until 450. After that, I started getting 0 again.
UPDATE 2:
The sub_query_table has 459 values in it. Finally, this query gives me the required answer:
select count(distinct a_t1) from t1
where a_t1 not in (select a_t1 from sub_query_table order by a_t1 limit 459);
Where as this one, gives 0 as the answer:
select count(distinct a_t1) from t1
where a_t1 not in (select a_t1 from sub_query_table);
But, why is this happening?
The 'NOT IN' operator works only over 'NOT NULL'. Columns with a value of null are not matched.
select count(distinct a_t1) from t1
where a_t1 not in (select a_t1 from t1 join t2 using (a_t1_t2) where a_t2=0) OR a_t1 IS NULL;