Transfer Access Code into Oracle Syntax (Power Query) - sql

I am just woorking on to transfer a Access Query into Power Query (Excel).
I think there are some challenges regarding the terms/ wording.
Its a Oracle DB.
The original code looks like this one:
SELECT
POOLK.DATA.DOC,
Val(Min(Right([PNR],5))) AS Value,
POOLK.DATA.RE,
POOLK.DATA.ST INTO [tValue]
FROM
POOLK.DATA
GROUP BY
POOLK.DATA.DOC,
POOLK.DATA.RE,
POOLK.DATA.ST
HAVING (((POOLK.DATA.DOC)>25000) AND ((POOLK.DATA.RE)="B9"))
ORDER BY POOLK.DATA.DOC DESC
This is my approach since now:
SELECT
LTRIM((SUBSTR(POOLK.DATA."PNR", -5)), '0') AS Value,
POOLK.DATA.DOC,
POOLK.DATA.RE,
POOLK.DATA.ST
FROM
POOLK.DATA
WHERE
POOLK.DATA.DOC >25000
AND
POOLK.DATA.RE ='B9'
I get failures with the HAVING and GROUP BY functions.
I think where presents other value, right?
Have someone ideas?
Best Regards

You still need the GROUP BY:
SELECT LTRIM(MIN(SUBSTR(D.PNR, -5)), '0') AS Value,
D.DOC, D.RE, D.ST
FROM POOLK.DATA d
WHERE D.DOC > 25000 AND
D.RE ='B9'
GROUP BY D.DOC, D.RE, D.ST;
I also added table aliases so the code is easier to write and to read.
The INTO is creating a table. If you really want to do that then:
create table tvalue as
SELECT LTRIM(MIN(SUBSTR(D.PNR, -5)), '0') AS Value,
D.DOC, D.RE, D.ST
FROM POOLK.DATA d
WHERE D.DOC > 25000 AND
D.RE ='B9'
GROUP BY D.DOC, D.RE, D.ST;

Related

SQL BigQuery - Error that variable is not grouped by even though it is

SQL Code:
SELECT community_table.community_name,
community_table.id,
DATE(timestamp) as date,
ifnull(COUNT(distinct app_opened.user_id), 0) as num_opened_DAU,
lag(COUNT(distinct app_opened.user_id)) OVER
(ORDER BY community_table.community_name, community_table.id, DATE(timestamp)) as pre_Value
FROM *** app_opened
LEFT JOIN (
SELECT DISTINCT id, community_id_2, context_traits_first_name, context_traits_last_name
FROM (
SELECT *
FROM ***,
UNNEST (JSON_EXTRACT_ARRAY(context_traits_community_ids, "$")) as community_id_2
)
GROUP by community_id_2, id, context_traits_first_name, context_traits_last_name) as community_id_table
ON community_id_table.id = app_opened.user_id
LEFT JOIN (
SELECT DISTINCT id, name as community_name
FROM ***) as community_table
ON TO_JSON_STRING(community_table.id) = community_id_table.community_id_2
WHERE app_opened.user_id is not null AND
EXTRACT(DAYOFWEEK FROM DATE(timestamp)) = 2 AND
community_table.community_name is not null
GROUP BY community_table.community_name, community_table.id, DATE(timestamp)
Error Message:
I am quite confused on what could be going wrong here, as the error says that timestamp is not grouped, even though I have grouped it at the bottom. I tried including just timestamp rather than Date(timestamp), but that ruins the table data that I am trying to create, where I find the number of users on a single day. Does anyone have any other ideas? My goal is for a single row, get the previous row's data, but because I am grouping by specific metrics, I need to make sure they are ordered by them as well. Thank you so much!
I think you simply need to modify OVER part as:
OVER (PARTITION BY community_table.community_name, community_table.id, DATE(timestamp)) as pre_Value
UPDATE. Seems that the problem was caused by using DATE() function within OVER so it can be solved by using DATE(timestamp) inside of subquery and passing alias to OVER

How do i divide by an Alias?

When I run the follwing query I get a ORA- 00937 error.
select apex_utilities.get_invtype(o_code)
,count(c.id) as "Case Count"
,round(sum(billable_time/3600),2) as "Time(hrs)"
,round(sum(billable_time/count(c.id)),2) as "AHT(secs)"
from myTable c
where c.date_created between :p31_period_start and add_months(:p31_period_start,1)
group by rollup(apex_utilities.get_invtype(o_code));
If I comment out the line
,round(sum(billable_time/count(c.id)),2) as "AHT(secs)"
it runs as expected, without this column appearing.
How do i get it to run including this line? If I add billable_time to the group by then the output is incorrect as I get many rows as it groups all the billable_time as well as the invtype. In addition i have to remove the 'SUM' from the line to get it to run.
The output I am looking for is similar to the following
invtype CaseCount Time(Hrs) AHT(secs)
CDCW 1234 308:53:45 909.56
CBCB 100 24:56:34 109.24
It seems like what you want is actually:
,round(sum(billable_time)/count(c.id),2) as "AHT(secs)"`
I suspect it's balking because you are aggregating inside an aggregation. If you truly mean to do this, then you'll probably need a subquery to get that count(c.id) so you can apply it inside the aggregation of SUM(billable_time, count_of_c_id) bit.
You'd need to move that count out into a subquery. I've done the same with the sum here for clarity:
with cteTable as
(
select apex_utilities.get_invtype(o_code) invtype
,count(c.id) casecount
,sum(billable_time) totaltime
from myTable c
where c.date_created between :p31_period_start and add_months(:p31_period_start,1)
group by rollup(apex_utilities.get_invtype(o_code))
)
select invtype
,casecount "Case Count"
,round(totaltime/3600,2) "Time(hrs)"
,round(totaltime/casecount,2) "AHT(secs)"
from cteTable;

Pulling through subquery calculations

I having a bit of an issue with a subquery and was hoping to get some input. I'm trying to aggregate data from two tables, I've gotten aggregation to work great from Financial Table (in isolation), but I'm having an issue on the claims piece. It's turning everything Cartesian, so I tried to write the claims calculations in the second subquery, but the main query has problems referencing the fields states c.CLM_LN_PD_AMT does not exist. This is the part before I moved the calculations into the second subquery. (sum CLM_LN_PD in subquery and then call c.clm_ln_pd in main query. Any nudge would be awesome.
select sum(f.MED_ELIGTY_IND) as MEDMEMBERMONTHS,
sum(f.PHARM_ELIGTY_IND) as RXMONTHS,
avg(f.DIAG_CD_RSK_SCORE) as MEDRISK,
avg(f.DIAG_CD_PHARM_RSK_SCORE) as PHARMRISK,
count(distinct c.CLM_ID_TXT) as NUMCLAIMS,
sum(c.CLM_LN_PD_AMT) as PAIDAMT,
sum(c.CLM_LN_ALWD_AMT) as ALWDAMT,
f.ELIGTY_MO_YYYYMM,
f.SE_NAME
from (select CTG_ID_TXT,
MED_ELIGTY_IND,
PHARM_ELIGTY_IND,
DIAG_CD_RSK_SCORE,
DIAG_CD_PHARM_RSK_SCORE,
ELIGTY_MO_YYYYMM,
SE_NAME
from FINCL_ATTRBN_RESLT_PMPM
Where to_char(ELIGTY_AS_OF_DT, 'mm/dd/yyyy') = '02/20/2014'
and ELIGTY_MO_YYYYMM between '201301' and '201312'
and CNTRCT_TYP_ID = '331929495'
and ACTRL_LOB not in ('BOEI', 'SC_ASO')) f,
(select distinct (CTG_ID), CLM_ID_TXT, CLM_LN_PD_AMT, CLM_LN_ALWD_AMT
from CLM_MED_DETL_VW
where to_char(CLM_LN_SVC_FROM_DT, 'yyyymm') between '201301' and
'201312'
and CLM_MED_DETL_VW.PD_PRD_YYYYMM between '201301' and '201312'
and CLM_MED_DETL_VW.CLM_ADJ_PRD_YYYYMM is null) c
where c.CTG_ID = f.CTG_ID_TXT
Group by f.SE_NAME, f.ELIGTY_MO_YYYYMM
order by f.SE_NAME, f.ELIGTY_MO_YYYYMM

How to Avoid Duplicate ID's In Access SQL

I have a problem that I hope you can help me.
I have the next query on Access SQL.
SELECT
ID_PLAN_ACCION, ID_SEGUIMIENTO,
Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM
SEGUIMIENTOS
WHERE
(((ID_PLAN_ACCION) = [CODPA]))
GROUP BY
ID_PLAN_ACCION, ID_SEGUIMIENTO;
And it returns this information:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
-----------------------------------------------
A1-01 1 16/01/2014
A1-01 2 30/01/2014
But I really need that it throws off this:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
----------------------------------------------
A1-01 2 30/01/2014
As you can see I only need the record that has the most recently date, not all the records
The GROUP BY doesn't work.
Please can you tell me what can I do? I am new on all this.
Thank you so much!!
PD: Sorry for my english, I'm learning
This will produce the results you want:
SELECT ID_PLAN_ACCION, max(ID_SEGUIMIENTO) as ID_SEGUIMIENTO, Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
GROUP BY ID_PLAN_ACCION;
I removed id_sequiimiento from the group by and added an aggregation function to get the max value. If the ids increase along with the date, this will work.
Another way to approach this query, though, is to use top and order by:
SELECT top 1 ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
ORDER BY FECHA_SEGUIMIENTO desc;
This works because you are only returning one row.
EDIT:
If you have more codes, that you are looking at, you need a more complicated query. Here is an approach using where/not exists:
SELECT ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS s
WHERE not exists (select 1
from SEGUIMIENTOS s2
where s.ID_PLAN_ACCION = s2.ID_PLAN_ACCION and
s2.FECHA_SEGUIMIENTO > s.FECHA_SEGUIMIENTO
)
ORDER BY FECHA_SEGUIMIENTO desc;
You can read this as: "Get me all rows from SEGUIMIENTOS where there is no other row with the same ID_PLAN_ACCION that has a larger date". Is is another way of saying that the original row has the maximum date.

SQL "Count (Distinct...)" returns 1 less than actual data shows?

I have some data that doesn't appear to be counting correctly. When I look at the raw data I see 5 distinct values in a given column, but when I run an "Count (Distinct ColA)" it reports 4. This is true for all of the categories I am grouping by, too, not just one. E.g. a 2nd value in the column reports 2 when there are 3, a 3rd value reports 1 when there are 2, etc.
Table A: ID, Type
Table B: ID_FK, WorkID, Date
Here is my query that summarizes:
SELECT COUNT (DISTINCT B.ID_FK), A.Type
FROM A INNER JOIN B ON B.ID_FK = A.ID
WHERE Date > 5/1/2013 and Date < 5/2/2013
GROUP BY Type
ORDER BY Type
And a snippet of the results:
4|Business
2|Design
2|Developer
Here is a sample of my data, non-summarized. Pipe is the separator; I just removed the 'COUNT...' and 'GROUP BY...' parts of the query above to get this:
4507|Business
4515|Business
7882|Business
7889|Business
7889|Business
8004|Business
4761|Design
5594|Design
5594|Design
5594|Design
7736|Design
7736|Design
7736|Design
3132|Developer
3132|Developer
3132|Developer
4826|Developer
5403|Developer
As you can see from the data, Business should be 5, not 4, etc. At least that is what my eyes tell me. :)
I am running this inside a FileMaker 12 solution using it's internal ExecuteSQL call. Don't be concerned by that too much, though: the code should be the same as nearly anything else. :)
Any help would be appreciated.
Thanks,
J
Try using a subquery:
SELECT COUNT(*), Type
FROM (SELECT DISTINCT B.ID_FK, A.Type Type
FROM A
INNER JOIN B ON B.ID_FK = A.ID
WHERE Date > 5/1/2013 and Date < 5/2/2013) x
GROUP BY Type
ORDER BY Type
This could be a FileMaker issue, have you seen this post on the FileMaker forum? It describes the same issue (a count distinct smaller by 1) with 11V3 back in 03/2012 with a plug in, then updated with same issue with 12v3 in 11/2012 with ExecuteSQL. It didn't seem to be resolved in either case.
Other considerations might be if there are any referential integrity constraints on the joined tables, or if you can get a query execution plan, you might find it is executing the query differently than expected. not sure if FileMaker can do this.
I like Barmar's suggestion, it would sort twice.
If you are dealing with a bug, directing the COUNT DISTINCT, Join and/or Group By by structuring the query to make them happen at different times might work around it:
SELECT COUNT (DISTINCT x.ID), x.Type
FROM (SELECT A.ID ID, A.Type Type
FROM A
INNER JOIN B ON B.ID_FK = A.ID
WHERE B.Date > 5/1/2013 and B.Date < 5/2/2013) x
GROUP BY Type
ORDER BY Type
you might also try replacing B.ID_FK with A.ID, who knows what context it applies, such as:
SELECT COUNT (DISTINCT A.ID), A.Type