Union All Command in SQL - sql

SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID from terminal b;
This is the code , but in the first statement as you can see there is no PROFILEID column. I would like to write " null " to that column because we don't know the PROFILEID of them. How can I write "null" to that column ?

Try this Query:
SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER||'|'||'NULL'
from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID
from terminal b;

Related

Reducing the length of my SQL code

I am using a third party software for this SQL query which has various limitations, one being a character limit of 1,000 characters. I have created code that will pull through a list of contact numbers registered to a particular account, and have had a view created specifically for this task. My current script (see below) works, but brings back duplicate responses. I want to shorten the length of the code, and to select for only Distinct responses.
As background, the CRM system this is linked to has multiple places someone can leave a contact number, so I need the code to remove any duplicates when it displays the response. The current code does not attempt to remove duplicates due to the aforementioned character limit, it is:
SELECT [Contact_Mobile_Phone] AS V, [Contact_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Home_Phone] AS V, [Contact_Home_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Work_Phone] AS V, [Contact_Work_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Work_Phone] IS NOT NULL
UNION ALL
SELECT [Group_Home_Phone] AS V, [Group_Home_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Group_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Group_Mobile_Phone] AS V, [Group_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Group_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Home_Phone] AS V, [Contact_Home_Phone] AS D
FROM [DatabaseName]
WHERE [Group_ID] = '{Script.V1}' AND [Contact_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Mobile_Phone] AS V, [Contact_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE [Group_ID] = '{Script.V1}' AND [Contact_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT 'Enter other number' AS V, 'Enter other number' AS D
As you can see, it's not the most elegant thing I've coded. What I want it to look like, is something similar to this:
SELECT DISTINCT ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) V,
([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) IS NOT NULL
AND MAINLKUPID = '{Import.TenantID}' AND ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) IS NOT NULL
The criteria is that it needs to display the same results in both Column V and D. It needs to not pull through Null data, and it needs to only enter each number once into each column, even if they came from different columns originally. I'm aware the syntax for my ideal code isn't right, hoping someone can point me in the right direction. Thanks in advance.
Why don't you put the code in a stored procedure and just call "EXEC my_stored_procedure;"? That would be a lot shorter!
Can you explain why you are selecting the same column twice, but giving it different names? Perhaps you intend for the first to be a description. If so, it should be in single quotes not square braces -- but this will affect the duplicate elimination.
You can do the following to remove duplicates:
Replace the UNION ALL with UNION
You can do the following to reduce the size:
Remove all square braces
Remove as V and as D from all but the first query
If that is not short enough, then remove the NULL comparison and structure the query as:
select *
from (<your query here with no null comparisons and `union` instead of `union all`) x
where d is not null;
select phone_num as V,phone_num as D
from [DatabaseName] unpivot (phone_num for phone_type in ([Contact_Mobile_Phone],[Contact_Home_Phone],[Contact_Work_Phone],[Group_Home_Phone],[Group_Mobile_Phone])) u
where MAINLKUPID = '{Import.TenantID}'
union
select phone_num as V,phone_num as D
from [DatabaseName] unpivot (phone_num for phone_type in ([Contact_Home_Phone],[Contact_Mobile_Phone])) u
WHERE [Group_ID] = '{Script.V1}'
union all
SELECT 'Enter other number','Enter other number'

How to define temporary table values in a subquery in Oracle

I am approaching this issue from a non DBA perspective, as in I do not have permissions to create new tables for the database. I am trying to work around this by using a subquery in Oracle kind of like this sudo code:
With temptable as ('col1name', 'col2name', 1,'a',2,'b')
Select * from temptable where col1name = 1
With the temptable looking like
Col1name | Col2name
1 a
2 b
And the output being row 1. I know it is not the easiest way to do it, but it is all I can think of to accomplish my task until I can get the admin to approve a new table. I have searched a lot but I can't find an answer. Is there a simple way to define temporary table data like this?
I would just do this as:
with temptable as (
select 1 as col1name, 'a' col2name from dual union all
select 2, 'b' from dual
)
Select *
from temptable
where col1name = 1;
As an alternative to a CTE (common table expresssion) as suggested by Gordon, you can also use a query as an old-school inline view.
For example:
SELECT tt.col1name
, tt.col2name
FROM ( SELECT 1 AS col1name, 'a' AS col2name FROM DUAL
UNION ALL SELECT 2, 'b' FROM DUAL
UNION ALL SELECT 3, 'c' FROM DUAL
) tt
WHERE tt.col1name = 1
ORDER
BY tt.col1name

What is the equivalent of the Oracle "Dual" table in MS SqlServer?

What is the equivalent of the Oracle "Dual" table in MS SqlServer?
This is my Select:
SELECT pCliente,
'xxx.x.xxx.xx' AS Servidor,
xxxx AS Extension,
xxxx AS Grupo,
xxxx AS Puerto
FROM DUAL;
In sql-server, there is no dual you can simply do
SELECT pCliente,
'xxx.x.xxx.xx' AS Servidor,
xxxx AS Extension,
xxxx AS Grupo,
xxxx AS Puerto
However, if your problem is because you transfered some code from Oracle which reference to dual you can re-create the table :
CREATE TABLE DUAL
(
DUMMY VARCHAR(1)
)
GO
INSERT INTO DUAL (DUMMY)
VALUES ('X')
GO
You don't need DUAL in MSSQLserver
in oracle
select 'sample' from dual
is equal to
SELECT 'sample'
in sql server
While you usually don't need a DUAL table in SQL Server as explained by Jean-François Savard, I have needed to emulate DUAL for syntactic reasons in the past. Here are three options:
Create a DUAL table or view
-- A table
SELECT 'X' AS DUMMY INTO DUAL;
-- A view
CREATE VIEW DUAL AS SELECT 'X' AS DUMMY;
Once created, you can use it just as in Oracle.
Use a common table expression or a derived table
If you just need DUAL for the scope of a single query, this might do as well:
-- Common table expression
WITH DUAL(DUMMY) AS (SELECT 'X')
SELECT * FROM DUAL
-- Derived table
SELECT *
FROM (
SELECT 'X'
) DUAL(DUMMY)
In SQL Server there is no dual table. If you want to put a WHERE clause, you can simple put it directly like this:
SELECT 123 WHERE 1<2
I think in MySQL and Oracle they need a FROM clause to use a WHERE clause.
SELECT 123 FROM DUAL WHERE 1<2
This could be of some help I guess, when you need to join some tables based on local variables and get the information from those tables:
Note: Local variables must have been
Select #XCode as 'XCode '
,#XID as 'XID '
,x.XName as 'XName '
,#YCode as 'YCode '
,#YID as 'YID '
,y.YName as 'YName '
From (Select 1 as tst) t
Inner join Xtab x on x.XID = #XID
Inner join Ytab y on y.YID = #YID
It's much simpler than that.
Use literal values to establish data types.
Put quotes around column names if they need special characters.
Skip the WHERE clause if you need 1 row of data:
SELECT 'XCode' AS XCode
,1 AS XID
,'XName' AS "X Name"
,'YCode' AS YCode
,getDate() AS YID
,'YName' AS "Your Name"
WHERE 1 = 0

sql union syntax error for 2 sql statements

I have an sql syntax in my php script that gives me an error. All I want is to get the result of 2 sql statemet.
$sql1 = " SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%') ";
and
$sql2 = " SELECT profile_id from profile WHERE `country`='uk' ";
I want to get values from my database for both $sql1 and $sql2. I use UNION statement like this but it did not work. Any ideas how to fix it :
$sql = " (SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%')) UNION (SELECT profile_id from profile WHERE `country`='uk' ) ";
A UNION query must have same output fields.
The select queries that you combine in a union query must have the
same number of output fields, in the same order, and with the same or
compatible data types.

Add a blank record in SQL SELECT results

I have this SQL query:
SELECT clientName FROM Clients;
resulting in:
clientName
"Einstein"
"Edison"
"Newton"
I want to add an empty record on top of "Einstein" like this
clientName
""
"Einstein"
"Edison"
"Newton"
Please help me with a SQL sintax for this, and i want it to work in msaccess.
Thanks
add UNION
SELECT DISTINCT '' AS clientName FROM Clients
UNION
SELECT clientName FROM Clients
Try This Query:
SELECT '""' AS clientName FROM Clients UNION SELECT clientName FROM Clients
Actually this is more or like same as that of J W. But he seems to forget the ""
This is DB2 version
SELECT '""' FROM SYSIBM.SYSDUMMY1
UNION
SELECT SELECT clientName FROM Clients;
The trick in Access is to change the query type to a Pass-Through query and then write it as Luv did with a few minor changes. This is what it looks like against a WCS DB2 table.
SELECT '' AS DisplayName FROM SYSIBM.SYSDUMMY1
UNION
SELECT DB2ADMIN.ACACTDESC.DISPLAYNAME
FROM DB2ADMIN.ACACTDESC;