I need to enter the total length of verblijfsduur (stay) per reisnr (trip) incrementally according to the order in which the celestial objects are visited.
This what I need: (ignore the tot_duur column, that is the sum of all verblijfsduur)
This what I get, see it dosn't show incrementally according to order, it shows the total perreisnr (trip number):
I don't know how I could indicate this in the PARTITION BY part of my query:
SELECT re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
,sum(be.verblijfsduur) OVER (PARTITION BY re.reisnr ORDER BY re.reisnr ) as
inc_duur
FROM reizen re INNER JOIN bezoeken be ON re.reisnr = be.reisnr
ORDER BY re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
I found the problem, thanks/sorry if you had an answer, I had to order by be.volgnr
SELECT re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
,SUM(be.verblijfsduur) OVER (PARTITION BY re.reisnr ORDER BY be.volgnr) as inc_duur
FROM reizen re INNER JOIN bezoeken be ON re.reisnr = be.reisnr
ORDER BY re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
Related
I would like to not reset a running total using the OVER clause in SQL server. I have tried several things and searched for an answer very hard, but could not find anything. My query is below, as well as the results.
SELECT
coaID,
fiscalID,
gl.amount,
SUM(gl.amount) OVER (PARTITION BY coaID, fiscalID ORDER BY gl.id) runningTotal
FROM
gl
INNER JOIN dbo.coa ON dbo.gl.coaId = coa.ID
WHERE
gl.coaID IN (
SELECT DISTINCT coa
FROM glCategoryGLAccountLink
WHERE glCategoryGLAccountLink.categoryId = 10001
)
AND gl.companyID=1
ORDER BY coaID, fiscalID
It mostly works, but resets the running total when fiscalID changes. My objective is to allow the running total to continue without being reset for each coaID.
My objective is to allow the running total to continue without being reset for each coaID.
Remove the fiscalID from the partition, ie change this:
SUM(gl.amount) OVER (PARTITION BY coaID, fiscalID ORDER BY gl.id) runningTotal
To:
SUM(gl.amount) OVER (PARTITION BY coaID ORDER BY gl.id) runningTotal
I think you still need to take fiscalid into account for the ordering. So:
SELECT coaID, fiscalID, gl.amount,
SUM(gl.amount) OVER (PARTITION BY coaID ORDER BY fiscalID, gl.id) runningTotal
FROM gl JOIN
dbo.coa c
ON gl.coaId = c.ID
WHERE gl.coaID IN (SELECT gal.coa
FROM glCategoryGLAccountLink gal
WHERE gal.categoryId = 10001
) AND
gl.companyID = 1
ORDER BY coaID, fiscalID;
It is possible that gl.id takes this into account. Your question is not clear on that.
Notes:
Qualify all column references with the table they come from. I would have added the qualifiers, but it is not clear which table they come from.
Table aliases help.
SELECT DISTINCT is not needed with IN. Most databases will ignore it, but you run the risk of the DISTINCT affecting the optimizer.
I have the following dataset:
Each sales order line has an item which can be found in various location areas in our warehouse (UPPER, GROUND, FLOOR). What I want is a way to evaluate each sales order line and then pick one location, based on a condition.
The condition would say, if SO line contains a location with FLOOR, pick only that location, else check if it contains GROUND, then pick that, or if it contains neither ground or floor then return UPPER.
I don't want to see multiple location areas for each SO line. What's all the ways this can be done? I'd imagine some form of using a case statement with a HAVING clause?
This can be done using the row_number function by ordering the location areas based on the conditions.
select *
from (select t.*
,row_number() over(partition by so#
order by case when location_area='Floor' then 1
when location_area='GROUND' then 2
else 3 end) rn
from tablename t
) x
where rn = 1
Select coalesce(f.SO, g.SO, u.SO) SO,
coalesce(f.Line, g.Line, u.Line) Line,
coalesce(f.item_code, g.item_code, u.item_code) item_code,
coalesce(f.item_description, g.item_description, u.item_description) item_description,
coalesce(f.SO_Qty, g.SO_Qty, u.SO_Qty) SO_Qty,
coalesce(f.branch_Number, g.branch_Number, u.branch_Number) branch_Number,
coalesce(f.location_area, g.location_area, u.location_area) location_area
from myTable f
full join myTable g
on f.location_area='floor'
and g.SO = f.So
and g.location_area='ground'
full join myTable u
on u.SO = f.So
and u.location_area='upper'
RDBMS = Microsoft SQL Server
I work for a refrigeration company and we want to do a better job of tracking the cost bottles of refrigerant were bought at for each inventory location. I am trying to create a SQL Query that pulls this information but I am running into some issues. For each inventory location I want to display the last cost refrigerant was bought at for that inventory location.I want to see the latest date we have record of for this location purchasing a specific refrigerant. I have tried using the Max function unsuccessfully and the Row_Number function I have not been able to get work. Any help would be much appreciated.
See below the code sample I am trying to only get to display the Latest Date each inventory location purchased R-22 30 pound jug.
select
lctn_id as Location,
invntryitm_id as InventoryItemID,
invntryitm_nme as InventoryItemName,
prchseordrlst_dte_rqstd as DateRequested,
prchseordrlst_unt_cst as UnitCost
from
invntryitm
join
prchseordrlst on prchseordrlst.invntryitm_rn = invntryitm.invntryitm_rn
join
prchseordr on prchseordr.prchseordr_rn = prchseordrlst.prchseordr_rn
join
lctn on lctn.lctn_rn = prchseordr.lctn_rn
where
invntryitm.invntryitm_nme ='REFRIGERANT R-22 30#'
and lctn_obslte = 'N'
group by
lctn.lctn_id, invntryitm.invntryitm_id, invntryitm.invntryitm_nme,
prchseordrlst.prchseordrlst_unt_cst
order by
lctn_id
I think an analytic/windowing function would give you what you need:
with location_data as (
select
lctn_id as Location,
invntryitm_id as InventoryItemID,
invntryitm_nme as InventoryItemName,
prchseordrlst_dte_rqstd as DateRequested,
prchseordrlst_unt_cst as UnitCost,
max (prchseordrlst_dte_rqstd) over (partition by lctn_id) as max_date
from
invntryitm
JOIN prchseordrlst on prchseordrlst.invntryitm_rn = invntryitm.invntryitm_rn
JOIN prchseordr on prchseordr.prchseordr_rn = prchseordrlst.prchseordr_rn
JOIN lctn on lctn.lctn_rn = prchseordr.lctn_rn
where
invntryitm.invntryitm_nme ='REFRIGERANT R-22 30#' and
lctn_obslte = 'N'
)
select *
from location_data
where max_date = DateRequested
order by Location
Bear in mind that if there is a tie, two location_id records with the same date, then you will get both of them back. If this is an issue, then you probably want row_number() instead of max():
row_number() over (partition by lctn_id order by prchseordrlst_dte_rqstd desc) as rn
And then you would
where rn = 1
to get the first row
The reason I didn't list row_number() first is that max is O(n), and if your data has dates and times, it may be sufficient for what you need.
In the query
cr is customers,
chh? ise customer_pays,
cari_kod is customer code,
cari_unvan1 is customer name
cha_tarihi is date of pay,
cha_meblag is pay amount
The purpose of query, the get the specisified list of customers and their last date for pay and amount of money...
Actually my manager needs more details but the query is very slow and that is why im using only 3 subquery.
The question is how to combine them ?
I have researched about Cte and "with clause" and "subquery in "where " but without luck.
Can anybody have a proposal.
Operating system is win2003 and sql server version is mssql 2005.
Regards
select cr.cari_kod,cr.cari_unvan1, cr.cari_temsilci_kodu,
(select top 1
chh1.cha_tarihi
from dbo.CARI_HESAP_HAREKETLERI chh1 where chh1.cha_kod=cr.cari_kod order by chh1.cha_RECno) as sontar,
(select top 1
chh2.cha_meblag
from dbo.CARI_HESAP_HAREKETLERI chh2 where chh2.cha_kod=cr.cari_kod order by chh2.cha_RECno) as sontutar
from dbo.CARI_HESAPLAR cr
where (select top 1
chh3.cha_tarihi
from dbo.CARI_HESAP_HAREKETLERI chh3 where chh3.cha_kod=cr.cari_kod order by chh3.cha_RECno) >'20130314'
and
cr.cari_bolge_kodu='322'
or
cr.cari_bolge_kodu='324'
order by cr.cari_kod
You will probably speed up the query by changing your last where clause to:
where (select top 1 chh3.cha_tarihi
from dbo.CARI_HESAP_HAREKETLERI chh3 where chh3.cha_kod=cr.cari_kod
order by chh3.cha_RECno
) >'20130314' and
cr.cari_bolge_kodu in ('322', '324')
order by cr.cari_kod
Assuming that you want both the date condition met and one of the two codes. Your original logic is the (date and code = 322) OR (code = 324).
The overall query can be improved by finding the record in the chh table and then just using that. For this, you want to use the window function row_number(). I think this is the query that you want:
select cari_kod, cari_unvan1, cari_temsilci_kodu,
cha_tarihi, cha_meblag
from (select cr.*, chh.*,
ROW_NUMBER() over (partition by chh.cha_kod order by chh.cha_recno) as seqnum
from dbo.CARI_HESAPLAR cr join
dbo.CARI_HESAP_HAREKETLERI chh
on chh.cha_kod=cr.cari_kod
where cr.cari_bolge_kodu in ('322', '324')
) t
where chh3.cha_tarihi > '20130314' and seqnum = 1
order by cr.cari_kod;
This version assumes the revised logic date/code logic.
The inner subquery select might generate an error if there are two columns with the same name in both tables. If so, then just list the columns instead of using *.
I am writing a window function that is supposed create a month window and only grab records that has the max value in the update flag field within that said month window.
I am having issues with my window function its still showing all results in the window when it should be showing the only the max value.
I have left my code below. Please help.
SELECT
gb1.SKU_Id,
gb1.Warehouse_Code,
gb1.Period_Start,
gb1.country,
tm.c445_month,
tm.report_date,
gb1.update_flag,
max(gb1.update_flag) over (partition by tm.yearmonth order by gb1.update_flag range between unbounded preceding and current row ) as update_window,
SUM(gb1.TOTAL_NEW_SALES_FORECAST) AS dc_forecast
FROM BAS_E2E_OUTPUT_GLOBAL_FCST gb1
inner join (
SELECT
gb2.SKU_Id,
gb2.Warehouse_Code,
gb2.Period_Start,
gb2.country,
gb2.update_flag,
gb2.report_date,
tm1.week_date,
tm1.c445_month,
tm1.yearmonth
FROM BAS_E2E_OUTPUT_GLOBAL_FCST as gb2
left join (
select distinct(week_date) as week_date,
c445_month,
yearmonth
from "PROD"."INV_PROD"."BAS_445_MONTH_ALIGNMENT"
group by c445_month, week_date, yearmonth
) as tm1 on gb2.report_date = tm1.week_date
group by SKU_Id,
Warehouse_Code,
Period_Start,
country,
update_flag,
report_date,
tm1.week_date,
tm1.c445_month,
tm1.yearmonth
) as tm
on gb1.report_date = tm.week_date
and gb1.SKU_ID = tm.sku_id
and gb1.Warehouse_Code = tm.warehouse_code
and gb1.Period_Start = tm.period_start
and gb1.country = tm.country
GROUP BY
gb1.SKU_Id,
gb1.Warehouse_Code,
gb1.Period_Start,
gb1.country,
tm.c445_month,
tm.yearmonth,
tm.report_date,
gb1.update_flag
You are currently using MAX with the window being defined as every preceding row up and including the current one. Hence, rightfully the max value it returns should probably change for each record. Perhaps you wanted to take the max over a fixed partition:
MAX(gb1.update_flag) OVER (PARTITION BY tm.yearmonth) AS update_window
By the way, if you did really intend to use MAX with your current window logic, on most versions of SQL the ORDER BY clause can be simplified to:
MAX(gb1.update_flag) OVER (PARTITION BY tm.yearmonth ORDER BY gb1.update_flag) AS update_window
That is, the default range is unbounded preceding to the current row, so it is not necessary to say this.