I have a question from my sql script in DB2.
SELECT a.fcyfdtaid current_id_book, a.fcyyyymm current_month_book, a.fcyaccsts current_quality_book, a.fcyarrmth current_dpd,
b.fcyfdtaid old_id_book, b.fcyyyymm old_month_book, b.fcyaccsts old_quality_book, b.fcyarrmth old_dpd
FROM crbidnpd.entfcym0 a JOIN crbidnpd.entfcym0 b
ON a.fcyfdtaid = b.fcyfdtaid AND
b.fcyarrmth < a.fcyarrmth AND
a.fcyyyymm='201911' AND a.fcyarrmth>10
where NOT EXISTS
(SELECT b.fcyfdtaid old_id_book FROM crbidnpd.entfcym0 c
WHERE c.fcyfdtaid = a.fcyfdtaid AND c.fcyyyymm = a.fcyyyymm AND c.fcyyyymm = b.fcyyyymm) order by a.fcydtaid;
and the result
images
how to make it being one row that displays in the last 2 years, on the left 2011 and on the right only 2010?
Related
I have 2 DB2 tables. I want to find out records that are in table A is not Table B with the following condition
I wrote this query and it is not working
SELECT A.CL_BATCH_DEPT,
A.CL_TRANS_CODE, A.CL_CUR_DOC_NO
FROM DBPA60AC.TB_ACCOUNT_EVENT A
LEFT JOIN DBPA60AC.TB_DOCUMENT B ON A.CL_CUR_DOC_NO = B.CL_DOCNO
WHERE A.CL_BATCH_DEPT = 'R07' AND A.CL_TRANS_CODE = '210'
AND A.CL_CUR_DOC_NO = "PI%" AND
B CL_DOCNO IS NULL
I am guessing that you want:
SELECT e.*
FROM DBPA60AC.TB_ACCOUNT_EVENT e LEFT JOIN
DBPA60AC.TB_DOCUMENT d
ON e.CL_CUR_DOC_NO = d.CL_DOCNO
WHERE e.CL_BATCH_DEPT = 'R07' AND
e.CL_TRANS_CODE = '210' AND
e.CL_CUR_DOC_NO LIKE 'PI%' AND
d.CL_DOCNO IS NULL;
That is, the comparison to PI% suggests that you really want LIKE.
Note that I also changed the aliases so they are meaningful.
This might seem like a noob question, but here's the thing:
In the image, the first table name is facturaDetalle and the second one's facturamaster
I want to SUM all the total matching idfactura in facturadetalle and save them into the total column in the facturamaster table.
I'm working on a master-detail form in ASP.NET
UPDATE m
SET total = (SELECT SUM(d.total) FROM dbo.facturadetalle d WHERE d.idfactura = m.idfactura)
FROM dbo.facturamaster m
--WHERE m.total IS NULL
You can use UPDATE statement to do that.
UPDATE m
SET TOTAL = SUM(d.Total)
FROM idfactura AS m
INNER JOIN idfacturaldetelle AS d
ON m.idfactura = d.idfactura
I need to add a proper "needCounter" column code to my query in Access.
It should count rows from 1 to "WorksPerWeekCounter" as it is shown in last, yellow column. Or better 0 to "WorksPerWeekCounter-1" :)
I find my code wrong because my "counter" does not correspond to my "counterMin"
Let's say I have a table [WORKS(roboczy)], and first 4 columns are just like below [http://i.stack.imgur.com/mTO53.jpg][picture] shows. (sorry, my reputation is too low to show pictures)
This is the Access query code I use (week2date function is in VBA code):
SELECT
W.Id_work, W.Id_pracownika, W.vecka, W.Rok,
week2date(W.vecka,W.Rok) AS dataWK,
C.countWorksPerWeek as WorksPerWeekCounter,
C.Id_workMin as id_workMin,
(W.Id_work- C.Id_workMin) as AddDays,
DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work]) AS counter,
C.counterMin
FROM [WORKS(roboczy)] W
inner join
( SELECT count(id_work) as countWorksPerWeek, Id_pracownika, vecka, Rok, min(id_work) as Id_workMin, min(DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work])) AS counterMin
FROM [WORKS(roboczy)]
GROUP BY Id_pracownika, vecka, Rok
) C ON W.Id_pracownika = C.Id_pracownika AND W.vecka = C.vecka AND C.Rok = W.Rok
GROUP BY W.Id_pracownika, W.vecka, Id_work, W.Rok, week2date(W.vecka,W.Rok), C.countWorksPerWeek, C.Id_workMin, C.counterMin
You can use a series of subqueries in one step:
SELECT w.Id_work, w.Id_pracownika, w.vecka, w.Rok, w.dataWK,
(SELECT count(w2.Rok) FROM WorkCounter w2
WHERE w2.vecka = w.vecka GROUP BY w2.vecka) As WorksPerWeekCounter,
(SELECT min(w3.Id_work) FROM WorkCounter w3
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As id_workMin,
w.Id_work -
(SELECT min(w3.Id_work) FROM WorkCounter w3
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As AddDays,
(SELECT count(*) FROM WorkCounter w4
WHERE w4.Id_work <= w.Id_work and w4.vecka = w.vecka) As Counter
FROM [WORKS(roboczy)] W
I'm working on a MVC 3 project where some of the reports will generate the SQL queries in code then executes them on an Oracle 10g database. Also, I understand SQL injection attacks, and all the values are coming from drop down lists, not text inputs, so this is not an issue. My generated queries "work", but they are extremely slow. Here is a quick run down on how the report works:
User selected a general ledger account and other filters.
User selected a time period (a fiscal year, a quarter or a specific month).
User runs report.
The report shows the general ledger account (or accounts if the select to show all accounts) and the values spent for each month along with a total for that account. The output from the query will look like this:
Account Number and Name | Month 1 | Month 2 | etc | Total
Month 1, Month 2, etc are dynamically added in while the SQL code is being generated, so depending on what the user selects, it may, or may not, be returned in the result set. What can I do to make this query faster? When I ran this query yesterday for the entire fiscal year (and only 1 account), it took 10mins for it to complete. I don't feel this is acceptable performance, and would like to make it faster. Keep in mind, I can only make changes to the query itself, not the database, so indexing is not going to happen (the DBAs keep everything locked down). The query is below. Thanks for any input you all can provide on this.
SELECT gl.gen_led_acct_nbr, gl.gen_led_acct_scr_nm, bu.bus_unit_txt, gl.gen_led_acct_typ_nm,
(SELECT SUM(fct.pd_txn_amt) Jun2012
FROM Maintable fct
JOIN Dimtable1 gl1 ON fct.gen_led_acct_key = gl1.gen_led_acct_key
JOIN Dimtable2 bu1 ON fct.bus_unit_key = bu1.bus_unit_key
JOIN Dimtable3 cc1 ON fct.cst_ctr_key = cc1.cst_ctr_key
JOIN Dimtable4 oc1 ON fct.cst_ctr_own_org_key = oc1.org_cd_key
JOIN Dimtable5 dt1 ON fct.chk_dt_key = dt1.dt_key
WHERE gl1.gen_led_acct_nbr = gl.gen_led_acct_nbr
AND bu1.bus_unit_txt = bu.bus_unit_txt
AND dt1.fscl_mo_nbr = 1
AND dt1.fscl_yr_nbr = 2012
GROUP BY gl1.gen_led_acct_nbr, gl1.gen_led_acct_scr_nm, bu1.bus_unit_txt) Jun2012,
(SELECT SUM(fct.pd_txn_amt) Jun2012
FROM Maintable fct
JOIN Dimtable1 gl2 ON fct.gen_led_acct_key = gl2.gen_led_acct_key
JOIN Dimtable2 bu2 ON fct.bus_unit_key = bu2.bus_unit_key
JOIN Dimtable3 cc2 ON fct.cst_ctr_key = cc2.cst_ctr_key
JOIN Dimtable4 oc2 ON fct.cst_ctr_own_org_key = oc2.org_cd_key
JOIN Dimtable5 dt2 ON fct.chk_dt_key = dt2.dt_key
WHERE gl2.gen_led_acct_nbr = gl.gen_led_acct_nbr
AND bu2.bus_unit_txt = bu.bus_unit_txt
AND dt2.fscl_mo_nbr = 2
AND dt2.fscl_yr_nbr = 2012
GROUP BY gl2.gen_led_acct_nbr, gl2.gen_led_acct_scr_nm, bu2.bus_unit_txt) Jul2012,
(etc...)
--The sub queries above can be repeated up to an indefinite amount of times (maybe 20+), but with a different fscl_mo_nbr (and possibly fscl_yr_nbr as well)
FROM Maintable fct
JOIN Dimtable1 gl ON fct.gen_led_acct_key = gl.gen_led_acct_key
JOIN Dimtable2 bu ON fct.bus_unit_key = bu.bus_unit_key
JOIN Dimtable3 cc ON fct.cst_ctr_key = cc.cst_ctr_key
JOIN Dimtable4 oc ON fct.cst_ctr_own_org_key = oc.org_cd_key
JOIN Dimtable5 dt ON fct.chk_dt_key = dt.dt_key
WHERE gl.gen_led_acct_nbr = 000000
AND bu.bus_unit_txt = 'AAAAA'
AND dt.fscl_mo_nbr = 1
AND dt.fscl_yr_nbr = 2012
GROUP BY gl.gen_led_acct_nbr, gl.gen_led_acct_scr_nm, bu.bus_unit_txt, gl.gen_led_acct_typ_nm
ORDER BY gl.gen_led_acct_nbr, gl.gen_led_acct_typ_nm
Your comment:
--The sub query above can be repeated up to another 11 times, but with a different dt1.fscl_mo_nbr
Suggests that one solution would be to use "in" rather than "=", as in:
AND dt1.fscl_mo_nbr in (1, 2, . . . )
You then would want to add t1.fscl_mo_nbr to the group by clause.
I also notice that this is a big correlated subquery, which is probably also killing performance. Something like the following should simplify the query and help it run faster:
SELECT gl.gen_led_acct_nbr, gl.gen_led_acct_scr_nm, bu.bus_unit_txt,
gl.gen_led_acct_typ_nm,
sum(case when year = 2012 and dt.fscl_mo_nbr = 1 then fct.pd_txn_amt end) as Jun2012,
sum(case when year = 2012 and dt.fscl_mo_nbr = 2 then fct.pd_txn_amt end) as Jul2012,
etc.
FROM Maintable fct
JOIN Dimtable1 gl ON fct.gen_led_acct_key = gl.gen_led_acct_key
JOIN Dimtable2 bu ON fct.bus_unit_key = bu.bus_unit_key
JOIN Dimtable3 cc ON fct.cst_ctr_key = cc.cst_ctr_key
JOIN Dimtable4 oc ON fct.cst_ctr_own_org_key = oc.org_cd_key
JOIN Dimtable5 dt ON fct.chk_dt_key = dt.dt_key
where dt.fscl_mo_nbr in (1, 2, 3 . . .)
AND dt.fscl_yr_nbr = 2012
GROUP BY gl.gen_led_acct_nbr, gl.gen_led_acct_scr_nm,
bu.bus_unit_txt, dt.fscl_mo_nbr
(Please excuse any typing errors . . . I removed the "1" suffix from all the aliases.)
How to show current year data and previous Data in two column SQL Server 2000
Below Procedure shows my Given date Data I want to set Previous year data from given date in other column
SELECT TOP 100 PERCENT
dbo.SI_Item.ig2_Code, dbo.SI_ItemGroup2.ig2_Desc,
SUM(SI_InvoiceDetail.invcd_Rate * dbo.SI_InvoiceDetail.invcd_Qty -
dbo.SI_InvoiceDetail.invcd_DiscountAmt) AS Total
FROM
dbo.SI_InvoiceDetail
INNER JOIN
dbo.SI_Item ON dbo.SI_InvoiceDetail.itm_ItemCode = dbo.SI_Item.itm_ItemCode
INNER JOIN
dbo.SI_InvoiceMaster ON dbo.SI_InvoiceDetail.invcm_CoCode = dbo.SI_InvoiceMaster.invcm_CoCode AND
dbo.SI_InvoiceDetail.invcm_BrCode = dbo.SI_InvoiceMaster.invcm_BrCode AND
dbo.SI_InvoiceDetail.invcm_SiteCode = dbo.SI_InvoiceMaster.invcm_SiteCode AND
dbo.SI_InvoiceDetail.invcm_Year = dbo.SI_InvoiceMaster.invcm_Year AND
dbo.SI_InvoiceDetail.invcm_Period = dbo.SI_InvoiceMaster.invcm_Period AND
dbo.SI_InvoiceDetail.docs_DocCode = dbo.SI_InvoiceMaster.docs_DocCode AND
dbo.SI_InvoiceDetail.doctyp_Code = dbo.SI_InvoiceMaster.doctyp_Code AND
dbo.SI_InvoiceDetail.invcm_DocNo = dbo.SI_InvoiceMaster.invcm_DocNo
INNER JOIN
dbo.SI_ItemGroup2 ON dbo.SI_Item.ig2_Code = dbo.SI_ItemGroup2.ig2_Code
WHERE (dbo.SI_InvoiceDetail.docs_DocCode = 'inv') AND
(dbo.SI_InvoiceDetail.itm_ItemCode BETWEEN '0101010000001' AND '0301020004001') AND
(dbo.SI_InvoiceMaster.invcm_Date BETWEEN #Invcm_date_from AND #Invcm_date_to)
GROUP BY dbo.SI_Item.ig2_Code ,SI_ItemGroup2.ig2_Desc
Resolved it 70% by changing in Query as below
SELECT TOP 100 PERCENT dbo.SI_Item.ig2_Code, dbo.SI_ItemGroup2.ig2_Desc,year(dbo.SI_InvoiceMaster.invcm_Date) AS SalesYear,
&
Group By Year(dbo.SI_InvoiceMaster.invcm_Date),dbo.SI_Item.ig2_Code ,SI_ItemGroup2.ig2_Desc
ORDER BY Year(dbo.SI_InvoiceMaster.invcm_Date) ,dbo.SI_Item.ig2_Code
but its works but not as i want because its showing year in Row and User mut inpuer date range of aboce 2 years then will show.
if anyone Got Proper solution do share please