This is an example of what I want to do:
I have a table called: TEST it has two columns: ColA and ColB
It has the following data:
ColA ColB
---- ----
0 1
0 1
0 3
2 1
2 3
I want to retrieve all values where ColA > 0, except I want to still include rows where ColA = 0 if ColB = 3
The following should work:
SELECT *
FROM TEST
WHERE ColA > 0 --All rows where ColA > 0
OR (ColA = 0 AND ColB = 3) -- All rows where ColA = 0 AND ColB = 3
Try This SQL
SELECT * FROM TEST WHERE ColA > 0 OR ( ColA = 0 AND ColB = 3)
It returns the following data
ColA ColB
0 3
2 1
2 3
Hope it helps.
SELECT * FROM TEST WHERE colA > 0 OR (colA=0 AND colB=3);
Not test yet, but hopefully this should work.
Related
I have a table structured as below
Customer_ID Sequence Comment_Code Comment
1 10 0 a
1 11 1 b
1 12 1 c
1 13 1 d
2 20 0 x
2 21 1 y
3 100 0 m
3 101 1 n
3 102 1 o
1 52 0 t
1 53 1 y
1 54 1 u
Sequence number is the unique number in the table
I want the output in SQL as below
Customer_ID Sequence
1 abcd
2 xy
3 mno
1 tyu
Can someone please help me with this. I can provide more details if required.
enter image description here
This looks like a simple gaps/islands problem.
-- Sample Data
DECLARE #table TABLE
(
Customer_ID INT,
[Sequence] INT,
Comment_Code INT,
Comment CHAR(1)
);
INSERT #table
(
Customer_ID,
[Sequence],
Comment_Code,
Comment
)
VALUES (1,10 ,0,'a'),(1,11 ,1,'b'),(1,12 ,1,'c'),(1,13 ,1,'d'),(2,20 ,0,'x'),(2,21 ,1,'y'),
(3,100,0,'m'),(3,101,1,'n'),(3,102,1,'o'),(1,52 ,0,'t'),(1,53 ,1,'y'),(1,54 ,1,'u');
-- Solution
WITH groups AS
(
SELECT
t.Customer_ID,
Grouper = [Sequence] - DENSE_RANK() OVER (ORDER BY [Sequence]),
t.Comment
FROM #table AS t
)
SELECT
g.Customer_ID,
[Sequence] =
(
SELECT g2.Comment+''
FROM groups AS g2
WHERE g.Customer_ID = g2.Customer_ID AND g.Grouper = g2.Grouper
FOR XML PATH('')
)
FROM groups AS g
GROUP BY g.Customer_ID, g.Grouper;
Returns:
Customer_ID Sequence
----------- ----------
1 abcd
1 tyu
2 xy
3 mno
I ask if the nested houses are used as follows:
SELECT
CASE
WHEN Col1 < 2 THEN
CASE Col2
WHEN 'X' THEN 10
ELSE 11
END
WHEN Col1 = 2 THEN 2
.....
ELSE 0
END as Qty,
......,
FROM ....
explanation: If Col1 <2 shows something, but that something if X gives me the value 10 otherwise 11 If Col1 = 2 shows 2 otherwise 0 everything in the column name Qty
Is the reasoning correct?
Thanks in advance
It's should return what you say you need, but it's easier to read this way:
SELECT
CASE
WHEN Col1 < 2 AND Col2 = 'X' THEN 10
WHEN Col1 < 2 THEN 11
WHEN Col1 = 2 THEN 2
--.....
ELSE 0
END AS Qty
FROM
-- ...
I have the following query:
SELECT case
when tbl.id % 2 = 0 then 'mod-2'
when tbl.id % 3 = 0 then 'mod-3'
when tbl.id % 5 = 0 then 'mod-5'
else 'mod-x'
end as odds, tbl.id from some_xyz_table tbl;
If the table has Id 5,6,7 then it is returning output as (copied from pg-admin):
"mod-5";5
"mod-2";6
"mod-x";7
But, here I can see 6 is divisible by both 2 and 3. And my expected output is:
"mod-5";5
"mod-2";6
"mod-3";6 <-- this
"mod-x";7
Is there any way to modify this query to obtain such output? Any alternate solution will do for me.
You could do this with UNION queries [EDIT changed it to use UNION ALL]:
SELECT 'mod-5', id FROM tbl -- divisible by 5
WHERE id %5 = 0
UNION ALL
SELECT 'mod-2', id FROM tbl -- divisible by 2
WHERE id %2 = 0
UNION ALL
SELECT 'mod-3', id FROM tbl -- divisible by 3
WHERE id %3 = 0
UNION ALL
SELECT 'mod-x',id FROM tbl -- not divisible by 5,3 or 2
WHERE id %5 <> 0 AND id%2 <> 0 AND id % 3 <> 0
Suppose I have data in table like:
id level flag
1 1 0
1 2 0
1 3 1
1 4 0
1 5 1
1 6 0
1 7 0
1 8 1
1 9 1
1 10 0
2 1 0
2 2 0
2 3 0
2 4 0
2 5 1
2 6 1
2 7 1
......
I want to update flag to 0 after first 1 value for flag. For example, with above sample data,
for id = 1, the first flag value =1 is level=3, then all flag values for level>3 should be updated to 0.
For id = 2, should update flag = 0 for all level>5
How to implement it with sql even one sql statement?
You should be able to do this with a WHERE EXISTS on the same table:
UPDATE t1
SET flag = 0
FROM TheTable t1
WHERE EXISTS (
SELECT 1
FROM TheTable t2
WHERE t2.id = t1.id
AND t2.level < t1.level
AND t2.flag = 1
)
SQL Fiddle demo
You can do this with an exists statement:
update table t
set flag = 0
where exists (select 1
from table t2
where t2.id = t.id and
t2.level < t.level and
t2.flag = 1
);
I have a table, TableA, that looks like this:
ColA ColB
0 20
1 10
0 5
1 15
I want to sort the rows so that all the records where ColA = 0 are listed first, and then these records are sorted by ColB ascendingly. After all the rows with ColA = 0 are listed, I then want to list all the rows with ColA = 1 but these rows sorted by ColB descendingly.
The results should look like this:
ColA ColB
0 5
0 20
1 15
1 10
Is this possible? Thanks for your help.
select t.ColA, t.ColB
from t
order by t.ColA
,case when t.ColA = 0 then t.ColB end asc
,case when t.ColA = 1 then t.ColB end desc
SELECT ColA,ColB
FROM table
ORDER BY
ColA, (CASE WHEN ColA = 1 THEN -1 * ColB ELSE ColB END)