TEST table has 315,729,533 records in total
Using the following query I get One set of result -
SELECT count(*)
from test
where Col1 <> '' and Col2='U'
34,270,975
I want a Query that can provide me another set of result that gived me Total - last query result (315,729,533-34,270,975 = 281,458,558)
Records for each Col & Col2 are as follows -
Col1 Count
NULL 638
282221444
~ 48
C 34724501
D 37055
F 11
N 3144
Col2 Count
D 3621131
M 1772356
U 311593354
Will appreciate inputs.
You would use:
select count(*)
from test
where (Col1 = '' or Col1 is null) or
(Col2 <> 'U' or Col2 is null);
Related
I have two columns in my table:
col1 count
a 5
b 10
c 15
az 10
I want to change the value az to a and then sum count, so, result would be:
TABLE=
col1 count
a 15
b 10
c 15
How do I do this in SQL?
I did:
select col1, SUM(count)
FROM TABLE
WHERE
WHERE TABLE.col1 = CASE WHEN TABLE.col1 = 'az' THEN 'a' ELSE TABLE.col1 END GROUP BY fodc.country
But, it does not give the right results. It gives the unexpected results and completely eliminates az
How to do this in SQL?
You can put the case in the select:
select (case when col1 = 'az' then 'a' else col1 end),
sum(count)
from table1
group by 1;
I have a table with a column 'id' which is autoincrement. In some records i have zero values. I want to update them by their previous rows (here previous means id - 1) .
How can i do that?
This query returns those records with zero values:
SELECT * FROM myTable
WHERE col1 = 0
which returns:
id col1 col2 ..... coln
15 0 0 0
23 0 0 0
You could use a correlated subquery:
update mytable t
set col1 = (select t1.col1 from mytable t1 where t1.id = t.id - 1)
where col1 = 0
I tried to get some records from PostgreSQL DB but it returns no records when I add OR condition.
e.g.:
select * from table where col1 = 'A'
returns: 5k records
select * from table where col1 = 'A' and col2 <> 1
returns: 3k records
select * from table where (col1 = 'A' and col2 <> 1) or (col1 = 'B')
returns: 0 records
Please advise what am I doing wrong?
I've a requirement where in if the query string matches column1 , return me 1. If it matches column 2 return 2 else if it matches column 3 return 3.
Table strunctre:
col1 col2 col3
11 12 13
22 23 24
If my query string is 23, then i'm expecting a return value of 2 as it matches col2.
Something like below:
select 1
from table1
where col1=querystring
and orderid=xxx
or select 2 from table1
where col2=querystring
and orderid=xxx
or select 3 from table1
where col3=querystring and orderid=xxx
Basically i'm expecting one query which return single value based on the column it matches.
Is it something doable in SQL as i'm not very good in DB skills.
Any help is highly appreciated.
There are a couple of approaches. If there is a guarantee that no more than one column will match at a time, a UNION will work:
SELECT 1 AS SomeCol
FROM table1
WHERE col1 = querystring
AND orderid = xxx
UNION
SELECT 2
FROM table1
WHERE col2 = querystring
AND orderid = xxx
UNION
SELECT 3
FROM table1
WHERE col3 = querystring
AND orderid = xxx;
If more than one match can happen, another approach is this (note the order of precedence is now col1, col2, col3 etc):
SELECT CASE
WHEN col1 = querystring THEN 1
WHEN col2 = querystring THEN 2
WHEN col3 = querystring THEN 3
END AS SomeCol
FROM table1
WHERE orderid = xxx;
Please try using case
declare #var int
set #var=23
select
case #var when col1 then 1
when col2 then 2
when col3 then 3 end
from YourTable
where
col1=#var OR
col2=#var OR
col3=#var
Try using IF-ELSE condition in your query and check with passed parameter.
Assume I have the following style table, col1 col2 and col3 have same value scopes, I want to select the records when two of the 3 columns have a value combination such as ('ab' and 'bc'), in the following example, the first 3 records should be selected. Any good way to do this? I am using Sybase.
| id | col1 | col2 | col3 |
1 ab bc null
2 null ab bc
3 ab ab bc
4 de ab xy
Thanks.
I don't have Sybase to check, but you can Try this:
select * from Table where (col1 = "ab" or col2 = "ab" or Col3 = "ab")
and (col1 = "bc" or col2 = "bc" or Col3 = "bc")
I agree that the answer given here is totally acceptable, however I feel that if there were more than a few columns that needed to be evaluated, nesting the comparisons in the WHERE clause could become a bit cumbersome and illegible. I had to overcome a similar problem and I found that the following technique was quite helpful (I have adapted it to solve the issue listed above). Note that the evaluation of Total in the outer query can be easily adapted to increase or decrease the number of columns that contain the criteria being evaluated:
SELECT *
FROM
(SELECT id
, col1
, col2
, col3
, SUM(CASE WHEN UPPER(col1) IN ('AB', 'BC') THEN 1 ELSE 0 END
+ CASE WHEN UPPER(col2) IN ('AB', 'BC') THEN 1 ELSE 0 END
+ CASE WHEN UPPER(col3) IN ('AB', 'BC') THEN 1 ELSE 0 END) as Total
FROM <table>
GROUP BY id
, col1
, col2
, col3) as results
WHERE Total >= 2
ORDER BY id