SQL query that joins results created by a query - oracle-sqldeveloper

I have two tables:
TWMWR and TWMWRemark. I have two competing criteria so I am trying to run the query separately and then join them. I am trying to find all the records in TWMWR that meet the following criteria:
PSWMS.TWMREMARK.TXT_REMARK LIKE '%Essential Equipment: N%'
AND PSWMS.TWMREMARK.TP_REMARK = '%CIS%'
then I want to take the list of WR (the records) and join that with another table to find the records that meet the following criteria:
PSWMS.TWMREMARK.ID_OPER = 'IXM0Q74'
AND PSWMS.TWMREMARK.TP_REMARK = 'CLM'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%SOC%'
They have to be done separately. Can anyone help? I have pasted what I have tried below. I am using mySql developer.
SELECT PSWMS.TWMWR.CD_WR,
PSWMS.TWMWR.TP_JOB,
PSWMS.TWMWR.TS_OPER_RECORDED,
PSWMS.TWMWR.ID_CUSTOMER,
PSWMS.TWMREMARK.TS_REMARK,
PSWMS.TWMREMARK.ID_OPER,
PSWMS.TWMREMARK.TP_REMARK AS TP_REMARK1,
PSWMS.TWMREMARK.TXT_REMARK
FROM PSWMS.TWMWR
INNER JOIN PSWMS.TWMREMARK
ON PSWMS.TWMWR.CD_WR = PSWMS.TWMREMARK.CD_WR
WHERE PSWMS.TWMWR.TP_JOB = 'CLMDPD'
AND PSWMS.TWMWR.TS_OPER_RECORDED >= '21, Oct 2013'
AND PSWMS.TWMREMARK.ID_OPER = 'IXM0Q74'
AND PSWMS.TWMREMARK.TP_REMARK = 'CLM'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%SOC%'
GROUP BY PSWMS.TWMWR.CD_WR,
PSWMS.TWMWR.TP_JOB,
PSWMS.TWMWR.TS_OPER_RECORDED,
PSWMS.TWMWR.ID_CUSTOMER,
PSWMS.TWMREMARK.TS_REMARK,
PSWMS.TWMREMARK.ID_OPER,
PSWMS.TWMREMARK.TP_REMARK,
PSWMS.TWMREMARK.TXT_REMARK
INNER JOIN
SELECT PSWMS.TWMWR.CD_WR
FROM PSWMS.TWMWR
INNER JOIN PSWMS.TWMREMARK
ON PSWMS.TWMWR.CD_WR = PSWMS.TWMREMARK.CD_WR
WHERE PSWMS.TWMWR.TP_JOB = 'CLMDPD'
AND PSWMS.TWMWR.TS_OPER_RECORDED >= '21, Oct 2013'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%Essential Equipment: N%'
AND PSWMS.TWMREMARK.TP_REMARK = '%CIS%' AS WR
ON PSWMS.TWMWR.CD_WR = WR.CD_WR, PSWMS.TWMREMARK.CD_WR = WR.CD_WR

Related

Why Group By is not working as expected in PostgreSQL?

I m doing a query and it is showing me the output but not as expected.
Above image it is giving me two extra row (blue indicator) after doing group by which not should be exists in the output. Here is my query
SELECT som.customer_po,
pro.product_id,
pro.product_name,
som.mo_id,
ri.status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id,
product_name,
som.mo_id,
ri.status
I tried different way but it is giving me the same result.
It solves my problem somehow. but i don't know it is best practice or not
SELECT max(som.customer_po) AS customer_po,
max(pro.product_id) AS product_id,
max(pro.product_name) product_name,
max(som.mo_id) mo_id,
max(ri.status) AS risk_status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id

Implementing Concat + RANK OVER SQL Clause in C# LINQ

I need to implement the following T-SQL clause ....
SELECT
CONCAT( RANK() OVER (ORDER BY [Order].codOrder, [PackedOrder].codPackedProduct ), '/2') as Item,
[Order].codOrder as [OF],
[PackedOrder].codLine as [Ligne],
[PackedOrder].codPackedProduct as [Material], ----------------------
[Product].lblPProduct as [Product],
[PackedProduct].lblPackedProduct as [MaterialDescription],
[PackedOrder].codPackedBatch as [Lot],
[Product].codCustomerColor as [ReferenceClient],
[PackedOrder].nbrPackedQuantity as [Quantity],
[PackedOrder].nbrLabelToPrint as [DejaImprime]
FROM [Order] INNER JOIN PackedOrder
ON [Order].codOrder = PackedOrder.codOrder INNER JOIN Product
ON [Order].codProduct = Product.codProduct INNER JOIN PackedProduct
ON PackedOrder.codPackedProduct = PackedProduct.codPackedProduct
Where [Order].codOrder = 708243075
So Far, I'm able to do:
var result =
from order1 in Orders
join packedorder1 in PackedOrders on order1.codOrder equals packedorder1.codOrder
join product1 in Products on order1.codProduct equals product1.codProduct
join packedproduct1 in PackedProducts on packedorder1.codPackedProduct equals packedproduct1.codPackedProduct
where order1.codOrder == _order.codOrder
select new FinishedProductPrintingM
{
OF = order1.codOrder,
Ligne = packedorder1.codLine,
Material = packedorder1.codPackedProduct,
Produit = product1.codProductType,
MaterialDescription = packedproduct1.lblPackedProduct,
Lot = packedorder1.codPackedBatch,
RéférenceClient = product1.codCustomerColor,
Quantité = packedorder1.nbrPackedQuantity,
Déjàimprimé = packedorder1.nbrLabelPrinted
};
Please let me know if its possible or not. I need to display the Items in such a way.Please feel free to add your valuable comments.
I am not aware how to use concat and Rank over function in LINQ.
Can anyone help me to convert my SQL query into LINQ?

Pass order by clause from Java in Jasper Report

I am using latest version of Jasper soft studio to design Jasper reports. I want to pass order by clause to jasper report based on certain criteria from Java. Can you please guide as how to append parameter(containing order by clause from Java) in the report query?
Query
with tmp as (
select d.gl_code, nvl(sum(d.debit),0)- nvl(sum(d.credit),0) as simple
from gl_forms g,gl_voucher_detail d,financial_year f
where g.voucher_no = d.voucher_no
and f.fid = $P{P_FID}
and g.voucher_date >= to_date('01-07-'+f.start_year,'dd-MM-yyyy') and g.voucher_date < $P{P_FROM}
and g.post=1
group by d.gl_code)
select g.voucher_no,
g.voucher_date,
g.voucher_type,
g.remarks remarks,
d.sr_no,
c.account_name,
d.debit,
d.credit,
d.narration narration,
c.account_code,
c.account_name || ' - ' || c.account_code Account,
(select name from company) name,
nvl((o.debit-o.credit),0) + nvl(tmp.simple,0) opening
from gl_voucher_detail d
join gl_forms g
on g.voucher_no = d.voucher_no
and g.post=1
and g.voucher_date between $P{P_FROM} and $P{P_TO}
left outer join opening_balances o
on d.gl_code = o.account_code
and o.fid = $P{P_FID}
right outer join chart_of_account c
on c.account_code = d.gl_code
left outer join tmp
on c.account_code = tmp.gl_code
where c.account_level=4
and c.active=1
and c.account_code = CASEWHEN($P{P_ACCOUNT}='%',c.account_code,$P{P_ACCOUNT})
Order by clause shall be one among these below two based on front fields selection:
order by c.account_name, g.voucher_date,g.voucher_no,d.sr_no
order by c.debit+c.credit desc

Optimizing Postgres query with timestamp filter

I have a query:
SELECT DISTINCT ON (analytics_staging_v2s.event_type, sent_email_v2s.recipient, sent_email_v2s.sent) sent_email_v2s.id, sent_email_v2s.user_id, analytics_staging_v2s.event_type, sent_email_v2s.campaign_id, sent_email_v2s.recipient, sent_email_v2s.sent, sent_email_v2s.stage, sent_email_v2s.sequence_id, people.role, people.company, people.first_name, people.last_name, sequences.name as sequence_name
FROM "sent_email_v2s"
LEFT JOIN analytics_staging_v2s ON sent_email_v2s.id = analytics_staging_v2s.sent_email_v2_id
JOIN people ON sent_email_v2s.person_id = people.id
JOIN sequences on sent_email_v2s.sequence_id = sequences.id
JOIN users ON sent_email_v2s.user_id = users.id
WHERE "sent_email_v2s"."status" = 1
AND "people"."person_type" = 0
AND (sent_email_v2s.sequence_id = 1888) AND (sent_email_v2s.sent >= '2016-03-18')
AND "users"."team_id" = 1
When I run EXPLAIN ANALYZE on it, I get:
Then, if I change that to the following (Just removing the (sent_email_v2s.sent >= '2016-03-18')) as follows:
SELECT DISTINCT ON (analytics_staging_v2s.event_type, sent_email_v2s.recipient, sent_email_v2s.sent) sent_email_v2s.id, sent_email_v2s.user_id, analytics_staging_v2s.event_type, sent_email_v2s.campaign_id, sent_email_v2s.recipient, sent_email_v2s.sent, sent_email_v2s.stage, sent_email_v2s.sequence_id, people.role, people.company, people.first_name, people.last_name, sequences.name as sequence_name
FROM "sent_email_v2s"
LEFT JOIN analytics_staging_v2s ON sent_email_v2s.id = analytics_staging_v2s.sent_email_v2_id
JOIN people ON sent_email_v2s.person_id = people.id
JOIN sequences on sent_email_v2s.sequence_id = sequences.id
JOIN users ON sent_email_v2s.user_id = users.id
WHERE "sent_email_v2s"."status" = 1
AND "people"."person_type" = 0
AND (sent_email_v2s.sequence_id = 1888) AND "users"."team_id" = 1
when I run EXPLAIN ANALYZE on this query, the results are:
EDIT:
The results above from today are about as I expected. When I ran this last night, however, the difference created by including the timestamp filter was about 100x slower (0.5s -> 59s). The EXPLAIN ANALYZE from last night showed all of the time increase to be attributed to the first unique/sort operation in the query plan above.
Could there be some kind of caching issue here? I am worried now that there might be something else going on (transiently) that might make this query take 100x longer since it happened at least once.
Any thoughts are appreciated!

T-SQL UPDATE query not giving expected results

I have an UPDATE query in SQL Server Management Studio 2012 that is not giving me the expected results. I believe it is because I had to add the IJDATE field to the SELECT and GROUP BY and therefore it is not summing correctly. I am trying to update the current month/year for each vendor. Is there a better way to write this UPDATE query?
UPDATE S
SET S.PurchaseDlr = I.PurchaseDlr
FROM (SELECT
IJVEND,
IJDATE,
SUM(IJQTY * IJCOST) AS PurchaseDlr
FROM
dbo.S2K_IJ
WHERE
IJTYPE IN ('I','ID')
GROUP BY
IJVEND,
IJDATE) I
INNER JOIN PurchaseDollars S on I.IJVEND = S.Vendor AND S.MonthNum = MONTH(I.IJDATE) AND S.Year = YEAR(I.IJDATE)
You are grouping by date, but it looks like you want to group by month and year:
UPDATE S
SET S.PurchaseDlr = I.PurchaseDlr
FROM (SELECT
IJVEND,
MONTH(IJDATE) AS Month,
YEAR(IJDATE) AS Year,
SUM(IJQTY * IJCOST) AS PurchaseDlr
FROM
dbo.S2K_IJ
WHERE
IJTYPE IN ('I','ID')
GROUP BY
IJVEND,
MONTH(IJDATE),
YEAR(IJDATE)) I
INNER JOIN PurchaseDollars S
ON I.IJVEND = S.Vendor
AND S.MonthNum = I.Month
AND S.Year = I.Year