I'm facing this problem - I have this kind of select result:
col1 col2 col3
5 95.91.232.198 1
8 95.91.232.198 1
9 95.91.222.206 5
152 95.91.222.206 1
25 95.91.204.108 5
5 95.91.204.108 5
column3 can have only 2 values: 1 or 5
I want to select only those rows which have different numbers in column3 with duplicated ip - example (what should be selected):
this should be selected
127.0.0.1 1
127.0.0.1 5
and this not
127.0.0.1 1
127.0.0.1 1
Appreciate any help.
With EXISTS:
select t.* from tablename t
where exists (
select 1 from tablename
where col2 = t.col2 and col3 <> t.col3
)
Or if you want only the ips:
select col2
from tablename
group by col2
having count(distinct col3) = 2
You can use DISTINCT keyword to select only unique combinations of values.
Starting data:
col1 col2 col3
5 95.91.232.198 1
8 95.91.232.198 1
9 95.91.222.206 5
152 95.91.222.206 1
25 95.91.204.108 5
5 95.91.204.108 5
Example:
SELECT DISTINCT col2, col3
FROM [table]
Output:
col2 col3
95.91.232.198 1
95.91.222.206 5
95.91.222.206 1
95.91.204.108 5
Related
I have this table:
col1
col2
1
2
1
1
2
-2
3
1
3
-1
I only want to have a results of distinct col1 which having only col2 are ALL negative.
Which means I want to have only see col1 = 2
How to do that?
SELECT col1 from table1 t
group by t.col1
having max(t.col2) < 0
you can select just col1 from your table where the max value of col2 is less than 0 after grouping by col1.
Example: http://sqlfiddle.com/#!9/08ed9b/25/0
I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.
Example:
ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4
Current Output:
ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15
Desired Output:
ID | Value
-------------
1 | 10
1 | -10
I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.
select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , *
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])
Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:
DROP TABLE IF EXISTS #sopg;
SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;
-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID
SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID
-- Another way, assuming that ID can have more than 2 rows and different values
DROP TABLE IF EXISTS #sopg2;
SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x
SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE
I am using SQL server 2012 . I have table like
col1 col2 col3
1 abc AA
2 xyz BB
3 def CC
I want to convert this table into
col1 col2 col3_AA col3_BB col3_CC
1 abc AA
2 xyz BB
3 def CC
please edit table format. I am not able to do
The conditional aggregation might help you
select col1, col2,
max(case when col3 = 'AA' then col3 end) col3_AA,
max(case when col3 = 'BB' then col3 end) col3_BB,
max(case when col3 = 'CC' then col3 end) col3_CC
from table
group by col1, col2
order by col1
Try This
1. Static PIVOT query.
For example, if the column "ITEM_HEAD_ID" values can only be like 1, 2 and 3, then this is what you need. According to your given data, you can use the following query:
SELECT *, (ISNULL([1], 0 ) + ISNULL([2], 0) + ISNULL([3], 0)) AS [Total]
FROM (SELECT [TRXID],
[ITEM_HEAD_ID],
[ITEM_HEAD_AMT]
FROM [Table]) AS t
PIVOT (MAX([ITEM_HEAD_AMT])
FOR [ITEM_HEAD_ID] IN ([1], [2], [3])) AS p;
Note: [Table] is the name of your table.
The result:
TRXID 1 2 3 Total
6 100.00 100.00 300.00 500.00
7 100.00 100.00 300.00 500.00
I have table like this
Id | Name | Status
------+------------+--------------
1 example1 3
1 example2 2
2 example3 3
2 example4 1
3 example5 1
4 example6 3
How To Write SELECT That Get To ME Result Like This
Id | Name | Status | Count_All
------+------------+--------------+------------
1 example1 3 6
2 example2 2 6
3 example3 3 6
4 example4 1 6
5 example5 1 6
6 example6 3 6
The Value Of Column Count_All, Is Count All Rows
Please use this solution..
SELECT Id , [Name] , [Status], COUNT(*) OVER() Count_All
FROM yourTableName
If I am understanding you correctly, you want a count of all rows in the table as the column Count_All, so Add a Count on column id to get all rows as a new column named Count_All then Group by your other columns to allow for the aggregate Count method.
SELECT [Id], [Name], [Status], COUNT([Id]) AS [Count_All]
FROM [dbo].[YourTable]
GROUP BY [Id], [Name], [Status]
This will do it:
SELECT
y.ID,
y.Name,
y.Status,
Count_All = (SELECT COUNT(*) FROM yourtable)
FROM
yourtable AS y
SELECT *,COUNT(1) OVER() AS COUNT FROM TABLE
I have a table with values,
col1 col2 col3
1 0 ABA
1 0 ABB
1 0 ABC
2 0 BBA
2 0 BBB
2 0 BBC
I am trying to update the table to see the number of repetition of col1, in this case col1 has repeated 3 times so each update to col2 incremented by 1.
Required output after the update table
col1 col2 col3
1 1 ABA
1 2 ABB
1 3 ABC
2 1 BBA
2 2 BBB
2 3 BBC
A simple row_number() -ing should work
;with TMP as (
select *, row_number() over (partition by col1 order by col3) as RowNum
from tbl
)
update TMP set col2=RowNum
Where
tbl: is your table name
partition by col1: resets the row numbering for each col1 group
order by col3: is the basis for numbering within a col1 group
Assuming you are intending col3 to be in non-descending order, this should do it:
UPDATE MyTable
SET col2=(SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.col1=T1.col1 AND T2.col3<=T1.col3)
FROM MyTable AS T1
You will get duplicates in col2, if there are duplicates in col3 for a particular col1 value.
In case you are interested, here is a pretty verbose (and more expensive execution wise) solution using a ranking function. It has the same issue (i.e., the count gets repeated) for duplicates in col1/col3, as the previous solution:
UPDATE MyTable
SET col2=(
-- In the following query, DISTINCT merges rank dups caused by col3 dups
-- SELECT TOP(1) MyRank would also work.
SELECT DISTINCT MyRank
FROM (
SELECT col3,
DENSE_RANK() OVER (PARTITION BY col1 ORDER BY col3) AS MyRank
FROM MyTable
WHERE col1=UpdatedTable.col1
) As RankTable
WHERE RankTable.col3=UpdatedTable.col3)
FROM MyTable AS UpdatedTable