Consider a Sample Table with two Column RoleId and User Name
Role | Name
1 AB
3 A
1 ABC
2 D
2 B
3 Abb
1 E
4 TE
How can i use SQL queries to get following Output.
Role | Name
3 A
3 Abb
1 AB
1 ABC
1 E
2 B
2 D
4 TE
I just want to Order by Role Id 3 first then by remaining Roleid.
Currently i am using Union to achieve so //
SELECT * FROM (SELECT * From #temp
Where roleid=3
UNION ALL
SELECT * From #temp
Where roleid != 3
) as X
You can use case to make more complex ordering:
select *
from #temp
order by case when Role = 3 then 0 else 1 end, Role, Name
select *
from #temp
order by CASE WHEN Role = 3 THEN 0 ELSE Role END, Name
I usually use NULLIF, but case might be faster?
SELECT *
FROM #temp
ORDER BY NULLIF(Role,3), Name
Related
How to select the value from the table based on category_id?
I have a table like this. Please help me.
Table A
ID Name category_id
-------------------
1 A 1
2 A 1
3 B 1
4 C 2
5 C 2
6 D 2
7 E 3
8 E 3
9 F 3
How to get the below mentioned output from table A?
ID Name category_id
--------------------
1 A 1
2 A 1
4 C 2
5 C 2
7 E 3
8 E 3
Give a row number for each row based on group by category_id and sort by ascending order of ID. Then select the rows having row number 1 and 2.
Query
;with cte as (
select [rn] = row_number() over(
partition by [category_id]
order by [ID]
), *
from [your_table_name]
)
select [ID], [Name], [category_id]
from cte
where [rn] < 3;
Kindly run this query It really help You Out.
SELECT tbl.id,tbl.name, tbl.category_id FROM TableA as tbl WHERE
tbl.name IN(SELECT tbl2.name FROM TableA tbl2 GROUP BY tbl2.name HAVING Count(tbl2.name)> 1)
Code select all category_id from TableA which has Name entries more then one. If there is single entry of any name group by category_id then such data will be excluded. In above example questioner want to eliminate those records that have single Name entity like wise category_id 1 has name entries A and B among which A has two entries and B has single entry so he want to eliminate B from result set.
I have two tables and i want to get the difference between them based on the name and the version of the book:
Table 1
id Name version
1 B5077 A
2 B5077 A
4 B5077 B
5 B5077 C
Table 2
id name version
1 B5077 B
2 B5077 C
3 B5077 D
4 B5077 E
SQL command (the result is really fast comparing to a full Join):
( SELECT name, version FROM table 1 where book = 'B5077'
EXCEPT
SELECT name, version FROM table 2 )
UNION ALL
( SELECT name, version FROM table 2 where book = 'B5077'
EXCEPT
SELECT name, version FROM table 1)
It gives me this output:
id name version
1 B5077 A
2 B5077 D
3 B5077 E
but how i can get the id of the line that has the difference and in which table ? so i can have something like this :
id name version idtable1 idtable2
1 B5077 A 1 NULL
2 B5077 A 2 NULL
3 B5077 D NULL 3
3 B5077 E NULL 4
Thanks,
Just select first the rows on Table 1 not present on Table 2, and then add the rows on Table 2 not present on Table 1.
select Name, Version, id as idtable1, null as idtable2
from Table1
where not exists (select * from Table2 where Table2.Name = Table1.Name and Table2.Version = Table1.version)
union
select Name, Version, null as idtable1, id as idtable2
from Table2
where not exists (select * from Table1 where Table1.Name = Table2.Name and Table1.Version = Table2.version)
I want to Update only top 1 OR only 1 row of a column where a column values are same.
(Just logical explanation don't go on syntax)
LIKE:
Update [Total] = (value from a another table with a common column)
but need to update only top 1 row OR any one row to the current (updating) table not all rows matching column value...
e.g
Table 1:
Skill Value
abc 3
def 4
xyz 3.5
Table 2:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3
shyam abc 4
Mohan abc 5
Raju xyz 4
Ratan xyz 6
Now I want to Update MyValue2 based on Table1 column Skill Value = MyValue2 but I want to update anyone OR top 1 row in Table2 NOT ALL Please help
Expected Output:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 3
shyam abc 4
Mohan abc 5
Raju xyz 4 3.5
Ratan xyz 6
OR Alternate output can be:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 Value from Table1 / no. of records with skill abc (3/3)
shyam abc 4
Mohan abc 5
Raju xyz 4 Value from Table1 / no. of records with skill xyz (3.5/2)
Ratan xyz 6
In Table 2, give a row number based on group by Skill column and order by MyValue column. And then updated the rows which having row number1 with Value from Table 1.
Query
;with cte as(
select [rn] = row_number() over(
partition by Skill
order by [MyValue]
), *
from [Table2]
)
update t1
set t1.[MyValue2] = t2.[Value]
from cte t1
join [Table1] t2
on t1.[Skill] = t2.[Skill]
where t1.[rn] = 1;
In my SQl server Sp.
`SELECT rating as [Rating],count(id) as [RatingCount]
FROM MMBPollResults
where mmb_id = #MMbid
GROUP BY rating
This SP returns the rating for each user.
i:e rating ratingcount
` 1 2
2 1
5 4
So this means that
2users have rated the transaction with 1star
1 user has rated the transaction with 2stars
4 users have rated the transaction with 5stars
This is how I need the output
rating ratingcount
` 1 2
2 1
3 0
4 0
5 4
Sorry, if this is a silly question
Thanks
Sun
You need a table with 1 to 5. This could be a number table or some other rating table.
Here I use a simple UNION to make a table with 1 to 5
SELECT
List.Rating,
count(MMB.*) as [RatingCount]
FROM
(
SELECT 1 AS Rating
UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
) List
LEFT JOIN
MMBPollResults MMB ON List.Rating = MMB.Rating AND MMB.mmb_id = #MMbid
GROUP BY
List.Rating
ORDER BY
List.Rating;
I have a table like this:
id name parent_id
1 ab1 3
2 ab2 5
3 ab3 2
4 ab4 null
5 ab5 null
6 ab6 null
I need to do a query with input id = 1 (for an example) and results will be like this:
id name parent_id
5 ab5 null
2 ab2 5
3 ab3 2
1 ab1 3
(List all parents at all level start at item id = 1)
Something like this perhaps?
WITH parents(id,name,parent,level)
AS
(
SELECT
ID,
NAME,
PARENT,
0 as level
FROM
TABLE
WHERE ID = 1
UNION ALL
SELECT
ID,
NAME,
PARENT,
Level + 1
FROM
TABLE
WHERE
id = (SELECT TOP 1 parent FROM parents order by level desc)
)
SELECT * FROM parents
Use WITH RECURSIVE. Documentation and adaptable example: Recursive Queries Using Common Table Expressions.