Count by ID if value is lower than and set the value 1 [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 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.

This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

Related

Auto increment for duplicate rows [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 days ago.
Improve this question
I have a table with column name No, which have duplicate values (which is necessary). I need to add an incrementing value in another column sub_no
For Example:
No
sub_no
1
0
1
1
1
2
2
0
2
1
3
0
3
1
3
2
3
3
I have tried update and select queries.
A window function will give you the results you want:
SELECT [No], ROW_NUMBER() OVER(PARTITION BY [No] ORDER BY [No]) - 1 AS
[sub_no]
FROM <table_name>;
You can find more information on window functions in T-SQL here.

How to do running subtraction 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 2 years ago.
Improve this question
I know how to do running sum but can't figure out how to do running subtract.
Example: we have single column of data and a new column will be formed for running subtraction.
Value
------
10
1
3
4
Output needs to be:
10
9
6
2
I need both these columns next to each other
Any suggestion please.
Your question only makes sense if you have a column that specifies the ordering. In that case:
select (case when row_number() over (order by <ordercol>) = 1
then col
else 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>)
end) as output
from t;
That is, return the column value on the first row. Otherwise, the math is a little tricky. But you want the first value minus the sum of the rest of the column. Arithmetically, this is the same as twice the first value minus the cumulative sum.
EDIT:
As Shawn points out, this can be simplified to:
select 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>) as output
from t;

Calculate BACKLOG_M_1 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I need to calculate the BLACKLOG_M value corresponding to the Snapdate of the first of the month. It becomes a constant value repeated for each line of the month
As it is explained in the picture:
SELECT T1.SNAP, T1.PERI, T1.ANNEE, T1.MOIS, T1.[BCKL], T2.[BCKL] AS BCK2,[NBR1]
,[NBR2]
FROM stg.FACTSALES AS T1 LEFT OUTER JOIN
(SELECT YEAR(SNAP) AS ANNEE, MONTH(SNAP) AS MOIS, [BCKL]
FROM stg.FACTSALES
WHERE (DAY(SNAP) = 1)) AS T2 ON T2.ANNEE = T1.ANNEE AND T2.MOIS = T1.MOIS
The script returns the correct values, but is there any another best idea or improvement?
You want to copy the value from the first day of the month through the rest of the month. That should be possible.
select fs.*,
(case when day(snapdate) = 1
then max(case when day_snapdate() = 1 then backlog_m end) over (partition by year_month)
end) as backlog_m_1
from stg.FACTSALES fs;
This assigns the value on the first day as well. It is not clear what you want for that value.

SQL: Generating a field based on a field [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 a table (for the sake of the exercise let's call it Persons)
I am trying to create a query that will generate the CrDRN number.
The CrDRN column should contain the DRN number of the last record that has a VoucherType = 80
In this instance the CrDRN column would have the values 1 1 1 5 5
Any help would be greatly appreciated.
The CrDRN column should contain the DRN number of the last record that
has a VoucherType = 80
Another way of saying that would be to get the max value of DRN where DRN is less than or equal to the current DRN and where VoucherType is 80.
You can formulate just that in a subquery that returns CrDRN.
select T1.DRN,
T1.VoucherType,
(
select max(T2.DRN)
from T as T2
where T2.DRN <= T1.DRN and
T2.VoucherType = 80
) as CrDRN
from T as T1

How to get results in a custom order? [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.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Instead of ASC or DESC, I want my query results to be in a specific, custom order.
For example, instead of A, B, C, D..., what if I wanted my results in, P, A, L, H...?
I have tried using case but not successfully
SELECT * FROM Customers
ORDER BY case country
when 'P' then 1 …
E.g., here, I'm trying to create a custom order on the Country column:
SELECT * FROM Customers
ORDER BY case when country = 'P' then 1
when country = 'A' then 2
when country = 'L' then 3
when country = 'H' then 4
else 5
end asc