I have a table as follows:
Date Line Qty RN
15-03-18 1 10 1
16-03-18 1 10 2
17-03-18 1 10 3
18-03-18 1 10 4
15-03-18 2 10 5
16-03-18 2 10 6
17-03-18 2 10 7
I have a combobox which has options 5,6 and 7
If user selects 5, I wish to make qty for rownum 6 and 7= 0
If user selects 6, I wish to make qty for rownum 7= 0
If user selects 7, I wish to keep qty as it is (for each line)
Meaning select qty for dates 5, 6 or 7 days make rest 0
But my row numbers are 1,2, mabbe upto 100.
I need to convert them in range of 7(weekly) and then accordingly make the
qty 0.
Trying something like this:
If 5 is selected from combobox:
SELECT date,line,(CASE
when rn=6 then 0
when rn=7 then 0
else qty
end) as qty
from above table
I am selecting date, line and changing qty according to the rn(rownum) Appreciate any help.
Use functions mod() and sign(), this construction gave me desired values:
select tbl.*, decode(sign(5 - mod(rn - 1, 7)), 1, qty, 0)
from tbl
demo
In place of hardcoded 5 put your parameter.
Related
I've got an inventory transactions table :
Product
Date
Direction
Quantity
A
Date 1
IN
3
B
Date 2
IN
55.7
A
Date 3
OUT
1
B
Date 3
OUT
8
B
Date 3
IN
2
I can easily get the stock for any date with the following query :
SELECT Product,
SUM(CASE Direction WHEN 'IN' THEN Quantity ELSE -1 * Quantity END)
FROM Transactions
WHERE Date <= '#DateValue#'
GROUP BY Product;
Now my purpose is to get stocks aged like this using the FIFO principle :
Product
Total stock
0-30 days
31-60 days
61-90 days
91+ days
A
3
3
0
0
0
B
34.2
10
14.2
7
3
C
25
20
3
1
1
D
10
2
8
0
0
E
1
0
0
1
0
I am using SQL Server 2016 & SSMS 18.
The solution should be fast as it will be working against a table with 3,000,000+ rows.
A single query is preferred since it will be integrated into an ERP system.
I have yet to find a solution based on a single query after weeks of research. Any help is appreciated. Thanks in advance.
I have a table that contains one or more records for each item. Each item can contain multiple sub-items (boards) and so the Itemid is often replicated with each record showing the division category (a number) that the Item/sub-item combo resides in:
ItemId Board# Division
142585109 0 6
142585114 0 3
142585116 0 1
142585120 0 4
142585197 0 5
142585197 2 4
142585197 3 3
142585197 5 6
142585197 8 1
142585294 0 4
142585317 0 1
I want to update the table and aggregate all of the division values (as a comma separated string) in a new field in this table, something like:
ItemId Board# AggDivisions
142585109 0 6
142585114 0 3
142585116 0 1
142585120 0 4
142585197 0 1,3,4,5,6
142585294 0 4
142585317 0 1
I used a ListAgg query to do the aggregation which works correctly but when I tried to incorporate this into an update query, I end up with multiple duplicates in the aggregated field for each record.
Here is my update attempt:
update itemtable dd
set aggregateddivisions = (SELECT Listagg(division, ',') within GROUP (ORDER BY division)
FROM itemtable ev
WHERE ev.itemid = dd.itemid
)
where exists (select 1
from itemtable ev
where ev.itemid = dd.itemid
);
How can I update the table with the aggregated list of values from the same table without ending up with duplicates?
I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)
Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:
I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.
My Database Table Contain Records Like this:
ID ProductId Occurences
1 1 1
2 2 5
3 3 3
4 4 3
5 5 5
6 6 8
7 7 9
Now i want to get top 4 ProductId with the highest Occurences.
This is my Query:
var data = (from temp in context.Product
orderby temp.Occurences descending
select temp).Take(4).ToList();
Now here as because ProductId 2 and 5 have same occurences and ProductId 3 and 4 also have same occurences then here I am not getting that how to resolve this means which product id should i take as because they are having same occurences.
Basically i am selecting this productid to display this products on my website.i will display those products which are return by this query.
So can anyone please give me some idea like how to resolve this ???
Expected Output:
ID ProductId Occurences
1 7 9
2 6 8
so Now for 3rd position Which ProductId i should select as because both ProductId 2 and 5 have same Occurences and for 4th position which ProductId i should select among 3 and 4 as because they both too have same occurences.
can I suggest you to use group by technique and pick the first one
context.Product.GroupBy(p => p.Occurance)
.Select(grp => grp.FirstOrDefault())
.Take(4)ToList();
Sorry, I have not tested this but it should do the job. You just need to add OrderBy along with it.