setting up a default value of a column in select statement - sql

actually i have 2 tables table1 and table2
table1
name
city
addr.
table2
name
city
addr.
ph.no
now ph.no field is an extra field in table 2
so i want to show field ph.no with a default value of 12345 in the output of select query on table1 as i want to append that output into an outfile.
help me out ..I am using db2 as400 database

Yes, you can do this:
SELECT name, city, addr, 12345 AS ph_no
FROM table1

I know this thread is very old but in case someone needs an answer to Nimmagadda's question, you should be able to accomplish it with something along the lines of:
SELECT name, city, addr,
CASE name
WHEN 'john' THEN 12345
WHEN 'peter' THEN 123
ELSE 0 /* ???? */ END AS ph_no
FROM table1

Related

How to append a default value in a query Row Source Type?

I have a Form that has a Field where the Lookup is set as a Combox Box Display Control, a Table / Query Row Source Type and a Query for the Row Source. I am wondering how to append non-bounded elements to this Combo Box.
Right now, the Combobox looks something like:
Mark
Chris
Mary
Charles
I would like for it to read as:
NONE
Mark
Chris
Mary
Charles
Of course, NONE is not an actual entry in my table. I would just like to append it for redundancy purposes.
My query for the Row Source is:
SELECT StudentId, StudentName FROM Student ORDER BY StudentId
I have tried just appending it like:
SELECT StudentId, StudentName, "NONE" FROM Student ORDER BY StudentId
But it completely skips the NONE part.
So, I am wondering if there's a way to append non-bounded elements to a Combobox that IS properly bound to a result set of a query.
You need union all :
SELECT s.*
FROM ((SELECT NULL AS StudentId, 'NONE' AS StudentName) UNION ALL
(SELECT StudentId, StudentName
FROM Student
)
) s;
You should use UNION in SQL query to join non-existing value
SELECT 0 as StudentId, 'NONE' as StudentName FROM Dual
UNION
SELECT StudentId, StudentName FROM Student
ORDER BY StudentId
Here Dual is table with one row

Sql Query regarding Join

I have two tables as follows:
Table 1:
Name | Specialisation
Table2:
Name | Slot | Date
I take user input of Name, Slot and Date. I want to display the records of Table1 for that Name such that there exists no record corresponding to the entered (Name, Slot, Date) in Table 2. What will be the SQL query for it?
Thanks in advance.
Supposing the input values were input_name, input_slot, and input_data, and the input_date was a suitable date format, one way to do it would be:
select name, specialisation from table1
where (name = input_name)
and (select name from table2
where (table2.name = input_name) and
(table2.slot = input_slot) and
(table2.date = input_date)) is NULL
Or something like that... :)
You can use a not in
select name
from table1
where name not in (
select name from table2
)
or not exists
select name
from table1
where name not exists (
select name from table2
where table2.name = table1.name
)

oracle sql how to match name split through 3 columns?

This database is not mine so i cannot alter it to a single name column (unfortunately).
**table1:**
name1,name2,name3
john,mega,rambo
john,master,travolta
john,super,connor
i'm sending query string $qs to a php file via ajax
file.php?qs=john%rambo
i want it to query the oracle database and return the name "John MEga Rambo", so i thought this would work but it doesn't:
SQL:
SELECT CONCAT(CONCAT(name1,name2),name3) as name
FROM table1
WHERE name LIKE '%{$qs}%'
I guess the biggest problem here is that someone can search by name1 or name2 / name1 or name3. Any ideas ?
Presumably you're getting an invalid identifier error. You can't referance a column alias in the same level of query it's defined (except in order by), because of the order the query is parsed and executed.
You could use a CTE or an inline view (subquery):
SELECT name
FROM (
SELECT CONCAT(CONCAT(name1,name2),name3) as name
FROM table1
)
WHERE name LIKE '%{$qs}%'
With some dummy data:
create table table1 (name1 varchar2(10), name2 varchar2(10), name3 varchar2(10));
insert into table1 (name1, name2, name3) values ('john', 'mega', 'rambo');
insert into table1 (name1, name2, name3) values ('john', 'master', 'travolta');
insert into table1 (name1, name2, name3) values ('john', 'super', 'connor');
that query - with {$qs} replaced with rambo - gets:
NAME
------------------------------
johnmegarambo
As you can see concat won't automatically add whitespace, so you might find the concatenation operator easier:
SELECT name
FROM (
SELECT name1 ||' '|| name2 ||' '|| name3 as name
FROM table1
)
WHERE name LIKE '%{$qs}%'
NAME
--------------------------------
john mega rambo
But if you're looking for more exact matches you might want to check each column instead:
SELECT name1 ||' '|| name2 ||' '|| name3 as name
FROM table1
WHERE name1 = '{$qs}'
OR name2 = '{$qs}'
OR name3 = '{$qs}'
Maybe you want the flexibility to search for partial matches though.
As a further consideration, by default you can only search for values exactly matching the case of the search string - so with your data, searching for RAMBO wouldn't find a match. There are a couple of ways around that if it's an issue for you.
Thank you Alex Poole for the replies, those didn't quite solve the problem BUT it pointed me in the right direction! It seems like oracle is a little bit picky with syntax, so a simple HAVING that would work in MySQL won't work on oracle. I'll post both solutions for ORACLE and MySQL
MySQL:
SELECT CONCAT(CONCAT(name1,name2),name3) AS name
FROM table1
HAVING name LIKE '{%$qs}%'
ORACLE
WITH table1 as ( SELECT CONCAT(CONCAT(name1,name2),name3) as name
FROM table1)
select * from table1 where name like '%{$qs}%'

How to create an Oracle SQL statement in the following case?

I have an oracle table with three clumns and three data records as shown below
ID FIRST_NAME LAST_NAME
01 Jason Martin
02 Alison Mathews
03 Robert Black
I want to select the first name by a given ID.
If the ID exists, i.e., it is a value among 01, 02, and 03, the corresponding FIRST_NAME needs to be displayed;
If the ID does not exist, a * symbol needs to be displayed.
Could you help me creating such an oracle SQL statement?
Many thanks in advance.
Do an outer join, this example using id = 2:
select coalesce(FIRST_NAME, '*')
from dual
left join tablename on id = 2
Use a CASE statement:
SELECT ID,
CASE WHEN ID IN ('01', '02', '03') THEN FIRST_NAME ELSE '*' END
FROM yourTable
Note: I assumed, based on your input data, that the ID column is VARCHAR and not numeric because of the leading zeroes. If ID is numeric, then change the query to this:
SELECT ID,
CASE WHEN ID IN (1, 2, 3) THEN FIRST_NAME ELSE '*' END
FROM yourTable

Returning distinct prioritizing results with order by

Name varchar, Value int, Active bit
-----------------------------------
'Name1',1,1
'Name2',2,1
'Name1',3,0
'Name2',4,0
'Name3',1,1
'Name4',1,1
I want to return where Active is anything but prioritize when it's 0 so I want to return this:
'Name1',3
'Name2',4
'Name3',1
'Name4',1
I tried this, but get an error to include Active in my return statement
Select Distinct Name, Value From Table Order by Active
So I tried this:
Select Distinct Name, Value, Active From Table Order by Active
But now it returns all the rows. I would like to prioritize where Active = 0 in the distinct results but since it requires I put Active in the return statement makes this complicated.
Can someone help?
Your question is a little confusing, but if I'm understanding it correctly, you need to use a group by statement:
select name,
max(case when active = 0 then value end) value
from yourtable
group by name
SQL Fiddle Demo
With your edits, you can use coalesce and still get it to work:
select name, coalesce(max(case when active = 0 then value end), max(value)) value
from yourtable
group by name
More Fiddle
You can order by fields not contained in the select clause
Select Name, Value
From Table
ORDER BY Active, Name, Value
But you cannot use SELECT DISTINCT at the same time.
If you use "select distinct" there is the possibility that some rows will be discarded, when this happens there is no longer any viable relationship retained between [Active] and the "distinct" rows. So if using select distinct, and you need to order by [Active], then [Active] MUST be in the select clause.
I couldn't delete the post b/c of the other answers, but here is answer I was looking for in case anyone else was wondering.
SELECT Distinct Name,Value FROM Table WHERE Active = 0
UNION ALL
SELECT Distinct Name,Value FROM Table a WHERE Active = 1 AND NOT EXISTS (
SELECT TOP 1 1 FROM Table a2 WHERE a2.Active = 0 AND a2.Name = a.Name
)
Review #Sgeddes 's answer for a better solution.
Thanks to everyone for their help.
Perhaps this:
create table #t(
Active int not null,
Name varchar(10) not null,
Value int not null,
primary key clustered (Active desc,Name,Value)
);
insert #t(Active,Name,Value)
select Active,Name,Value from [Table];
select Name, Value
from #t;
go
yields as desired:
Name Value
---------- -----------
Name1 1
Name2 2
Name3 1
Name4 1
Name1 3
Name2 4