Add order column into query result in SQL Server - sql

I have a table, called Table1 that looks like:
id|val1|val2|FKId
------------------
1|val |val | 123
2|val |val | 123
3|val |val | 234
4|val |val | 345
I'm using:
SELECT * FROM Table1
I need to add to the result of this query, a column that will contain the order number for each row by FKId. Result that I need looks like:
id|val1|val2|FKId |Order
------------------------
1|val |val | 123 | 1
2|val |val | 123 | 1
3|val |val | 234 | 2
4|val |val | 345 | 3
Maybe there is some function in SQL Server to get this?

Using Ranking function Dense_Rank() you can get the required order. To know more about Dense_rank check here.
SELECT *,
Dense_Rank()
OVER (
PARTITiON BY val1
ORDER BY fkid) [Order]
FROM tablename

Related

SQL sum all rows by id

I am trying to learn SQL queries and have this scenario where I have this table:
Table1
ID | Name | Hour
----------------
1 | Mark | 2
2 | ken | 1.5
3 | jake | 3
1 | Mark | 1.8
2 | ken | 1
Expected result
ID | Name | Hour
----------------
1 | Mark | 3.8
2 | ken | 2.5
3 | jake | 3
I have tried to use the sum() function but I get an error.
My query:
Select ID, Name, Sum(Hour)
From Table1
Where ID = ID
Response:
Kindly use Group by clause whenever the Aggregate functions (min(),max(),sum(),count(),...etc.,) and columns are used together.
Non aggregated columns present in SELECT columns should be used in GROUP BY clause.
For using aggregate function you need to use Group By like this:
Select ID, Name , Sum(Hour) AS Hour From Table1
Group By ID, Name
Order By ID

SQL - How to do something like value.Contains?

someone can help me, I need to exclude some repeated values, the result is:
There are some rows with null values and in that case I named 'No Informado'.
In line from 26 to 32 there is the same value1 and value2, but value3 is different.
I will need this result,
id | name | user
0x00E281759429DD4B807F467F8B2319E3 | PC_XBPOX0112 | llopez
0x00F37F5DA2C8854699EFBA30F7102DDD | PC_BSCTY1312 | No Informado
0x00F53DBE60CFF343942E3893ABA809EB | PC_SVCTY6834 | ntapia
0x00FDB75C00B8D84E8A1862A56C71A766 | NB_TSCTY06606 | jogonzalez
0x010029519191B34BB498E7F9FEAE3E21 | PC_BSCTY3229 | kfuentes
0x011506756396BC4588E705BFCFA84847 | PC_BSCTY3134 | csepulveda
0x0120BE537B242C4EB01C4F94E82E64BF | PC_BSCTY1296 | eaviles
0x01322ABEC4F19E41B2139291952838EE | PC_VSCTY6535 | vbravo
0x0133C6B80B50E44A928AF770510856E3 | PC_FSCTY0084 | mcarreno
0x01463ECF32DEBD41943330EC7C1822D4 | PC_BSCTY3220 | fegonzalez
0x01610C718C04264A8349FAEA6676363F | PC-FSCTY0543 | fcastro
someone can help me?
Forward thanks!
Another option is the WITH TIES clause in concert with Row_Number()
Example
Select Top 1 With Ties *
From YourTable
Order by Row_Number() over (Partition By ID Order by Date Desc)
Returns
id name date
1 name1 2018-01-01
2 name2 2018-01-01
3 name5 2018-02-01
SELECT Id
, MAX(name) AS Name
, MAX([date]) AS [date]
FROM TableName
GROUP BY Id

T-SQL: I want to get result in like in second screenshot

[strong text][3]
This is my problem case in which we have have to group partner ID on the basis of thire and and assign them a rank ID.
Here some rank ID have same group count and same partnerid we want to identify them and remove them.
Desired result should be like this:
You want to select the distinct rows?
select distinct groupcount, partnerid
from tbl
test setup:
create table tbl (RankId int, GroupCount int, PartnerId int);
insert into tbl values
(1,2,316),(1,2,329)
,(2,2,316),(2,2,329)
,(3,3,316),(3,3,329),(3,3,319);
select distinct GroupCount, PartnerId
from tbl;
rextester demo: http://rextester.com/CXTE98698
returns:
+------------+-----------+
| GroupCount | PartnerId |
+------------+-----------+
| 2 | 316 |
| 2 | 329 |
| 3 | 316 |
| 3 | 319 |
| 3 | 329 |
+------------+-----------+

SQL : Getting duplicate rows along with other variables

I am working on Terradata SQL. I would like to get the duplicate fields with their count and other variables as well. I can only find ways to get the count, but not exactly the variables as well.
Available input
+---------+----------+----------------------+
| id | name | Date |
+---------+----------+----------------------+
| 1 | abc | 21.03.2015 |
| 1 | def | 22.04.2015 |
| 2 | ajk | 22.03.2015 |
| 3 | ghi | 23.03.2015 |
| 3 | ghi | 23.03.2015 |
Expected output :
+---------+----------+----------------------+
| id | name | count | // Other fields
+---------+----------+----------------------+
| 1 | abc | 2 |
| 1 | def | 2 |
| 2 | ajk | 1 |
| 3 | ghi | 2 |
| 3 | ghi | 2 |
What am I looking for :
I am looking for all duplicate rows, where duplication is decided by ID and to retrieve the duplicate rows as well.
All I have till now is :
SELECT
id, name, other-variables, COUNT(*)
FROM
Table_NAME
GROUP BY
id, name
HAVING
COUNT(*) > 1
This is not showing correct data. Thank you.
You could use a window aggregate function, like this:
SELECT *
FROM (
SELECT id, name, other-variables,
COUNT(*) OVER (PARTITION BY id) AS duplicates
FROM users
) AS sub
WHERE duplicates > 1
Using a teradata extension to ISO SQL syntax, you can simplify the above to:
SELECT id, name, other-variables,
COUNT(*) OVER (PARTITION BY id) AS duplicates
FROM users
QUALIFY duplicates > 1
As an alternative to the accepted and perfectly correct answer, you can use:
SELECT {all your required 'variables' (they are not variables, but attributes)}
, cnt.Count_Dups
FROM Table_NAME TN
INNER JOIN (
SELECT id
, COUNT(1) Count_Dups
GROUP BY id
HAVING COUNT(1) > 1 -- If you want only duplicates
) cnt
ON cnt.id = TN.id
edit: According to your edit, duplicates are on id only. Edited my query accordingly.
try this,
SELECT
id, COUNT(id)
FROM
Table_NAME
GROUP BY
id
HAVING
COUNT(id) > 1

... where count(col) > 1

I have a table like this:
+-----+-----+-------+
| id | fk | value |
+-----+-----+-------+
| 0 | 1 | peter |
| 1 | 1 | josh |
| 3 | 2 | marc |
| ... | ... | ... |
I'd like now to get all entries which have more than one value.
The expected result would be:
+-----+-------+
| fk | count |
+-----+-------+
| 1 | 2 |
| ... | ... |
I tried to achieve that like this:
select fk, count(value) from table where count(value) > 1;
But Oracle didn't like it.
So I tried this...
select * from (
select fk, count(value) as cnt from table
) where cnt > 1;
...with no success.
Any ideas?
Use the having clause for comparing aggregates.
Also, you need to group by what you're aggregating against for the query to work correctly. The following is a start, but since you're missing a group by clause still it won't quite work. What exactly are you trying to count?
select fk, count(value)
from table
group by fk
having count(value) > 1;