SQL - Condition which Matches two rows - sql

I am trying to extract the data from SQL developer which matches two row conditions. Both the rows has one unique value ( ID ) and table name is abc.tcd
ID = Type = GL code = amount
1 = Debit = 0701 = 10000
1 = credit = 0601 = 10000
1 = Credit= 0501 = 1000
1 = Debit= 0401 = 1000
2 = Debit = 0701 = 9000
2 = credit = 0801 = 9000
3 = Debit = 0701 = 6000
3 = credit = 0601 = 6000
Condition 1 :
GL code = '0701' having Type = 'Debit'
condition 2 :
GL code = '0601' having Type = 'Credit'
Expected output :
ID Type GL code amount
1 = Debit = 0701 = 10000
1 = credit = 0601 = 10000
1 = Credit = 0501 = 1000
1 = Debit = 0401 = 1000
3 = Debit = 0701 = 6000
3 = credit = 0601 = 6000
Output should display all the rows based on ID

If I understand the question correctly, you want to extract all the rows for some ID where two different rows fulfill two different conditions. you could use a couple of in operators:
SELECT *
FROM mytable
WHERE id IN (SELECT id
FROM my_table
WHERE GLCode = '0701' AND Type = 'Debit')
AND id IN (SELECT id
FROM my_table
WHERE GLCode = '0601' AND Type = 'Credit')
Of course, this can be easily translated to use the exists operator:
SELECT *
FROM mytable a
WHERE EXISTS (SELECT *
FROM my_table b
WHERE a.id = b.id AND b.GLCode = '0701' AND b.Type = 'Debit')
AND EXISTS (SELECT *
FROM my_table c
WHERE a.id = c.id AND c.GLCode = '0601' AND c.Type = 'Credit')
A more elegant way might be to have all the conditions in a single query with ors and count how many of them are fulfilled:
SELECT *
FROM mytable
WHERE id IN (SELECT id
FROM my_table
WHERE (GLCode = '0701' AND Type = 'Debit') OR
(GLCode = '0601' AND Type = 'Credit')
GROUP BY id
HAVING COUNT(*) = 2)

Another alternative:
SELECT * FROM MyTable
INNER JOIN
(
SELECT ID
FROM MyTable
WHERE (GLCode = '0701' AND Type = 'Debit') OR (GLCode = '0601' AND Type = 'Credit')
GROUP BY ID
HAVING COUNT(DISTINCT GLCode) = 2 AND COUNT(DISTINCT Type) = 2
) X
ON MyTable.ID = x.ID;
SqlFiddle here
Basically "find the ids having two distinct rows meeting the criteria". We then return all rows with this ID
Edit
Your real query would look like:
SELECT *
FROM tbaadm.ctd
INNER JOIN
(SELECT Tran_id
FROM tbaadm.ctd
WHERE ((GL_SUB_HEAD_CODE = '06106' AND PART_TRAN_TYPE = 'C')
OR (GL_SUB_HEAD_CODE = '29101' AND PART_TRAN_TYPE = 'D'))
AND (tran_date >= '01-12-2014' AND tran_date < '30-12-2014')
GROUP BY Tran_id
HAVING COUNT(DISTINCT GL_SUB_HEAD_CODE) = 2 AND COUNT(DISTINCT PART_TRAN_TYPE) = 2
) X
ON tbaadm.ctd = x.Tran_id;
The parenthesis around the ANDs are obviously redundant due to operator precedence, but might help with readability?
Also, note with Date range checking that it is convention to include start date but exclude end date, viz x >= start and x < end

Related

Pl sql get x character of a string in a query

SELECT MR.MUSTERI_ROL_AD AS ACENTE_AD , ////HERE\\\\
(SELECT mrrt.musteri_rol_ad
FROM calisan ct, musteri_rol mrrt, musteri mmt
where ct.bagli_rol_id = a.acente_id
AND ct.calisan_rol_id = mrrt.musteri_rol_id
AND mrrt.musteri_id = mmt.musteri_id
AND ct.teknik_personel='H'
AND ct.calisan_rol_id is not null
and rownum < 2)Bayi_Yetkili_Kisi,
( case when M.TCK_NO is null then M.VKN_NO
else null end)VKN_NO,
(Select (SELECT taa.adresi
FROM tobb_acente ta, tobb_acente_adres taa
WHERE ta.levhano = a.levha_no
AND ta.tobb_acente_sorgu_id = taa.tobb_acente_sorgu_id
AND rownum < 2)
FROM musteri_adres mat, musteri_rol mrt, musteri mt, adres ad
WHERE mrt.musteri_rol_id = a.acente_id
AND mt.musteri_id = mrt.musteri_id
AND mat.musteri_id(+) = mt.musteri_id
AND ad.adres_id(+) = mat.adres_id AND rownum < 2) adress,
(SELECT mi.aciklama
FROM musteri_iletisim mi, musteri_rol_iletisim mri
where mi.musteri_id = m.musteri_id
AND mi.iletisim_tip_kod =
pck_const_iletisim_tip.cep_telefon()
AND mri.musteri_rol_id = mr.musteri_rol_id
and mi.musteri_iletisim_id = mri.musteri_iletisim_id
and rownum < 2) as CEpTELEFON ////// HERE \\\\\
FROM MUSTERI_ROL MR, MUSTERI M, ACENTE A,SATIS_KANALI SK
WHERE MR.MUSTERI_ID = M.MUSTERI_ID
AND MR.MUSTERI_ROL_ID = A.ACENTE_ID
AND A.SATIS_KANALI_ID = SK.SATIS_KANALI_ID
AND MR.ROL_ID = 1
AND A.Uretim_Kaynagi = 'E'
AND A.UST_ACENTE_ID is null
AND MR.Ust_Musteri_Rol_Id is null
AND M.VKN_NO != 'x'
AND TO_CHAR(TRUNC(MR.Bitis_Tarih), 'DD/MM/YYYY') is null or MR.Bitis_Tarih = SYSDATE
Hi guys i need some help about split in Oracle's SQL i get ACENTE_AD x character and i wanna see only first 5 character. and i get CEpTELEFON like 012345678 and i wanna split first 3 character(012) in a column and last 6 character(345678) the in other column. thanks.
You can use substr() string manipulation function to split your phone value in the outer select statement by considering your original query as a subquery.
SELECT t.Bayi_Yetkili_Kisi, t.Vkn_No, t.Adres,
substr(CepTelefonu,1,3) as AlanKodu, substr(CepTelefonu,-6) as CepTelefonu
FROM
(
SELECT MR.MUSTERI_ROL_AD AS ACENTE_AD,
(SELECT mrrt.musteri_rol_ad
FROM musteri_rol mrrt
JOIN calisan ct ON ct.calisan_rol_id = mrrt.musteri_rol_id
JOIN musteri mmt ON mmt.musteri_id = mrrt.musteri_id
WHERE ct.bagli_rol_id = a.acente_id
AND ct.teknik_personel = 'H'
AND ct.calisan_rol_id is not null
AND rownum < 2) as Bayi_Yetkili_Kisi,
(CASE
WHEN M.TCK_NO is null THEN
M.VKN_NO
ELSE
null
END) as Vkn_No,
(SELECT (SELECT taa.adresi
FROM tobb_acente ta
JOIN tobb_acente_adres taa
ON taa.tobb_acente_sorgu_id = ta.tobb_acente_sorgu_id
WHERE ta.levhano = a.levha_no
AND rownum < 2)
FROM musteri mt
JOIN musteri_rol mrt ON mrt.musteri_id = mt.musteri_id
LEFT JOIN musteri_adres mat ON mat.musteri_id = mt.musteri_id
LEFT JOIN adres ad ON ad.adres_id = mat.adres_id
WHERE mrt.musteri_rol_id = a.acente_id
AND rownum < 2) as Adres,
(SELECT mi.aciklama
FROM musteri_iletisim mi
JOIN musteri_rol_iletisim mri ON mri.musteri_iletisim_id = mi.musteri_iletisim_id
WHERE mi.musteri_id = m.musteri_id
AND mi.iletisim_tip_kod = pck_const_iletisim_tip.cep_telefon() -- I think the right hand side implies a stored function, doesn't it?
AND mri.musteri_rol_id = mr.musteri_rol_id
AND rownum < 2) as CepTelefonu
FROM MUSTERI_ROL MR
JOIN MUSTERI M ON M.MUSTERI_ID = MR.MUSTERI_ID
JOIN ACENTE A ON A.ACENTE_ID = MR.MUSTERI_ROL_ID
JOIN SATIS_KANALI SK ON SK.SATIS_KANALI_ID = A.SATIS_KANALI_ID
WHERE MR.ROL_ID = 1
AND A.Uretim_Kaynagi = 'E'
AND A.UST_ACENTE_ID is null
AND MR.Ust_Musteri_Rol_Id is null
AND M.VKN_NO != 'x'
AND to_char(trunc(MR.Bitis_Tarih), 'dd/mm/yyyy') is null
OR MR.Bitis_Tarih = sysdate
) t;
Btw, Consider using ANSI-92 style SQL rather than former ANSI-89 with comma-seperated tables written like the above one. It's easier to write, understand and maintain.

Get the sum of a count column in SQL

I have the following query
SELECT
dtc.coupon_type_company_name,
COUNT(*) * dtc.coupon_type_company_coupon_amount AS 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM
[dbo].[coupon_type_Company_User] dtcu
JOIN
coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN
person p ON dtcu.userID = p.userID
WHERE
coupon_type_company_coupon_is_combinable = 1
OR coupon_type_company_has_coupon = 1
AND dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
This returns the following:
What I want to have however is just one column and one row that should take the SUM of my middle column (count(*)*dtc.coupon_type_company_coupon_amount).
How could I achieve this and prevent doing this in my code backend (C#)
You can wrap your query like this:
SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
your query
) s
Use a "Table Expression", as in:
select sum(Total_Coupon_To_Be_Used) from (
SELECT dtc.coupon_type_company_name,
count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM [dbo].[coupon_type_Company_User] dtcu
JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN person p ON dtcu.userID = p.userID
WHERE coupon_type_company_coupon_is_combinable = 1
or coupon_type_company_has_coupon = 1
and dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
) x

Not able to optimize a SQL Query. Taking huge cost. Oracle Apps

This is a query which I have to use as part of extracting Receipts based on certain conditions from Oracle 11i as a part of Conversion.
--> One important check is to check for invoices on hold. I just need to pull the details wherein the invoices are on hold. And the condition for checking is
In AP_HOLDS_ALL table -- 1) the invoice should exist
2) release lookup code should be NULL
3) status flag should be S or NULL and
4) there should not exist any line in AP_HOLDS_ALL table with same invoice ID with release lookup code as not null.
I have tried and added the below criteria in last of the following query, but it fetches the rows very very slow (100 000 records in a day).
How to improve the performance?
select * -- multiple Columns
from rcv_shipment_headers rsh,
rcv_shipment_lines rsl,
rcv_transactions rt,
hr_operating_units hou,
po_headers_all poh,
po_lines_all pol,
po_line_locations_all pll,
po_distributions_all pda,
po_vendors pv,
po_vendor_sites_all pvs
where 1 = 1
and rsh.shipment_header_id = rsl.shipment_header_id
and rsh.shipment_header_id = rt.shipment_header_id
and rsl.shipment_line_id = rt.shipment_line_id
and rt.po_header_id = poh.po_header_id
and rt.po_line_id = pol.po_line_id
and rt.po_line_location_id = pll.line_location_id
and rt.po_distribution_id = pda.po_distribution_id
and poh.po_header_id = pol.po_header_id
and pol.po_line_id = pll.po_line_id
and pll.line_location_id = pda.line_location_id
and poh.org_id = hou.organization_id
and poh.type_lookup_code = 'STANDARD'
and poh.authorization_status = 'APPROVED'
and poh.approved_flag = 'Y'
and rsh.ship_to_org_id = pll.ship_to_organization_id
and rt.organization_id = pll.ship_to_organization_id
and pol.org_id = hou.organization_id
and pll.org_id = hou.organization_id
and pda.org_id = hou.organization_id
and hou.date_to is null
and (rt.transaction_type = 'DELIVER' or rt.transaction_type = 'RECEIVE')
and rt.vendor_site_id = pvs.vendor_site_id
and rt.vendor_id = pv.vendor_id
and pv.vendor_id = pvs.vendor_id
and (nvl(pda.quantity_ordered, 0) - nvl(pda.quantity_cancelled, 0)) > 0
and nvl(pda.quantity_delivered, 0) > 0
and nvl(pda.quantity_billed, 0) > 0
and nvl(rsl.quantity_received, 0) > 0
and ((nvl(pda.quantity_delivered, 0) = nvl(pda.quantity_billed, 0)) or
(nvl(pda.quantity_delivered, 0) > nvl(pda.quantity_billed, 0)) or
(nvl(pda.quantity_delivered, 0) < nvl(pda.quantity_billed, 0)))
and exists
(select aida.po_distribution_id
from ap_invoices_all aia, ap_invoice_distributions_all aida
where aia.cancelled_date is null
and aida.po_distribution_id = pda.po_distribution_id
and exists
(select c.invoice_id
from ap_holds_all c
where c.release_lookup_code is null
and c.invoice_id = aia.invoice_id
and c.org_id = nvl(p_leg_operating_unit, c.org_id)
and (c.status_flag = 'S' or c.status_flag is null)
and not exists
(select 1
from ap_holds_all d
where d.invoice_id = c.invoice_id
and d.org_id = nvl(p_leg_operating_unit, d.org_id)
and d.release_lookup_code is not null))
and aia.org_id = nvl(p_leg_operating_unit, aia.org_id)
and aia.invoice_id = aida.invoice_id)
and poh.org_id = nvl(p_leg_operating_unit, poh.org_id)
p_leg_operating_unit is a parameter which is having NULL value as I am trying to get the values for all the OU's
** last exists is causing the issue.
Try with the following , may solve your issue :
/*+ parallel(4) */ --hint. If doesnt work , then try with
parallel(8)/parallel(16).
Reorder the joins . Join the tables based on the least number of rows returned. FOr example Table A,B,C . Join a & b returns 100
rows, but B&c returns 20 rows. then First join B & C table and join
with A.
Use /*+ Index * hint if optimizer doesn't access indexes.

SQL Load Fact with Merge Statement Slow

I'm loading a fact table using MERGE and it's taking 44 minutes for 1 million records. Is there a way I can improve this?
My query:
MERGE INTO dbo.FactProjMarketCode AS target
USING (
SELECT
SourceSystem = 'ORACLE_ERP',
ProjMarketCodeSourceId = jmc.XT_JOB_MARKETING_CODE_ID,
p.ProjectId,
MarketCodeId = ISNULL(mc.MarketingCodeId,-1),
Weight = 0.01 * ISNULL(jmc.WEIGHT,100),
WasMissing = CASE WHEN jmc.WEIGHT IS NULL THEN 1 ELSE 0 END,
jmc.LAST_UPDATE_DATE
FROM
dimProject p
LEFT JOIN stage.XT_JOB_MARKETING_CODE jmc
ON p.ProjectSourceId = jmc.XT_PROJECT_ID
LEFT JOIN DimMarketingCode mc
ON jmc.MARKETING_CODE = mc.MarketingCode -- should create column MarketingCodeSourceId
AND mc.SourceSystem = 'ORACLE_ERP'
WHERE
p.SourceSystem = 'ORACLE_ERP'
) AS source
ON target.SourceSystem = source.SourceSystem
AND target.ProjMarketCodeSourceId = source.ProjMarketCodeSourceId
WHEN MATCHED AND source.LAST_UPDATE_DATE > target.LAST_UPDATE_DATE
THEN UPDATE SET
target.ProjectId = source.ProjectId,
target.MarketCodeId = source.MarketCodeId,
target.Weight = source.WEIGHT,
target.WasMissing = source.WasMissing,
target.LAST_UPDATE_DATE = source.LAST_UPDATE_DATE
WHEN NOT MATCHED BY target
THEN INSERT (
SourceSystem,
ProjMarketCodeSourceId,
ProjectId,
MarketCodeId,
Weight,
WasMissing,
LAST_UPDATE_DATE
) VALUES (
source.SourceSystem,
source.ProjMarketCodeSourceId,
source.ProjectId,
source.MarketCodeId,
source.Weight,
source.WasMissing,
source.LAST_UPDATE_DATE
)
WHEN NOT MATCHED BY source
THEN DELETE;

TSQL - TOP and COUNT in one SELECT

i try to combine these two statements to one, but all my tries failed!
Is it possible to merge them?
-- Is there a open answer?
SELECT CASE COUNT(tbl_Communication.pk_Communication) WHEN 0
THEN 0 ELSE 1 END AS hasAnsweredCom
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
-- Get the answer text
SELECT TOP 1 tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
ORDER BY tbl_Communication.pk_Communication
Right join trick.
SELECT TOP 1
CASE WHEN tbl_CommunicationElements.pk_Communication IS NULL THEN 0 ELSE 1 END hasAnsweredCom
, tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
RIGHT JOIN (VALUES(1)) AS Ext(x) ON (
tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
)
If you are willing to put the two results on one line, the following works:
select (CASE count(*) WHEN 0 THEN 0 ELSE 1 END) AS hasAnsweredCom,
MAX(case when seqnum = 1 then subject end) as hasAnsweredComStepName
from (SELECT tbl_Communication.pk_Communication, tbl_Communication.subject,
ROW_NUMBER() over (order by pk_communication) as seqnum
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
) t
The second value will be NULL if there are no answers.
As for returning two rows. My guess is that subject is a string whereas hasAnsweredCom is an integer. The types conflict, so any sort of union or bringing the results together will probably result in a type conflict on the second row.