1 or 0 Per Group - sql

Re-edited to make it clearer:
I would like my results to appear as they do in 'Column b' based on 'Column a' groupings? so a 1 or 0 per group based on column a. Column b does not exist currently, I am trying to code this in. I was trying to use row_number or rank but this not appear to work for me. So how do I write my SQL so I can get my SQL results to mirror Column b? Any help is appreciated
Thank - you
column a
column b
aaa
1
aaa
0
ddd
1
ddd
0
ddd
0
yyy
1
yyy
0
yyy
0

You just need to wrap your row_number() in a case, something like this:
select
column_a
case row_number() over (partition by column_a)
when 1 then 1
else 0
end as column_b
from
table
/

Related

Ignoring records for certain criteria

I have data as below,
ACCOUNT
FLAG
asdf
1
asdf
2
asdf
3
kjhj
1
qwer
1
qwer
1
need to get output:
ACCOUNT
FLAG
kjhj
1
qwer
1
situation is that need to get records that have only "1" in 2nd column. If they have any other value other than "1", need to ignore all records for particular 1st column.
can you plz suggest a query
tried group by but didn't find option
Group to a single account per output row, then assert that all rows in a group must have flag=1 by using HAVING with both min and max.
SELECT
account,
MIN(flag) flag
FROM
your_table
GROUP BY
account
HAVING
MIN(flag) = 1
AND MAX(flag) = 1
Some people prefer the following and being more understandable, and it also causes a NULL row to exclude the group...
HAVING
MIN(CASE WHEN flag=1 THEN 1 ELSE 0 END) = 1

Is there a way to count the number of values in column?

I am a newbie studying MSSQL and Database.
I am trying to write a SQL query to count values in the column.
Following table is original one.
name value
----------
A 1
A 1
A 2
B 1
B 2
I want to get a table like this.
name one two
--------------
A 2 1
B 1 1
A has two 1 and one 2 and B has one 1 and 2. It seems I can accomplish it using COUNT built-in function. I tried but failed. Is there any idea to do it?
use conditional aggregation
select name, sum(case when value=1 then 1 else 0 end) as one,
sum(case when value=2 then 1 else 0 end) as two
from table_name group by name

how can i alternate between 0 and 1 values in sql server?

I want to create a select which will alternate between 1 and 0
my table looks like that
id1 id2 al
11 1 1
40 1 0
12 1 0
237 1 1
but I want to make it like that
id1 id2 al
40 1 0
11 1 1
12 1 0
237 1 1
I want to keep the same values in my table but I just want to switch the rows to alternate between 0 and 1
Consider:
select *
from mytable
order by row_number() over(partition by al order by id1), al
This alternates 0 and 1 values - if the groups have a different number of rows, then, once the smallest group exhausts, all remaining rows in the other group appear at the end of the resultset.
I am unsure which column you want to use to order the rows within each group - I assumed id1, but you might want to change that to your actual requirement.

How to arrange Sql rows in a specific format

I have table with up to 50 rows... like given below.
ID menu dispOdr ParntID
---------------------
1 abc 1 0
2 cde 2 0
3 fgh 1 2
4 ghdfdj 2 2
5 tetss 1 1
6 uni 3 0
but I want to be sorted
Like
ID menu dispOdr ParntID
---------------------
1 abc 1 0
5 tetss 1 1
2 cde 2 0
3 fgh 1 2
4 ghdfdj 2 2
6 uni 3 0
If have any query please let me know.. thanks in advance.
I am using sql server 2014
I think you need your current vs. desired output reversed. You say you want the menu column sorted, but it appears that it already is.
So assuming you are actually starting with the second table, you can sort the menu column simply using ORDER BY:
SELECT *
FROM mytable
ORDER BY menu ASC
I think that the query below produces the required output:
SELECT t1.ID, t1.menu, t1.dispOdr, t1.ParntID
FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.ParntID = t2.ID
ORDER BY CASE
WHEN t1.ParntID = 0 THEN t1.dispOdr
ELSE t2.dispOdr
END,
CASE
WHEN t1.ParntID = 0 THEN 1
ELSE 2
END,
t1.dispOdr
The first CASE expression groups records according to the dispOdr of their parent. The second CASE places parent on the top of its subgroup. Finally, the last expression used in the ORDER BY clause orders all child records within a subgroup.
Note: The above query works with one level of nesting.

Update Duplicate Records

I have a column store table, TABLE_HANA with the below data in HANA DB:
T_ID1 T_ID2 T_DESC T_RN
1 1 AAA ?
1 1 BBB ?
1 2 CCC ?
1 3 DDD ?
1 3 EEE ?
I need to group them by T_ID1 and T_ID2 and update the T_RN column of that group with a sequential value as shown below:
T_ID1 T_ID2 T_DESC T_RN
1 1 AAA 1
1 1 BBB 2
1 2 CCC 1
1 3 DDD 1
1 3 EEE 2
Since the table is very huge(millions of records), I am looking for a solution with good performance. Please suggest.
TIA
The first question is whether you really need to do the update. The following query returns what you want:
select h.*,
row_number() over (partition by t_id1, t_id2 order by t_desc) as t_rn
from table_hana h;
This may be sufficient.
The update will probably be expensive:
update table_hana h
set t_rn = (select count(*)
from table_hana h2
where h2.t_id1 = h.t_id1 and h2.t_id2 = h.t_id2 and h2.desc <= h.desc
);
An index on table_hana(t_id1, t_id2, t_desc) should help both these queries.