How to select in SQL Server 2008 - sql

Have an issue
Examle
ID ID2
1 100
3 100
5 100
1 110
2 110
4 110
select * from table where ID in (1,4) ---executing not correctly
select * from table where ID = '1' and ID = '4' ---not work
I need that ID2 will '110' (select ID2 which have 2 value ID)
Thanks.

If you have few ID's you can use EXISTS:
SELECT ID, ID2
FROM dbo.Table1 t1
WHERE EXISTS
(
SELECT 1 FROM dbo.Table1 t2
WHERE t2.ID=1 AND t2.ID2=t1.ID2
)
AND EXISTS
(
SELECT 1 FROM dbo.Table1 t2
WHERE t2.ID=4 AND t2.ID2=t1.ID2
)
AND ID IN (1, 4)
This returns the two records that have the same ID2 and ID=1 AND ID=4.
ID ID2
1 110
4 110
Demo-Fiddle

SELECT ID FROM (
SELECT ID, COUNT(*) OVER(PARTITION BY ID2) as cnt FROM Table) t
WHERE t.cnt>1

Related

retrieving ids with 'only' one type of code

Sample of table:
ID Code
2324 1
2324 2
2325 1
2326 1
2326 2
I want to get the id’s that only have code ‘1’ and not also code ‘2’ so the result would be
2325 1
Since the others have code’s 1 and 2
I've tried
SELECT * FROM TABLE
WHERE CODE != 1 AND CODE = 2
but that just returns any id's with code 2 regardless if the id also has code 1 or not
select ID, min(code) from t1
group by ID
having min(code) = 1 and max(code) = 1
With NOT EXISTS:
SELECT * FROM TABLE T
WHERE T.Code = 1 AND NOT EXISTS (
SELECT 1 FROM TABLE WHERE ID = T.ID AND Code <> T.Code
)
Try this:
select * from myTable t1
where not exists(select 1 from myTable
where t1.id = id and Code <> 1)
use not exists
select * from table t1 where Not exists
( select 1 from table t2 where t1.id=t2.id and
t2.code=2)
with cte as
(
select 2324 as id ,1 as code union all
select 2324 ,2
union all
select 2325,1
union all
select 2326,1 union all
select 2326 ,2
)
select * from cte t1 where Not exists
( select 1 from cte t2 where t1.id=t2.id and
t2.code=2)
demo link
id code
2325 1
You can try this.
SELECT * FROM TABLE
WHERE T.Code = 1 AND NOT EXISTS (
SELECT 1 FROM TABLE WHERE ID = T.ID AND Code <> T.Code
)

Multiple records for same id and i don't want to pull if id is not matching

I have a table 1 which is like this:
Id Values
100 1
100 2
100 3
110 1
110 2
110 4
120 3
I want the id where there is no 1 and 2 so my result should be like this
ID Values
120 3
you could try something like this.
SELECT id
,values_t
FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM Table1 t2
WHERE t1.id = t2.id
AND t2.values_t IN (
1
,2
)
);
Fiddle
This makes a select of all data without id=1 or id=2
SELECT *
FROM Table2
WHERE values != 1 OR values != 2
Also you can use this as WHERE:
WHERE values > 2
SELECT Id, Values
FROM table2
WHERE Id Not in (select Id from table2 where Values IN (1,2))
this is your query

SQL Query to retrieve values that belong exclusively to a group

Suppose I have one table with the following values and columns:
ID1 | ID2
1 | 1
2 | 1
3 | 1
4 | 1
4 | 2
3 | 3
4 | 3
4 | 4
4 | 4
I'd like to retrieve the ID2 values that belong exclusively to records where ID1 = 4. So for the above example, I'd like to see the following response:
ID1 | ID2
4 | 2
4 | 4
Try working it out contrapositively like this.
Finding all elements where ID1 is only 4 is the same as finding all elements that don't not have ID1 = 4.
CREATE TABLE #temp (ID1 NVARCHAR(10), ID2 NVARCHAR(10))
INSERT INTO #temp(ID1,ID2) VALUES (N'1',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'2',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'3',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'2')
INSERT INTO #temp(ID1,ID2) VALUES (N'3',N'3')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'3')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'4')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'4')
SELECT * FROM #temp AS t
SELECT DISTINCT * FROM #temp AS t
WHERE id2 NOT IN (SELECT ID2 FROM #temp AS t WHERE ID1 <> 4)
These queries will probably be useful to you for the more general cases (and by general I mean when ID1 is something other than 4):
select distinct t1.id1, t1.id2
from T as t1
where not exists (
select 1
from T as t2
where t2.ID1 <> t1.ID1 and t2.ID2 = t1.ID2
)
select t1.id1, count(distinct t1.id2)
from T as t1
where not exists (
select 1
from T as t2
where t2.ID1 <> t1.ID1 and t2.ID2 = t1.ID2
)
group by t1.id1
You can also do this:
select 4,id2 from
(select distinct ID1 , ID2 from t) t1
group by id2
having count(*)=1
There are a few ways to do this:
SELECT t1.id1, t1.id2
FROM mytable t1
WHERE t1.id1 = 4
AND NOT EXISTS ( SELECT 1 FROM mytable t2
WHERE t2.id2 = t1.id2
AND t2.id1 != 4 );
or:
SELECT id1, id2 FROM (
SELECT id1, id2
FROM mytable
GROUP BY id1, id2
HAVING COUNT(*) = 1
) WHERE id1 = 4;
or:
SELECT id1, id2 FROM (
SELECT id1, id2, COUNT(*) OVER ( PARTITION BY id2 ) AS cnt
FROM mytable
) WHERE id1 = 4
AND cnt = 1;

Max rows by group

Current SQL:
select t1.*
from table t1
where t1.id in ('2', '3', '4')
Current results:
id | seq
---+----
3 | 5
2 | 7
2 | 5
3 | 7
4 | 3
Attempt to select maxes:
select t1.*
from table t1
where t1.id in ('2', '3', '4')
and t1.seq = (select max(t2.seq)
from table2 t2
where t2.id = t1.id)
This obviously does not work since I'm using an in list. How can I adjust my SQL to get these expected results:
id | seq
---+----
2 | 7
3 | 7
4 | 3
Group By is your friend:
SELECT
id,
MAX(seq) seq
FROM TABLE
GROUP BY id
EDIT: Response to comment. To get the rest of the data from the table matching the max seq and id just join back to the table:
SELECT t1.*
FROM TABLE t1
INNER JOIN (
SELECT
id
MAX(seq) as seq
FROM TABLE
GROUP BY id
) as t2
on t1.id = t2.id
and t1.seq = t2.seq
EDIT: Gordon and Jean-Francois are correct you can also use the ROW_NUMBER() analytic function to get the same result. You need to check the performance difference for your application (I did not check). Here is an example of that:
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY seq DESC) as row_num
,*
FROM TABLE
) as TMP
WHERE row_num = 1
This SQL Query will give you max seq from individaul ID.
SELECT t1.*
FROM t1
WHERE t1.id in ('2', '3', '4')
AND NOT EXISTS (
SELECT *
FROM t1 t2
WHERE t2.id = t1.id
AND t2.seq > t1.seq
select *
from table
where (id,seq) in
(
select id,max(seq)
from table
group by id
having id in ('2','3','4')
);
That is if id and/or seq are completely part of the PK of that table.
Here's another example, using the first/last method I mentioned earlier in the comments:
with sd as (select 3 id, 5 seq, 1 dummy from dual union all
select 2 id, 7 seq, 2 dummy from dual union all
select 2 id, 5 seq, 3 dummy from dual union all
select 3 id, 7 seq, 4 dummy from dual union all
select 3 id, 7 seq, 5 dummy from dual union all
select 4 id, 3 seq, 6 dummy from dual)
select id,
max(seq) max_seq,
max(dummy) keep (dense_rank first order by seq desc) max_rows_dummy
from sd
group by id;
ID MAX_SEQ MAX_ROWS_DUMMY
---------- ---------- --------------
2 7 2
3 7 5
4 3 6
The keep (dense_rank first order by ...) bit is requesting to keep the values associated with the rank of 1 in the order list of rows. The max(...) bit is there in case more then one row has a rank of 1; it's just a way of breaking ties.

Select rows not in another table by comparing two table

I have following two tables TableA and TableB
TableA
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 3 1 200
4 1 2 75
5 2 2 100
6 1 3 400
7 2 3 200
TableB
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 1 2 75
I want to compare Month_Id Customer_Id Total_Amount in both tables and select Id from TableA. The output should be as follow.
Output
Id
3
5
6
7
My concept is:
SELECT TableA.Id FROM TableA
WHERE TableA.Month_Id <> TableB.MonthId AND
TableA.Customer_Id <> TableB.Customer_Id AND
TableA.Total_Amount <> TableB.Total_Amount
SELECT TableA.Id
FROM TableA
WHERE NOT EXISTS (
SELECT 1
FROM TableB
WHERE TableB.Month_Id = TableA.Month_Id
AND TableB.Customer_Id = TableA.Customer_Id
AND TableB.Total_Amount = TableA.Total_Amount
)
select Id
from (
select Id, Month_Id, Customer_Id, Total_Amount from TableA
except
select Id, Month_Id, Customer_Id, Total_Amount from TableB
) q
SELECT id FROM
(SELECT id, month_id, customer_id, total_ammount FROM TableA
EXCEPT
SELECT id, month_id, customer_id, total_ammount FROM TableB);
You can use the EXCEPT set operator:
SELECT id
FROM (SELECT * FROM table_a
EXCEPT
SELECT * FROM table_b) t
You can use Merge with WHEN NOT MATCHED
place your condition in ON <merge_search_condition>
SELECT Id FROM TableA A LEFT JOIN tableB B
ON A.Id=B.Id AND A.Month_Id =B.Month_Id
AND A.Customer_Id =B.Customer_Id
AND A.Total_Amount=b.Total_Amount
WHERE B.Id is NULL
In oracle sql it would be:
SELECT ID FROM
(SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_A
MINUS
SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_B);
Is this what you want?
(Not sure of MINUS operator in sql-server though)