i have a tabel with columns as so..
column1 column2 column3 column4 column5
i want the datas to fill like below in column two..
where colum1,3,4,5...user will give the values...
in column2 its values should be auto sequencing like as follows...
column1 column2 column3 column4 column5
1 1 one one one
1 2 two two two
1 3 three three three
2 1 one one one
2 2 two two two
2 3 three three three
3 1 one one one
3 2 two two two
3 3 three three three
Folks kindly Give ur Suggestions...Thankxxx!!! cya!
It seems you want to give each row a distinct value for equal values in column1. This can be done with a windowing function:
SELECT column1,
row_number() over (partition by column1) as column
FROM some_table
I do not understand what you want to have in columns3, 4 and 5
Depending on your platform, the following May work...
UPDATE
<your-table>
SET
column2 = ROW_NUMBER() OVER (PARTITION BY column1)
EDIT
As Martin says in his comment...
WITH
windowed_table
AS
(
SELECT
column2,
ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) AS row_number
FROM
my_table
)
UPDATE
windowed_table
SET
column2 = row_number
Related
I have a below table like this -
Policy Column1 Column2
A 4 100
B 4 100
C 3 100
D 3 100
E 2 100
F 5 100
The Output should be
Policy Column1 Column 2
E 2 100
F 5 100
Can someone please guide me.
Assuming you're after rows where the value in column1 is unique (i.e. no other row has that same value in column 1)
select max(Policy) Policy -- if we're just getting 1 row, the max is the only value
, Column1
, max(Column2) Column2 -- as above
from myTable
group by Column1 -- group by column 1 then
having count(1) = 1 -- count how many rows are in that group; if it's unique we get 1
Select * from policies
where Column1 in (select Column1
from policies
group by Column1
having count(*)=1);
DBFiddle demo is here
You want to divide the data into chunk via partition by. Then you can determine which of them has only a single row.
with data as (
select *, count(*) over (partition by Column1, Column2) as cnt
from T
)
select Policy, Column1, Column2 from data where cnt = 1;
This kind of query was harder to write twenty years ago. If you're learning SQL I'd encourage you to get a handle on some of the foundational concepts before diving into table expressions and analytic functions.
The table looks like
column1 column2 column3
400196 2021-07-06 33
400196 2021-07-06 33
400196 2021-08-16 33
I want to get the sum of column3 values based on grouping of column 1 but the duplicate values of date should not be added
The desired output is:
column1 column3
400196 66
The query I wrote is
select sum(column3)
from table_name
group by column1
But this gives me result 99
You can remove duplicate values in a subquery:
select t.column1, sum(t.column3)
from (select distinct t.column1, t.column2, t.column3
from t
) t
group by t.column1;
Note: This sort of problem can arise when you are joining tables together. Removing duplicates may not always be the right solution. Often it is better to do the calculation before joining, so you don't have duplicate values to deal with.
You could use a two step process here, first remove duplicates, then aggregate and sum:
SELECT column1, SUM(column3) AS column3
FROM (SELECT DISTINCT column1, column2, column3 FROM yourTable) t
GROUP BY column1;
Demo
Let's say I have table like this:
Column1 Column2
C 2
B 1
A 3
I need to exchange values in the second column to get this:
Column1 Column2
C 3
B 2
A 1
The goal is only for numeric column to have values sorted to follow alphabetical order on another column. The actual table has multiple columns and column 1 is people's name, while column 2 two is rank for rendering column 1 values in UI.
What is the most optimal way to do this?
I am doing this from C# code, on SQL server and have to use System.Data.SqlClient.SqlCommand because of transaction. But maybe it's not important if this can all be done from SQL.
Thank you!
So you need to update Column2 with the row-number according toColumn1?
You can use ROW_NUMBER and a CTE:
WITH CTE AS
(
SELECT Column1, Column2, RN = ROW_NUMBER() OVER (ORDER BY Column1)
FROM MyTable
)
UPDATE CTE SET Column2 = RN;
This updates the table MyTable and works because the CTE selects a single table. If it contains more than one table you have to JOIN the UPDATE with the CTE.
Demo
I have a table like this:
Column1 Column2
---------------
1 1
1 2
1 3
1 4
2 1
2 1
2 1
2 1
In column1 one there are 2 different ids, in column2 there are different values for each id from column1.
How can I get the id from column1 where not all ids from column2 are the same? So in this instance the output should be 1 - because they have all different values in column2, where id from column1 has all 1's in column2
Just use group by and having:
select column1
from table t
group by column1
having min(column2) <> max(column2);
Note: you could also use count(distinct), but that has more overhead than min() and max().
Similar logic can be used if the second column could be NULL. That doesn't appear in the sample data so it doesn't seem worth including it in the logic unless the OP specifically says this is a possibility.
Try like this:
select Column1
from yourTable
group by Column1
having count(DISTINCT column2) > 1;
I would think something like this should do the job:
SELECT t.column1 FROM table t
GROUP BY t.column1
HAVING COUNT(DISTINCT t.column2) > 1
This approach will handle the case where a null is an acceptable value in column2.
select column1
from
(
select distinct column1, column2
from yourTable
) t
group by column1
having count(*) > 1
I'm trying to isolate a problem with a violation of a unique key index. I'm pretty certain that the cause is resulting from columns that have the same value in 3 columns not having the same value in the 4th (when they should). As an example...
Key Column1 Column2 Column3 Column4
1 A B C D
2 A B C D
3 A B C D
4 A B C Z
I basically want to select column 4, or some way to let me identify column 4. I know it's a matter of using aggregrate functions but I'm not very familiar with them. Can anyone assist on a way to select Key, Column4 for rows that have a different column 4 value and the same column 1-3 values?
This is what you want:
select column1, column2, column3
from t
group by column1, column2, column3
having min(column4) <> max(column4)
Once you get the right values for the first three columns, you can join back in to get the specific rows.
Or, you can use window functions like this:
select t.*
from (select t.*, min(column4) over (partition by column1, column2 column3) as min4,
max(column4) over (partition by column1, column2 column3) as max4
from t
) t
where min4 <> max4;
If NULL is a valid "other" value that you want to count, you will need additional logic for that.
If you want to get all columns, then (it could be simpler if windowed count supported distinct but it's not):
with cte1 as (
select distinct * from Table1
), cte2 as (
select
*,
count(column4) over(partition by column1, column2, column3) as cnt
from cte1
)
select * from cte2 where cnt > 1;
if you want just to select key:
select
column1, column2, column3
from Table1
group by column1, column2, column3
having count(distinct column4) > 1
sql fiddle demo