Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to select some records from a table with CASE option
like this
SELECT col1,col2,
CASE col2
WHEN '7c6014eb0000d37090d972c0ad2520f7' THEN 'xxxxx'
WHEN '5610d19400005469af3a78d225e11cb9' THEN 'aaaaa'
WHEN '31c08eb10000ye1aa51ff5a165246604' THEN 'bbbb'
WHEN '37e543fe00016d03007f6b304edfa94e' THEN 'ccccc'
WHEN '0ca1e79f0001zde1909b64c3d1246b80' THEN 'ddddd'
WHEN '25a14c480001g491c7284b0e107a39e7' THEN 'eeeee'
+500k line ....
END AS TargetAliasColumnName
FROM table
but the problem that i have a large script +500k record,
i got just( Command(s) completed successfully.
) :/
Update:
The hole script compile in the excute area, but after executing , i got just Command(s) completed successfully. my table name is ( account) in the table there are 2 columns ( user,password) in the table there are 1 milion records, i want to to select all these records in the table but with CASE password records
It,s better to create table and put all case value pair into table .
after you can use joins to achieve your goal.
The SQL is probably too big to compile. If you add the case values to a table you will be able to do what you want.
tbl_case
key_column, case_value
7c6014eb0000737090d972c0ad2520f7 xxxxx
SELECT table.key_value, tbl_case.case value
from table
join tbl_case on table.key_value = tbl_case.key_value; ----------
Try This ,
Create another table like this
CREATE TABLE [dbo].[Table_1](
[Col2] [nvarchar](50) NULL,
[CaseVal] [nchar](10) NULL
) ON [PRIMARY]
Insert all the Distinct data what you have. Then write a sql like below
SELECT b.Col1, b.Col2, a.CaseVal TargetAliasColumnName
FROM Table_1 a inner join [dbo].[Table1] b on
a.col2=b.Col2
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have my database up and running and can do some basic selects using SQL Server 2017 Management Studio. The problem I'm running into now is a have a table that depending on the values within a row, a certain outcome is generated. I have the below code which currently works.
I'm running into issues what I try to reference Master_Status when I'm writing another case.
Basically I need do say if Master_Status is null after the first check then allow it to go into the second criteria, etc. Have about 10 outcomes I need to build this out for.
Is this possible? Should I be using reference tables?
EDIT
Below better represents my problem: I'm trying to loop through a bunch of critera to determine the end status of a sale. The sale can only have a unique status which is why I'd like to check if master_status is null.
WITH MasterStatus AS
(
SELECT
[Name]
,case WHEN saleDate is not Null then 'outcome1'
else NULL end as 'Master_Status'
,case when SaleSize is not NULL and Master_Status is Null then
'Outcome2'
else NULL end as 'Master_status'
from [AllNames]
)
select
[Name]
from MasterStatus where Master_Status is not null
Based on the current iteration of your query, it seems you're struggling with how CASE expressions work. Rather than trying to use multiple CASE expressions to assign values to the same column (which won't work), you need one CASE expression with multiple WHEN conditions.
WITH MasterStatus AS
(
SELECT
Name
,CASE
WHEN saleDate IS NOT NULL
THEN 'Outcome1'
WHEN SaleSize IS NOT NULL
THEN 'Outcome2'
--...
WHEN ColumnN IS NOT NULL
THEN 'OutcomeN'
ELSE NULL
END AS Master_Status
FROM
AllNames
)
SELECT
Name
FROM
MasterStatus
WHERE
Master_Status IS NOT NULL;
The WHEN conditions will be evaluated in order, and on the first WHEN that evaluates to TRUE, the corresponding THEN value will be assigned to the column alias, Master_Status.
What you have actually already works, I believe your case statement is just ALWAYS true (so Master_Status is always not null.
See below:
declare #allnames as table ([name] int);
insert into #allnames([name])
values (1),(0)
;WITH MasterStatus as (
select
[name]
,case WHEN [name] = 1 then 'outcome1'
else NULL end as 'Master_Status'
from #allnames
)
select
[name]
from MasterStatus where Master_Status is not null
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Why my query is still showing the duplicate tuples?
I want all the attributes of all distinct tuples
select distinct * from employees;
I think your approach related to NULL values and empty strings maybe the cause of the problem :
create table tab( id int, value varchar2(75));
insert into tab values(1,' ');
insert into tab values(1,' ');
insert into tab values(1,'');
insert into tab values(1,null);
select distinct * from tab;
ID VALUE
-- -----
1
1
1 (null)
select id, length(value) as value from tab;
ID VALUE
-- -----
1 2
1 1
1 (null)
1 (null)
Oracle considers '' as NULL but the empty strings with length >= 1 is not considered as NULL.
SQL Fiddle Demo
If yours columns have some " " , you'll find duplicate because
Value1 value 2 " " is not
Value1 value 2 "" nor
value1 value2 NULL
Plus, PrimaryKey is always unique so
select distinct *, is unique
Often, if you need "select unique" in table employee, there a problem of conception because every employe should appears only once (if it is a real table employe without been linking)
To fix this during import you would have to trim whitespace from the end of the string, this should take care of those 3 different characters. Note that LTRIM and RTRIM only removes "blanks" which further down in the documentation suggests that only spaces are considered. You would thus have to use a different trim function, like in the programming language you're using, to do this trimming.
If you need all distinct results for a tuple then you should not use * (all column) but explicitly select for only the columns involved in tuple
select distinct col1, col2, col3 from employees;
Typically using all column selector (*) you select also the primary key from your table that typically (by definition) is not duplicated so using select * ...
you get all the rows in table.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
create table t1 (col1 int);
Leave the table empty.
select isnull(col1,0) from t1;
I want to replace null with zeros in the above case. However, ISNULL() does not work if there are no records in the table. How can I get around this?
Maybe you can first test if the table is empty:
IF NOT EXISTS (select * from t1)
SELECT 0
ELSE select coalesce(col1,0) from t1;
Anyway, you should use COALESCE instead of ISNULL because it is standard SQL.
In case if you want to replace non existing value then surround another null check of mentioned select statement. like this
ISNULL(select isnull(col1,0) from t1,0)
COALESCE(col1, 0)
will give you the first non-NULL value from the list, which is
col1 if col1 does not contain NULL
0 if col1 contains NULL.
If no record exists in the table , your query returns EMPTY not NULL .
You can transform the EMPTY value into a NULL value and then switch it to 0 like this :
SELECT ISNULL((SELECT col1 FROM tl),0) AS col1;
When I want to return exactly one row from a table that might be empty or have an arbitrary number of rows, then I often use aggregation:
select coalesce(max(col1), 0)
from t1;
This is guaranteed to return exactly one row. It is unclear what you want when the table is not empty, however.
If you absolutely must have a value returned for an empty table, maybe something like:
if ((select COUNT(*) from t1) = 0)
begin
select 0 as col1;
end
else
begin
select isnull(col1,0) as col1 from t1;
end
The left join will get a hard coded table, OneZero, even with no rows in t1.
select isnull(t1.col1, OneZero.zero)
from ( values(0) ) as OneZero (zero)
left join t1
on 1 = 1
I am trying to take the results of a select query in SQL and place them in another table in a different database. The table structure is identical. The select query is as follows;
USE Warwick
Go
Select tblOperations.Link, Project.*
From tblOperations
Inner Join Warwick.dbo.Project
On tblOperations.Link= Warwick.dbo.Project.[Project ID]
Where tblOperations.Job# = Warwick.dbo.Project.[Job Number] and
tblOperations.[Status] = 'Active' or tblOperations.[Status] = 'Pending'
The join lets me select just the jobs that are considered active. I need to copy the results into the table WCI_DB.dbo.Project, which already exists. I would lke to append and not overwrite if the record exists.
Any help would be appreciated.
Thanks.
You should tag your question with the database, which seems to be SQL Server. The SQL syntax is insert:
insert into WCI_DB.dbo.Project
<your select here>;
Normally, you want to list columns after the table name:
insert into WCI_DB.dbo.Project(list of columns>
<your select here>;
However, if this is a one-time exercise and you know the columns are the same, then it is small sin to omit them once.
To create a new table, using select into, which is documented here.
select . . .
into WCI_DB.dbo.Project
. . .
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle: how to UPSERT (update or insert into a table?)
Could you guys give me an suggestion on how to proceed in the below situation:
Read table 2 column 1
if value says the record exists in table 1
update table 1 record with table 2 record details
else(value says the record does not exist in table 1)
insert table 1 record with table 2 record details
I am beginner to Oracle SQL, Please let me know if there is a better approach..I was thinking about using cursors to solve this..
The simplest answer is to use the merge statement:
MERGE INTO table1 a
USING ( select column1, column2
from table2 ) b
ON ( a.column1 = b.column1 )
WHEN MATCHED THEN
update set a.column2 = b.column2
WHEN NOT MATCHED THEN
insert (a.column1, a.column2)
values (b.column1, b.column2)
Simply put this takes everything from the select on table2. It then joins this query to table1 on the condition. If there is a "match" then it updates, otherwise inserts.
The documentation has more information about various additional options that you don't, currently, require.
Have a look at the merge statement.
http://psoug.org/reference/merge.html