Clause ORDER BY - sql

I have a problem with a select in sql server, i have this table with 2 columns:
a 2
b 1
c 100
d 1
a 100
b 1
c 2
d 1
I want ordered it based on the first column,in this way:
a 2
a 100
b 1
b 1
c 2
c 100
d 1
d 1
But then j want the rows with secondcolumn=100 be moved at the bottom,so:
a 2
b 1
b 1
c 2
d 1
d 1
a 100
c 100
I have tried with clause ORDER BY column1 ASC, (column2=100) ASC,but it didnt work!
Thankyou and greetings.

Actually, you want the rows with 100 in the second column moved to the bottom first, and then ordered by the first column:
order by (case when col2 = 100 then 1 else 0 end),
col1

Use the CASE expression as below
SELECT *
FROM tab
ORDER BY CASE
WHEN column2 = 100 THEN 1
ELSE 0
END ASC,
column1 asc

SELECT *
FROM table1
ORDER BY
CASE
WHEN col2>=100 THEN 1
ELSE 0
END,
col1,
col2
SQLFiddle Example

Related

Select table adding columns with data depending on duplicates in other column

Imagine this data.
Id
Type
1
A
1
B
1
B
2
A
3
B
I want to select table and ad two columns turning it to this. How can i do it? (In teradata)
Id
Type
Id with both A+B
Id with only A
1
A
1
0
1
B
1
0
1
B
1
0
2
A
0
1
3
B
0
0
I'm not familiar with teradata but in standard SQL next query should be working:
SELECT
T.*,
CASE WHEN Cnt = 2 THEN 1 ELSE 0 END AS BOTH_TYPES_PRESENT,
CASE WHEN Cnt = 1 AND Type = 'A' THEN 1 ELSE 0 END AS ONLY_A_PRESENT
FROM T
LEFT JOIN (
SELECT Id, COUNT(DISTINCT Type) Cnt FROM T WHERE Type IN ('A', 'B') GROUP BY Id
) CNT ON T.Id = CNT.Id;
SQL online editor

SQL -- Multiple rows, similar value in a row, need to not show specific values

Here is the issue:
Table name = a
1 2 3
123 1 A
123 1 A
123 2 A
332 1 A
332 1 A
321 2 B
321 2 A
321 1 A
So far what I have is this:
select distinct 1,2,3 from a where a.2='1' and a.3='B';
What it returns is each result (except for 321).
I only want to select values column 1 as long as that value is not in a row where there is a 2 in column 2 or a B in column 3. Is this possible?
"not in a row where there is a 2 in column 2 or a B in column 3" can be expressed as
select distinct 1,2,3 from a where a.2!='2' or a.3!='B';
or
select distinct 1,2,3 from a where a.2 <> '2' or a.3 <> 'B';
I would use group by and having:
select col1
from t
group by col1
having sum(case when col2 = 2 then 1 else 0 end) = 0 and
sum(case when col3 = 'B' then 1 else 0 end) = 0;

How to get the desired results for this scenario [SQL]

I need to build a query for the following scenario :
Input table:
col1 col2 col3 col4
-------------------------------
1 2 a pi
1 4 a ci
1 2 a ci
2 3 a pi
2 4 a ci
1 3 a ci
1 3 a pi
Logic required:
Fetch all the records from the input table except for the records matching below condition.
If value of group (a,b) is same for multiple rows, then only keep the row that has d='pi'
Ex: for row 1 & 3 value of (a,b) = (1,2) we need to keep only row 1 that has d='pi'.
Final desired output:
col1 col2 col3 col4
---------------------------------
1 2 a pi
1 4 a ci
2 3 a pi
2 4 a ci
1 3 a pi
Please help me out.
You can approach this with analytic functions, if you like:
select a, b, c, d
from (select t.*,
row_number() over (partition by a, b
order by (case when d = 'pi' then 1 else 2 end)
) as seqnum
from table t
) t
where seqnum = 1;
use a where not exist construct:
SELECT * FROM tab1 t
WHERE NOT EXISTS (SELECT 1 FROM tab1
WHERE t.a = a AND t.b = b)
OR d ='pi'

Set value of column to 0 for only one of the duplicate records?

I have the following table, and I want to set the value of deleted column to zero for only one of the records that have duplicate names.
id name deleted
------------------------
1 a 1
2 a 1
3 a 1
4 b 1
5 c 1
6 d 1
so the output will be:
id name deleted
------------------------
1 a 0
2 a 1
3 a 1
4 b 0
5 c 0
6 d 0
If your dbms is SQL-Server( >= 2005) you can use a CTE with ROW_NUMBER:
WITH CTE AS
(
SELECT ID, Name, Deleted,
RN = ROW_NUMBER() OVER(PARTITION BY name ORDER BY ID)
FROM dbo.T
)
UPDATE CTE
SET Deleted = 0
WHERE RN = 1
DEMO
UPDATE Tbl SET deleted = 0 WHERE id IN
(SELECT MIN(id) FROM Tbl GROUP BY name)
UPDATE A
SET A.deleted = 0
FROM Tbl A
LEFT JOIN Tbl B
ON A.Name = B.Name
AND A.Id > B.Id
WHERE B.Id IS NULL

SQL selecting from a particular duplicate row

Hi all I have a sql query
SELECT FKid1, FKID2, a, b, c
from [source]
where FKID1 = 3
which returns the following data set so
(hope formatting holds)
FKID1 FKID2 A B C
3 40297 0 0 2
3 40297 0 100 1
3 40325 0 0 2
3 40325 0 0 3
3 40325 0 10 -1
3 40348 0 10 3
3 40391 0 10 -1
3 40392 0 10 -1
3 40501 0 10 -1
3 40501 0 0 2
I'm trying to improve this query so that if there are 2 rows with duplicate FKID1 and FKID2 values, it will pick the column B value from a particular row as follows...
if there is a row with C = -1 use the B value in this row and ignore others.
if there are 2 rows with C <> -1 then pick the MAX(B) value.
For rows that are not duplicated, return as normal.
IE the results should look as follows...
FKID1 FKID2 A B C
3 40297 0 100 1
3 40325 0 10 -1
3 40348 0 10 3
3 40391 0 10 -1
3 40392 0 10 -1
3 40501 0 10 -1
the correct values selected for the B column and no dupes.
We have a solution at the moment, but I think it's overcomplicated and wondered if anyone had any ideas?
Thanks.
One way
;WITH cte As
(
SELECT FKid1, FKID2, a, b, c,
ROW_NUMBER() OVER (PARTITION BY FKID1, FKID2
ORDER BY CASE WHEN C = -1 THEN 0 ELSE 1 END ASC, B DESC) AS RN
from [source]
where FKID1 = 3
)
SELECT FKid1, FKID2, a, b, c
FROM cte
WHERE RN=1
Select T1.FKID1, T1.FKID2
, Min(A) As A
, Case
When Count(*) = 2 And NegCB.B Is Not Null Then Max(NegCB.B)
Else Max(B)
End As B
, Min(C) As C
From Source As T1
Outer Apply (
Select T3.B
From Source As T3
Where T2.FKID1 = T1.FKID1
And T2.FKID2 = T1.FKID2
And T2.C = -1
) As NegCB
Where FKID1= 3
Group By T1.FKID1, T1.FKID2