Procedure in T SQL for collecting historical data [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 14 hours ago.
Improve this question
I need help with writing a stored procedure in T-SQL. I have some transactional table which is about Sales Opportunities. Opportunities are created by the system automatically.
In one opportunity there can be few items - let's say products A, B and C. That means that there is no guarantee that OpportunityId is unique identifier. Every Opportunity has also end date - it is date when we will know if opportunity ended with success or defeat.
I need to have a stored procedure which will add a new opportunity with all data when it is created and then this procedure should check on end date what was final state of that procedure - sold or not sold and what were items inside.
It should look like that:
OpportunityId
ProduktId
EndDate
State
1
A
2022-03-20
Wait
1
B
2022-03-20
Wait
1
A
2022-03-20
Sold
1
B
2022-03-20
Sold
1
C
2022-03-20
Sold
So first two rows are "beginning state" when creating new chance, rows 3, 4, 5 are how it ended.
I'm really struggling with this, guys. Please help me

Related

How to fetch data using where clause in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
There is no error, but the results show the values excluding the row that meets both conditions. The result shows 3, in fact, it should be 4 excluding data that has 22 for age and UK for the country. Any idea why the result is 3, not 4?
SELECT COUNT(*) FROM CUSTOMERS WHERE CUSTOMERS.AGE = '22' OR CUSTOMERS.COUNTRY = 'UK';
You have two customers at the age of 22 and 2 residing in the UK.
This is actually a correct result here - how on earth do you come to the conclusion that you have four people meeting that criteria? If more than one point matches for a database entry, it will only count as one for the result, since it's still the same database entry.

Compare the same field on the same table for the same stock code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the following Table:
StockCode Bin
123 L02A3
123 L08B2
123 L08C2
456 L04B2
456 L02C1
789 L19A2
When looking at a Bin for a stock code, any bin that contains an "A" is on the floor level of the warehouse. If the bin contains a "B", it is on the middle rack of the warehouse, and if the bin contains a "C", it is on the top rack of the warehouse.
I would like a sql query that compares all the bins for each stock code and shows which stock codes do not have a current bin on the "A" level.
From the example table above, stock code "456" would show because it is the only stock code that does not have inventory in an "A" bin.
This will be very helpful because it will flag us to which inventory to move down to the floor level before the pick tickets are printed.
Thank you in advance for any help!
Think group by and having:
select stockcode
from t
group by stockcode
having sum(case when bin like '%A_' then 1 else 0 end) = 0;
The having clause counts the number of rows for each stock code where the bin has an A (this assumes that the penultimate letter is an A, as in the examples in the question). If the number is 0, then the stock code is returned.

SQL Server 2000: how to replace a entry with only the last 2 characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a table in a SQL Server 2000 database with a column with several entries like 01-03, 04-05. The first 2 digits are the room number and the last two digits are the bed number.
I want to replace the 01-05, 03-07 etc. with only the bed number. So only the last two charcters have to be in the column.
How do I accomplish this?
Sample data is:
In the column [Bed] with values:
22-01
08-01
09-03
01-16
Result has to be:
01
01
03
16
RIGHT should solve your problem:
SELECT RIGHT(RTRIM(Bed), 2) FROM tbl

update filed in a table recursively [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following table test
id name formula
--------------------------------
1 A aa+bb+cc
2 aa e+f+g
3 e b
4 f t
5 g 5
How can I update the filed formula to get something like that
id name formula
--------------------------------
1 A b+t+5+bb+cc// update aa=b+t+5
2 aa b+t+5//at first update formula which has id =2
3 e b
4 f t
5 g 5
Your question is too complex. You need to solve several problems before you can actually update anything. You need to be able to:
decompose a formula so that you can get a list of all the operands (do you have to do that in SQL, though?);
distinguish between a reference operand and a constant value (or function?) operand;
determine which reference operands are valid references (exist in the table);
determine the order in which the references must be evaluated.
I suggest you seek help on these problems separately, because Stack Overflow is not a good fit for overly complex questions. (Do try solving them yourself first, though.)

Django get all rows with same value in column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how can I filter in Django to get a data row one times if the same value in on column?
columns:
a | b
x | y
a | y
y | s
Want one data row set with y (b) and one data row set with s (b).
If not clear what I mean I edit with SQL script...
EDIT: SELECT DISTINCT b FROM table;
Sorry to be blind...
http://docs.djangoproject.com/en/dev/ref/models/querysets/
Have a nice day.