I have tables like following
I'd like to fill col2in9999/12/31 by referring to col1==A
col1 col2
A 2011/1/1
B 2013/4/1
C 2000/1/1
A 2010/1/1
Therefore my desired result is following
Are there any way to achieve this?
col1 col2
A 9999/12/31
B 2013/4/1
C 2000/1/1
A 9999/12/31
If someone has opinion,please let me know
Thanks
If you have one table and that's all that needs updating then:
UPDATE [table] SET col2 = '31-DEC-9999' WHERE col1 = 'A';
If they're two different tables you could just run CREATE TABLE table2 AS SELECT * FROM table1 and then just run that UPDATE above ^. Sure it's an extra step but it does the job.
Simply add case statement to your query:
SELECT COL1, CASE WHEN COL1 = 'A' THEN '9999/12/31' ELSE COL2 END AS COL2 FROM TABLE1;
Related
I am quite new to SQL and would need some suggestions to write a query to select the value of Col1 only if none of the value of status of Col2 (Col3) is null in plsql.
In the above, I am expecting the result to return only A2 as in A1, there is a null value in col3.
There are many ways to skin this cat but this should return you all the A2 rows:
select *
from yourTable d
where d.col1 in (select col1
from (select col1
,sum(case when col3 is null then 1 else 0 end) null_values
from yourTable
group by col1
)
where null_values = 0
)
To explain the approach briefly: The inner query counts the number of null col3 values for each col1 value; then the middle query returns the only the distinct col1 values having no associated null col3 values, and the outer query returns the all detail for all rows containing the col1 value.
The above is SQL of course. As you have specified PL/SQL, you could include something like a cursor or the bulk collect into clause as desired to work with the query result set.
As mentioned in my comment below, this is just one way of achieving the output - Oracle analytic functions may also prove helpful. Also depending on the size of your data set, you may want to consider the performance aspect of your chosen method.
I have a table with two Columns, Col1 and Col2. I want to retrieve only the data when the Col1 has multiple entries with different data in Col2.
For example, I want to retain:
Col1 Col2 Col1 Col2
AB-123456 AP-321654 AB-123456 AP-321654
AB-123456 AP-321789 AB-123456 AP-321789
AB-123456 AC-321456 AB-123456 AC-321456
AB-951357 AP-989898 AB-456851 AP-110211
AB-753159 AC-956854 AB-456851 AP-110279
AB-456851 AP-110211
AB-456851 AP-110279
I created through the Report feature a report that groups by Col1 and creates a subcount on the number of entries in Col2. Using the report, I seem to be having a problem with using the subtotal counter. If the subtotal counter is > 1, I want to report, otherwise skip and go to the next Col1 data.
My next option was to write VBA code to read through the table and output the multiples to a new data, then I could run that data through the report to format, etc. To count the multiples and use criteria, I thought perhaps the DCount function would work. I have tried different variations, but to no avail.
Ex:
NUM_OF_MULTS = DCount("Col1", "TBL_Of_Col1_Col2", Current_Col2 = Prev_Col2)
For Index1 = 1 to NUM_OF_MULTS ......
I tried different criteria, but it is either all the records or none.
I think the following should perform as you require:
select t1.* from YourTable t1
where exists (select 1 from YourTable t2 where t1.col1 = t2.col1 and t1.col2 <> t2.col2)
Change both occurrences of YourTable to suit your table name.
For every record, this uses a correlated subquery to test whether another record exists in the dataset with the same col1 value as the current record, but a different col2 value, thus meeting your criteria.
The use of select 1 is purely for optimisation: we don't care what data is returned by the subquery, only that it has records.
If you want to do it with SQL code you can use EXISTS:
select t.* from tablename as t
where exists (
select 1 from tablename
where Col1 = t.Col1 and Col2 <> t.Col2
)
and another option with the IN operator:
select * from tablename
where Col1 in (
select Col1
from (select distinct Col1, Col2 from tablename)
group by Col1
having count(*) > 1
)
Table structure
id col1 col2
1 data1 false
2 data2 true
I tried
select id, case col2 when true then col1 from table
and it is showing an internal server error. I want to select the col1 from table when the corresponding col2 is true.
probably just a simple syntax error in your case statment.
try this..
select
id,
case when col2=1 then col1 else 'some other value' end as computedCaseCol
from table1
see: SQL fiddle
Do you mean that ?
select id,col1 from tabe where col2 = 'true'
or
select id,col1 from tabe where col2 is true
??
You might not understood what I want to ask from the title but ,here is the explanation.
I have a data in Oracle database table. What I wanted to do is insert a new data to the table. This new data is based on the existing data but I have to change the value of one columns. So if I have 10 rows in the database after the insertion i will have 20 rows but the new 10 rows contain the same data except on of the columns is changed.
E.g table before insertion a new data
Col1 Col2 Col3
a b AA
1 2 33
table after insertion a new data
Col1 Col2 Col3
a b **BB**
1 2 **44**
Provided that you can encode what the new value should be; yes.
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
Col1,
Col2, -- This is a specific example based on your comment.
Col3 + 6 -- This just adds 6 to the existing value, but any SQL
FROM -- could actually go here, such as a CASE statement...
myTable
So, the question becomes; Do you have rules that you can implement in SQL for calculating the new value for Col3?
The rules could be something basic like...
CASE WHEN Col3 = 'AA' THEN '**BB**'
WHEN Col3 = '33' THEN '**44**'
ELSE 'Unknown'
END,
Or you could have all the new values in another table and look them up using a join...
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
OldTable.Col1,
OldTable.Col2,
COALESCE(NewTable.Col3, 'Unknown')
FROM
myTable AS OldTable
LEFT JOIN
lookup AS NewTable
ON OldTable.Col1 = NewTable.Col1
AND OldTable.Col2 = NewTable.Col2
Or a whole bunch of other options.
It will depend on how you determine how to change the data. How do you know, for example, that AA should become BB or that 33 should become 44?
Something like this will work for the two cases you posted. You can adapt it to whatever rule you want by changing the CASE statement to compute the new value differently.
INSERT INTO table_name( col1, col2, col3 )
SELECT col1,
col2,
(CASE WHEN col3 = 'AA'
THEN 'BB'
WHEN col3 = '33'
THEN '44'
ELSE null
END)
FROM table_name;
I have to create an SQL Server 2005 query which checks for the value of one attribute in the table and based on its value, select different sets of columns.
How can I do that?
for e.g.
In table 'car', if the values of 'type' attribute are 1 and 2
when type = 1, i want to execute a select 'query1' with 3 columns.
when type = 2, i want to execute another select 'query2' with 4 other columns.
How do I do that?
Please help.
I think you're looking at a Stored Procedure with an If statement. CASE will work, but it can't change the number of columns returned.
SELECT
Col1 = CASE WHEN Type = 1 THEN (SELECT Null FROM T1)
ELSE (SELECT Col1 FROM T2) END
, Col2 = CASE WHEN Type = 1 THEN (SELECT Col1 FROM T1)
ELSE (SELECT Col2 FROM T2) END
, Col3 = CASE WHEN Type = 1 THEN (SELECT Col2 FROM T1)
ELSE (SELECT Col4 FROM T2) END
, Col4 = CASE WHEN Type = 1 THEN (SELECT Col3 FROM T1)
ELSE (SELECT Col4 FROM T2) END
FROM Cars
If you would show us the DDL of all tables involved, you'd probably get a better answer or a different (read better) approach.