How do I use the value from row above when a given column value is zero? - sql

I have a table of items by date (each row is a new date). I am drawing out a value from another column D. I need it to replace 0s though. I need the following logic: when D=0 for that date, use the value in column D from the date prior.
Actually, truth be told, I need it to say, when D is 0, use the value from the latest date where D was not a 0, but the first will get me most of the way there.
Is there a way to build this logic? Maybe a CTE?
Thank you very much.
PS I'm using SSMS 2008.
EDIT: I wasn't very clear at first. The value I want to change is not the date. I want change the value in D with the latest non-zero value from D, based on date.

May be the following query might help you. It uses the OUTER APPLY to fetch the results. Screenshot #1 shows the sample data and query output against the sample data. This query can be written better but this is what I could come up with right now.
Hope that helps.
SELECT ITM.Id
, COALESCE(DAT.New_D, ITM.D) AS D
, ITM.DateValue
FROM dbo.Items ITM
OUTER APPLY (
SELECT
TOP 1 D AS New_D
FROM dbo.Items DAT
WHERE DAT.DateValue < ITM.DateValue
AND DAT.D <> 0
AND ITM.D = 0
ORDER BY DAT.DateValue DESC
) DAT
Screenshot #1:

UPDATE t
Set value = SELECT value
FROM table
WHERE date = (SELECT MAX(t1.date)
FROM table t1
WHERE t1.value != 0
AND t1.date < t.date)
FROM table t
WHERE t.value = 0

You could maybe something like this as part of an update script...
SET myTable.D = (
SELECT TOP 1 myTable2.D
FROM myTable2
WHERE myTable2.myDateField < myTable.myDateField
AND myTable2.D != 0
ORDER BY myTable2.myDateField DESC)
That's assuming that you want to actually update the data though rather than just replace the values for the purpose of a select query.

How about:
SELECT
i.ID,
i.DateValue,
D = CASE WHEN I.D <> 0 THEN I.D ELSE X.D END
FROM
Items I
OUTER APPLY (
SELECT TOP 1 S.D
FROM Items S
WHERE S.DATEVALUE < I.DATEVALUE AND S.D <> 0
ORDER BY S.DATEVALUE DESC
) X

SELECT t.id,
CASE WHEN t.D = 0 THEN t0.D
ELSE t.D END
FROM table AS t
LEFT JOIN table AS t0
ON t0.time =
(
SELECT MAX(time) FROM t0
WHERE t0.time < t.time
AND t0.D != 0
)
or if you want to avoid aggregates entirely,
SELECT t.id,
CASE WHEN t.D = 0 THEN t0.D
ELSE t.D END
FROM table AS t
LEFT JOIN table AS t0
ON t0.time < t.time
LEFT JOIN table AS tx
ON tx.time > t0.time
WHERE t0.D != 0
AND tx.D != 0
AND tx.id IS NULL -- i.e. there isn't any

Related

How to determine next row value using select and case in SQL server?

Using select and case statement in SQL
How to make the 4th column "1" if next row of LETTER column is D. See example below:
https://i.stack.imgur.com/br4dn.png
You can combine CASE with LEAD(). Assuming you are ordering by stepthe query can look like:
select
t.*,m
case when lead(letter) over(order by step) = 'D'
then 1 else 0 end as is_next_row_letter_d
from t
Assuming that the STEP column provides the ordering and that it is continuous, we could use a self-join approach here:
SELECT t1.STEP, t1.ID, t1.LETTER,
CASE WHEN t2.LETTER = 'D' THEN 1 ELSE 0 END AS IS_NEXT_D
FROM yourTable t1
LEFT JOIN yourTable t2
ON t2.STEP = t1.STEP + 1
ORDER BY t1.STEP;

I just started learning SQL and I couldn't do the query, can you help me?

There is a field in the sql query that I can't do. First of all, a new column must be added to the table below. The value of this column needs to be percent complete, so it's a percentage value. So for example, there are 7 values from Cupboard=1 shelves. Where IsCounted is here, 3 of them are counted. In other words, those with Cupboard = 1 should write the percentage value of 3/7 as the value in the new column to be created. If the IsCounted of the others is 0, it will write zero percent. How can I do this?
My Sql Code:
SELECT a.RegionName,
a.Cupboard,
a.Shelf,
(CASE WHEN ToplamSayım > 0 THEN 1 ELSE 0 END) AS IsCounted
FROM (SELECT p.RegionName,
r.Shelf,
r.Cupboard,
(SELECT COUNT(*)
FROM FAZIKI.dbo.PM_ProductCountingNew
WHERE RegionCupboardShelfTypeId = r.Id) AS ToplamSayım
FROM FAZIKI.dbo.DF_PMRegionType p
JOIN FAZIKI.dbo.DF_PMRegionCupboardShelfType r ON p.Id = r.RegionTypeId
WHERE p.WarehouseId = 45) a
ORDER BY a.RegionName;
The result is as in the picture below:
It looks like a windowed AVG should do the trick, although it's not entirely clear what the partitioning column should be.
The SELECT COUNT can be simplified to an EXISTS
SELECT a.RegionName,
a.Cupboard,
a.Shelf,
a.IsCounted,
AVG(a.IsCounted * 1.0) OVER (PARTITION BY a.RegionName, a.Cupboard) Percentage
FROM (
SELECT p.RegionName,
r.Shelf,
r.Cupboard,
CASE WHEN EXISTS (SELECT 1
FROM FAZIKI.dbo.PM_ProductCountingNew pcn
WHERE pcn.RegionCupboardShelfTypeId = r.Id
) THEN 1 ELSE 0 END AS IsCounted
FROM FAZIKI.dbo.DF_PMRegionType p
JOIN FAZIKI.dbo.DF_PMRegionCupboardShelfType r ON p.Id = r.RegionTypeId
WHERE p.WarehouseId = 45
) a
ORDER BY a.RegionName;

How to use max in with clause to get a single value and use this value in main query with null check

I am unable to write this, please help. Below will give an idea of what I'm trying to achieve.
WITH monthly_data AS
(SELECT MAX(some_date) latest_dt FROM monthly_data
)
SELECT SUM(data)
FROM daily_data
WHERE (monthly_data.latest_dt IS NULL
OR daily_data.some_date > monthly_data.latest_dt)
table: monthly_data
id some_date
007 08-MAY-2018
table: daily_data
some_date data
07-MAY-2018 1
08-MAY-2018 1
09-MAY-2018 1
Expected result
Case 1: 1 row exist in table monthly_data.
Query should return 1.
Case 2: No rows exist in table montly_data.
Query should return 3.
The joins in the above query is incorrect but basically written to give you an idea of what I'm trying to do. Also, when I say no rows exist in table monthly_data, it is simplified explanation. There are other conditions in the actual query that filter out the data.
This has to go in a procedure
Edit
Thanks to #D-Shih I'm in a much better position where I started by using the exist clause query that he has provided.
On performance terms, can we write it in a faster way? Something that can evaluate to below would be fastest I believe
WITH CTE AS
( SELECT MAX(some_date) latest_dt FROM monthly_data
)
SELECT SUM(d.some_data)
FROM daily_data d
WHERE (d.some_date > '08-MAY-2018'
OR '08-MAY-2018' IS NULL)
If I understand correct.I think this will be work.
Due to you didn't provide some sample data and expect result.If that didn't your expect result,you can provide some sample data and expect result,I will edit my answer.
WITH CTE AS (
SELECT Max(some_date) latest_dt
FROM monthly_data
)
SELECT Sum(d.data)
FROM daily_data d
WHERE Exists (
SELECT 1
FROM CTE c
WHERE
d.some_date > c.latest_dt
OR
c.latest_dt IS NULL
)
Edit
You can try use CTE table JOIN on daily_data table
WITH CTE AS (
SELECT Max(some_date) latest_dt
FROM monthly_data
)
SELECT SUM(d.data)
FROM CTE c JOIN daily_data d
ON d.some_date > c.latest_dt OR c.latest_dt IS NULL;
sqlfiddle: http://sqlfiddle.com/#!4/33c64e/28
TRY THIS:
SELECT CASE WHEN SUM(CASE WHEN md.Sdate IS NOT NULL THEN 1 ELSE 0 END) > 0 THEN
SUM(CASE WHEN md.Sdate IS NOT NULL THEN 1 ELSE 0 END)
ELSE
SUM(CASE WHEN md.Sdate IS NULL THEN 1 ELSE 0 END)
END cnt
FROM daily_data dd
LEFT JOIN monthly_data md ON md.Sdate = dd.Sdate
....... {other conditions}

SQL query performance- ssis

I have an update query which is taking 15 hrs to complete in the production server. What modifications can I do to make it run faster.
UPDATE pos
SET pos.is_pub = 1
FROM A pos
WHERE pos.is_pub <> 1
and s_type <= (
SELECT TOP 1 month
FROM B with(nolock)
)
AND isnull(is_pub, 0) <> 1
AND isnull(is_adj, 0) <> 1
here 'type' and 'month' are actually integers having number of months as the values.
Start by moving the subquery to the FROM clause:
UPDATE pos
SET pos.is_pub = 1
FROM A pos CROSS JOIN
(SELECT TOP 1 month
FROM B with (nolock) -- very strange, no `order by`
) b
WHERE pos.is_pub <> 1 AND
pos.s_type <= b.month AND -- very strange, comparing "type" to "month"
(is_adj is null or is_adj <> 0);
Given this, there is not much that indexes can do because of the WHERE conditions. Perhaps you are updating essentially all rows in the table, which can be quite expensive. It is often cheaper to re-build the table rather than update it.

SQL Group By and assign highest a value

I am struggling with the SQL (mssql) to manipulate my data as i need it. I have a table like this;
SOMEID, SOMEFIELD, DATE
5 True 01-01-2010
5 True 01-01-2011
5 False 05-05-2012
7 True 05-05-2011
7 False 06-07-2015
What I am trying to achieve is to add another column which assigns the value 1 if they are the most recent for that ID, and 0 if not. So in the above data example the new column values from top to bottom would be 0, 0, 1, 0, 1.
I know I need to group by date but am having trouble assigning the values.
Thanks for any pointers!
You can use row_number() in SQL Server like this:
select *
, case when (row_number() over (partition by SOMEID order by [Date] desc)) = 1 then 1 else 0 end seq
from
yourTable
order by
SOMEID, [Date];
SQL Fiddle Demo
You can use a self join to get the highest row per group then in update query use a case statement to assign value to new column
update a
set a.[somecol] = case when b.[SOMEID] is null then 1 else 0 end
from demo a
left join demo b on a.[SOMEID] = b.[SOMEID]
and a.[DATE] < b.[DATE]
DEMO
try this
SELECT SOMEID, SOMEFIELD, DATE
, CASE WHEN (SELECT MAX(SubTab.Date)
FROM myTable SubTab
WHERE SubTab.SOMEID = myTable.SOMEID
) = myTable.DATE
THEN 1 ELSE 0 END
FROM myTable