Problem in oracle query - oracle

Hai guys,
I've a query in which i need to interchange the values of two fields.
The query is as follows:
SELECT TO_DATE(A.G_LEDGER_DATE,'dd/mm/YYY')as G_LEDGER_DATE,C.ACC_MASTER_NAME,
A.G_LEDGER_REF_NO ,
NVL(CASE WHEN B.G_LEDGER_SECTION = 1 THEN
CASE WHEN
(SELECT COUNT(*)FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_DR_VALUE,
NVL(CASE WHEN B.G_LEDGER_SECTION = -1 THEN
CASE WHEN
(SELECT COUNT(*) FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_CR_VALUE,
B.G_LEDGER_SECTION,C.ACC_MASTER_ID,SUBSTR(A.G_LEDGER_REF_NO,0,3) AS Types,'Z' as OrderChar ,
CASE WHEN A.G_LEDGER_REMARK IS NULL THEN B.G_LEDGER_REMARK ELSE A.G_LEDGER_REMARK END AS Narration
FROM SOSTRANS.ACC_GEN_LEDGER A
LEFT OUTER JOIN SOSTRANS.ACC_GEN_LEDGER B ON A.G_LEDGER_ID = B.G_LEDGER_ID
LEFT OUTER JOIN SOSMASTER.ACC_ACCOUNT_MASTER C ON A.ACC_MASTER_ID = C.ACC_MASTER_ID WHERE A.G_LEDGER_CANCEL='N' AND
B.ACC_MASTER_ID = 'MSOS000001' AND
A.ACC_MASTER_ID <> 'MSOS000001' AND
A.G_LEDGER_SECTION <> B.G_LEDGER_SECTION AND
A.G_LEDGER_DATE >= '25/sep/2009' AND
A.G_LEDGER_DATE<='26/sep/2009'
ORDER BY OrderChar,G_LEDGER_DATE
Now i get the output as
... G_LEDGER_DR_VALUE G_LEDGER_CR_VALUE .....
... 2000 0 .....
... 3000 0 .....
... -1000 0 .....
I need to get the negetive value of the G_LEDGER_DR_VALUE side in G_LEDGER_CR_VALUE and if negetive value exists in G_LEDGER_CR_VALUE then it should be in the G_LEDGER_DR_VALUE field
Can anyone help me to solve this?

If I understood your question well, you select a value (that I will call g_ledger_value) that you want to appear in a different column depending on its sign.
This is how I would do it :
SELECT
CASE WHEN t.g_ledger_value>0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_dr_value,
CASE WHEN t.g_ledger_value<0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_cr_value
FROM
(SELECT g_ledger_value FROM mytable) t;

It sounds like a combination of SIGN() and CASE is what you need ...
CASE WHEN SIGN(G_LEDGER_DR_VALUE) = -1 then ...
ELSE ...
END
etc

SELECT G_LEDGER_DR_VALUE,
CASE WHEN G_LEDGER_DR_VALUE < 0
THEN G_LEDGER_CR_VALUE
ELSE G_LEDGER_DR_VALUE
END
FROM (...)
Is it that you mean? I suggest calculate values of CR___VALUE and DR_VALUE in subquery, and then in wrapping query make CASE which returns you correct value.

Related

Hive sum query needed?

I have data set like below:
PIC_NUMBER|C_DATE|OR_QUANTITY
1|2017-03-01|10
1|2017-03-02|11
1|2017-03-03|12
1|2017-03-04|13
1|2017-03-05|14
1|2017-03-06|15
1|2017-03-07|16
2|2017-03-02|20
2|2017-03-04|13
2|2017-03-05|14
3|2017-03-02|5
3|2017-03-03|6
3|2017-03-05|7
3|2017-03-06|8
3|2017-03-07|9
4|2017-03-01|10
4|2017-03-02|11
4|2017-03-03|12
4|2017-03-04|13
4|2017-03-05|14
4|2017-03-06|15
4|2017-03-07|16
1|2017-03-08|20
1|2017-03-09|21
1|2017-03-10|22
1|2017-03-11|23
1|2017-03-12|24
1|2017-03-13|25
1|2017-03-14|26
2|2017-03-08|30
2|2017-03-09|31
2|2017-03-10|32
2|2017-03-11|33
2|2017-03-12|34
2|2017-03-13|35
2|2017-03-14|36
3|2017-03-08|30
3|2017-03-09|31
3|2017-03-12|34
3|2017-03-14|36
4|2017-03-08|20
4|2017-03-09|21
4|2017-03-10|22
4|2017-03-11|23
4|2017-03-12|24
4|2017-03-13|25
4|2017-03-14|26
And I want to sum OR_QUANTITY in a way that is exclude lesser date OR_QANTITY ,and sum will be for same PIC_NUMBER.
Example result set is:
PIC_NUMBER|C_DATE|SUM_OR_QUANTITY
1|2017-03-01|252
1|2017-03-02|242
1|2017-03-03|231
1|2017-03-04|219
1|2017-03-05|206
1|2017-03-06|192
1|2017-03-07|177
2|2017-03-02|278
2|2017-03-04|258
2|2017-03-05|245
3|2017-03-02|166
3|2017-03-03|161
3|2017-03-05|155
3|2017-03-06|148
3|2017-03-07|140
4|2017-03-01|252
4|2017-03-02|242
4|2017-03-03|231
4|2017-03-04|219
4|2017-03-05|206
4|2017-03-06|192
4|2017-03-07|177
1|2017-03-08|161
1|2017-03-09|141
1|2017-03-10|120
1|2017-03-11|98
1|2017-03-12|75
1|2017-03-13|51
1|2017-03-14|26
2|2017-03-08|231
2|2017-03-09|201
2|2017-03-10|170
2|2017-03-11|138
2|2017-03-12|105
2|2017-03-13|71
2|2017-03-14|36
3|2017-03-08|131
3|2017-03-09|101
3|2017-03-12|70
3|2017-03-14|36
4|2017-03-08|161
4|2017-03-09|141
4|2017-03-10|120
4|2017-03-11|98
4|2017-03-12|75
4|2017-03-13|51
4|2017-03-14|26
Can we write recursive functions in hive for this aggregation ?
This will give the desired result
select PIC_NUMBER, val1 , sum(OR_QUANTITY) from
( select a.PIC_NUMBER,a.C_DATE ,OR_QUANTITY,
case when (a.C_DATE >= temp.C_DATE ) then temp.C_DATE ELSE null END as val1
from table_name a , table_name temp
where temp.PIC_NUMBER = a.PIC_NUMBER ) temp1
where val1 is not null
group by PIC_NUMBER ,val1

Error with sql oracle

i'm using spagobi with oracle DBMS but when i want to get values where year between 2010 and 2014 a got error : right parenthesis missing
select (sum(d.taux_depot *100)/count(r.trimestre) ) as taux , trimestre as trimestre
from datamart_cnss d , ref_temps r
where d.ID_TEMPS = r.ID_TEMPS
and (case when $P{anneecnss}=123 then (r.annee between 2010 and 2014 ) else $P{anneecnss} end) = r.annee
and (case when To_CHAR($P{regimecnss})=123 then To_CHAR(d.id_regime) else To_CHAR($P{regimecnss}) end) = To_CHAR(d.id_regime)
and (case when To_CHAR($P{bureau_cnss})=123 then To_CHAR(d.id_bureau) else To_CHAR($P{bureau_cnss}) end) = To_CHAR(d.id_bureau)
group by trimestre
order by trimestre asc
Thank you
This is not a valid construct:
case when $P{anneecnss}=123 then (r.annee between 2010 and 2014 ) else $P{anneecnss} end
You cannot have a condition inside the then part, just a value or expression that you can then compare with something else.
To apply that filter selectively you don't need to use a case statement, use and and or; I think this is equivalent:
where d.ID_TEMPS = r.ID_TEMPS
and (($P{anneecnss} = 123 and r.annee between 2010 and 2014)
or ($P{anneecnss} != 123 and $P{anneecnss} = r.annee))
and ($P{regimecnss} = 123 or To_CHAR($P{regimecnss}) = To_CHAR(d.id_regime))
and ($P{bureau_cnss} = 123 or To_CHAR($P{bureau_cnss}) = To_CHAR(d.id_bureau))
...

Using IF statement in Oracle SQL

I'm trying to use an IF ELSE statement in a sql query because I want to sum the units per district based on their package size per each PLN #. Can I do this? Here is my attempt, which does not work:
SELECT district_nbr,PLN_NBR, SUM(A.LO_IOH_UNITS) AS TOTAL_UNITS, SUM (A.LO_IOH_EXT_COST_DLRS) AS TOTAL_DOLLARS
FROM FCT_LOIOH_DAY_STR_PLN A, DIM_PROD_PLN B, DIM_LOCATION C
WHERE B.PLN_NBR IN(40000683181, 40000418723, 40000335776)
AND A.PROD_ID = B.PROD_ID
AND A.LOC_ID = C.LOC_ID
if PLN_NBR = '40000683181' then SUM(A.LO_IOH_UNITS)/25
else SUM(A.LO_IOH_UNITS)/60
GROUP BY district_nbr,PLN_NBR
SELECT district_nbr, PLN_NBR,
SUM(A.LO_IOH_UNITS) / (case when PLN_NBR = 40000683181 then 25 else 60 end) AS TOTAL_UNITS,
SUM (A.LO_IOH_EXT_COST_DLRS) AS TOTAL_DOLLARS
FROM FCT_LOIOH_DAY_STR_PLN A,
DIM_PROD_PLN B,
DIM_LOCATION C
WHERE B.PLN_NBR IN (40000683181, 40000418723, 40000335776)
AND A.PROD_ID = B.PROD_ID
AND A.LOC_ID = C.LOC_ID
GROUP BY district_nbr, PLN_NBR;
In Oracle SQL, you can use CASE WHEN ... ELSE for conditionals:
CASE WHEN PLN_NBR = '40000683181' THEN SUM(A.LO_IOH_UNITS) / 25
ELSE SUM)A.LO_IOH_UNITS) / 60
END
Such CASE WHEN ... statements go to the part where the other selected columns go:
SELECT
foo,
bar,
CASE ....
FROM
...

left join do not show all records

In following code left join do not show all the records from left !!!
select *,CASE WHEN (ResDEBIT> ResCREDIT) THEN (ResDEBIT) when (ResCREDIT> ResDEBIT)then (ResCREDIT) else 0 END AS Mande,CASE WHEN (ResDEBIT> ResCREDIT) THEN ('debit') when (ResCREDIT> ResDEBIT)then ('credit') ELSE ('ziro') END AS Status from(SELECT Sales.CustomerInfo.CustomerInfoID,FullTitle=(cast(Sales.CustomerInfo.AccountFK as nvarchar)+' - '+Sales.CustomerInfo.FullName), Sales.CustomerInfo.TopicFK, Sales.CustomerInfo.AccountFK,Sales.CustomerInfo.CompanyRegNo,Sales.CustomerInfo.PersonTypeFK,Sales.CustomerInfo.BankAccountDetail,Sales.CustomerInfo.BankAccountNo, Sales.CustomerInfo.AccountNo, Sales.CustomerInfo.FullName,
Sales.CustomerInfo.Birthdate, Sales.CustomerInfo.TitleFK, Sales.CustomerInfo.RegistrationDate, Sales.CustomerInfo.CustomerPhotoFK, Sales.CustomerInfo.SocialNo,
Sales.CustomerInfo.WebPage, Sales.CustomerInfo.JobFK, Sales.CustomerInfo.MaxDebitLimit, Sales.CustomerInfo.MaxChequeCredit,
Sales.CustomerInfo.PreferedPaymentMethodFK, Sales.CustomerInfo.FirstBalanceKind, Sales.CustomerInfo.FirstBalance, Sales.CustomerInfo.Debit,
Sales.CustomerInfo.Credit, Sales.CustomerInfo.Note, Sales.CustomerInfo.FinancialPeriodFK, Sales.CustomerInfo.CompanyInfoFK,
isnull(SUM(Accounting.DocumentDetail.Debit),0) AS Debit1, isnull(SUM(Accounting.DocumentDetail.Credit),0) AS Credit1, (CASE WHEN (isnull(SUM(Accounting.DocumentDetail.Credit),0)
- isnull(SUM(Accounting.DocumentDetail.Debit),0)) < 0 THEN (isnull(SUM(Accounting.DocumentDetail.Debit),0) - isnull(SUM(Accounting.DocumentDetail.Credit),0)) ELSE 0 END) AS ResDEBIT,
(CASE WHEN (isnull(SUM(Accounting.DocumentDetail.Credit),0) - isnull(SUM(Accounting.DocumentDetail.Debit),0)) > 0 THEN (isnull(SUM(Accounting.DocumentDetail.Credit),0)
- isnull(SUM(Accounting.DocumentDetail.Debit),0)) ELSE 0 END) AS ResCREDIT,Sales.CustomerInfo.BlackListed, Sales.CustomerInfo.IsActive
FROM Sales.CustomerInfo left JOIN
Accounting.DocumentDetail ON Sales.CustomerInfo.AccountFK = Accounting.DocumentDetail.TopicFK
GROUP BY Sales.CustomerInfo.CustomerInfoID, Sales.CustomerInfo.TopicFK, Sales.CustomerInfo.AccountFK, Sales.CustomerInfo.AccountNo,
Sales.CustomerInfo.FullName, Sales.CustomerInfo.Birthdate, Sales.CustomerInfo.TitleFK,Sales.CustomerInfo.CompanyRegNo,Sales.CustomerInfo.PersonTypeFK,Sales.CustomerInfo.BankAccountDetail,Sales.CustomerInfo.BankAccountNo, Sales.CustomerInfo.RegistrationDate,
Sales.CustomerInfo.CustomerPhotoFK, Sales.CustomerInfo.SocialNo, Sales.CustomerInfo.WebPage, Sales.CustomerInfo.JobFK, Sales.CustomerInfo.MaxDebitLimit,
Sales.CustomerInfo.MaxChequeCredit, Sales.CustomerInfo.PreferedPaymentMethodFK, Sales.CustomerInfo.FirstBalanceKind, Sales.CustomerInfo.FirstBalance,
Sales.CustomerInfo.Debit, Sales.CustomerInfo.Credit, Sales.CustomerInfo.Note, Sales.CustomerInfo.FinancialPeriodFK, Sales.CustomerInfo.CompanyInfoFK,
Sales.CustomerInfo.BlackListed, Sales.CustomerInfo.IsActive) CustomerInfo
check how many distinct record exists for your group by columns
Select distinct
Sales.CustomerInfo.CustomerInfoID
,Sales.CustomerInfo.TopicFK
,Sales.CustomerInfo.AccountFK
,Sales.CustomerInfo.AccountNo
,Sales.CustomerInfo.FullName
,Sales.CustomerInfo.Birthdate
,Sales.CustomerInfo.TitleFK
,Sales.CustomerInfo.CompanyRegNo
,Sales.CustomerInfo.PersonTypeFK
,Sales.CustomerInfo.BankAccountDetail
,Sales.CustomerInfo.BankAccountNo
,Sales.CustomerInfo.RegistrationDate
,Sales.CustomerInfo.CustomerPhotoFK
,Sales.CustomerInfo.SocialNo
,Sales.CustomerInfo.WebPage
,Sales.CustomerInfo.JobFK
,Sales.CustomerInfo.MaxDebitLimit
,Sales.CustomerInfo.MaxChequeCredit
,Sales.CustomerInfo.PreferedPaymentMethodFK
,Sales.CustomerInfo.FirstBalanceKind
,Sales.CustomerInfo.FirstBalance
,Sales.CustomerInfo.Debit
,Sales.CustomerInfo.Credit
,Sales.CustomerInfo.Note
,Sales.CustomerInfo.FinancialPeriodFK
,Sales.CustomerInfo.CompanyInfoFK
,Sales.CustomerInfo.BlackListed
,Sales.CustomerInfo.IsActive
from Sales.CustomerInfo
the number of return records for your above query will be same...To get all records from your left table you can use any one of the below 2 methods but i would prefer the 2nd one
1)use sub query
2)first do your aggregation in a separate query and join it with your left table again... the query should be some thing similar to below code:
;WITH CTE ( AccountFK, Debit1 ,Credit1 ,ResDEBIT ,ResCREDIT )
AS (
SELECT
Sales.CustomerInfo.AccountFK
,isnull(SUM(Accounting.DocumentDetail.Debit), 0) AS Debit1
,isnull(SUM(Accounting.DocumentDetail.Credit), 0) AS Credit1
,(
CASE
WHEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0)) < 0
THEN (isnull(SUM(Accounting.DocumentDetail.Debit), 0) - isnull(SUM(Accounting.DocumentDetail.Credit), 0))
ELSE 0
END
) AS ResDEBIT
,(
CASE
WHEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0)) > 0
THEN (isnull(SUM(Accounting.DocumentDetail.Credit), 0) - isnull(SUM(Accounting.DocumentDetail.Debit), 0))
ELSE 0
END
) AS ResCREDIT
FROM Sales.CustomerInfo
LEFT JOIN Accounting.DocumentDetail ON Sales.CustomerInfo.AccountFK = Accounting.DocumentDetail.TopicFK
GROUP BY Sales.CustomerInfo.CustomerInfoID
,Sales.CustomerInfo.TopicFK
,Sales.CustomerInfo.AccountFK
,Sales.CustomerInfo.AccountNo
,Sales.CustomerInfo.FullName
,Sales.CustomerInfo.Birthdate
,Sales.CustomerInfo.TitleFK
,Sales.CustomerInfo.CompanyRegNo
,Sales.CustomerInfo.PersonTypeFK
,Sales.CustomerInfo.BankAccountDetail
,Sales.CustomerInfo.BankAccountNo
,Sales.CustomerInfo.RegistrationDate
,Sales.CustomerInfo.CustomerPhotoFK
,Sales.CustomerInfo.SocialNo
,Sales.CustomerInfo.WebPage
,Sales.CustomerInfo.JobFK
,Sales.CustomerInfo.MaxDebitLimit
,Sales.CustomerInfo.MaxChequeCredit
,Sales.CustomerInfo.PreferedPaymentMethodFK
,Sales.CustomerInfo.FirstBalanceKind
,Sales.CustomerInfo.FirstBalance
,Sales.CustomerInfo.Debit
,Sales.CustomerInfo.Credit
,Sales.CustomerInfo.Note
,Sales.CustomerInfo.FinancialPeriodFK
,Sales.CustomerInfo.CompanyInfoFK
,Sales.CustomerInfo.BlackListed
,Sales.CustomerInfo.IsActive
)
SELECT Sales.CustomerInfo.CustomerInfoID
,FullTitle = (cast(Sales.CustomerInfo.AccountFK AS NVARCHAR) + ' - ' + Sales.CustomerInfo.FullName)
,Sales.CustomerInfo.TopicFK
,Sales.CustomerInfo.AccountFK
,Sales.CustomerInfo.CompanyRegNo
,Sales.CustomerInfo.PersonTypeFK
,Sales.CustomerInfo.BankAccountDetail
,Sales.CustomerInfo.BankAccountNo
,Sales.CustomerInfo.AccountNo
,Sales.CustomerInfo.FullName
,Sales.CustomerInfo.Birthdate
,Sales.CustomerInfo.TitleFK
,Sales.CustomerInfo.RegistrationDate
,Sales.CustomerInfo.CustomerPhotoFK
,Sales.CustomerInfo.SocialNo
,Sales.CustomerInfo.WebPage
,Sales.CustomerInfo.JobFK
,Sales.CustomerInfo.MaxDebitLimit
,Sales.CustomerInfo.MaxChequeCredit
,Sales.CustomerInfo.PreferedPaymentMethodFK
,Sales.CustomerInfo.FirstBalanceKind
,Sales.CustomerInfo.FirstBalance
,Sales.CustomerInfo.Debit
,Sales.CustomerInfo.Credit
,Sales.CustomerInfo.Note
,Sales.CustomerInfo.FinancialPeriodFK
,Sales.CustomerInfo.CompanyInfoFK
,cte.Debit1
,cte.Credit1
,cte.ResDEBIT
,cte.ResCREDIT
,Sales.CustomerInfo.BlackListed
,Sales.CustomerInfo.IsActive
,CASE
WHEN (ResDEBIT > ResCREDIT)
THEN (ResDEBIT)
WHEN (ResCREDIT > ResDEBIT)
THEN (ResCREDIT)
ELSE 0
END AS Mande
,CASE
WHEN (ResDEBIT > ResCREDIT)
THEN ('debit')
WHEN (ResCREDIT > ResDEBIT)
THEN ('credit')
ELSE ('ziro')
END AS STATUS
FROM Sales.CustomerInfo
LEFT JOIN cte ON Sales.CustomerInfo.AccountFK = cte.AccountFK

1 result set from 3 queries?

I'm at my wits end attempting to make this happen. Currently I have 3 separate queries. For automation purposes I need to get the results of these three queries in one output, I cannot seem to join them properly to get the expected results.
QUERY 1:
SELECT OH.EXTN_HOST_ORDER_REF,
OL.EXTN_HOST_ORDER_LINE_REF,
OL.ORIGINAL_ORDERED_QTY,
OL.EXTN_TENDER_QUANTITY,
OL.EXTN_CUM_PICK_QTY,
OL.SHIPPED_QUANTITY,
OL.EXTN_REFUND_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OH.DOCUMENT_TYPE = '0001'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER BY PL.EXTN_HOST_ORDER_LINE_REF ASC;
QUERY 2:
SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '3700.02'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
QUERY 3
SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '9000'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
The query should show NULL values where no data exists from query 2 & 3.
Thanks in advance for your help and advice!
If you are allowed to show both returned and cancelled quantities, the following simple edit should work. Hope this helps.
SELECT oh.extn_host_order_ref,
ol.extn_host_order_line_ref,
ol.original_ordered_qty,
ol.extn_tender_quantity,
ol.extn_cum_pick_qty,
ol.shipped_quantity,
ol.extn_refund_qty,
DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
FROM yfs_order_header oh
INNER JOIN yfs_order_line ol
ON oh.order_header_key = ol.order_header_key
LEFT OUTER JOIN yfs_order_release_status rs
ON ol.order_line_key = rs.order_line_key
WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;

Resources