SQL select statement to Exclude a column from being sorted [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 7 years ago.
Improve this question
I am looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help

This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name

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;

query to check multiple revsions on a table [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 5 years ago.
Improve this question
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.

How can I count repeat rows in SQL Server 2012? [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 want to assign one unique number for each repeated row in select statement.
table
Name | Position
Mark Wol 1
Mark wol 1
MArtha 1
Martha 1
and I want this
1 Mark Wl 1
2 Martha 1
You can use as few or as many rows as you want. If you want to use all of the rows, list them all.
SELECT Row1, Row2, Row3,
ROW_NUMBER() OVER (
PARTITION BY Row1, Row2, Row3,
ORDER BY Row1
)
FROM Table
The best solution is to already have a unique ID for each record and then you are choosing to show that (which will come from the first found record)...
SELECT name,COUNT(*),id
FROM test_table
GROUP BY name
HAVING COUNT(*) > 1

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.)