I have the following to union several queries into one, however I also want to add an additional column that says Yes or No for duplicates, however, my IIF statement just returns No. Can somone suggest a way around this?
SELECT ztUnion.[PID (NOD)], ztUnion.Tasking, IIf(Count([PID (NOD)])>1,"Yes","No") AS [Multi Value]
FROM (SELECT qselAG.[PID (NOD)], qselAG.Tasking
FROM qselAG
UNION
SELECT qselWORKING.[PID (NOD)], qselWORKING.Tasking
FROM qselWORKING
UNION
SELECT qselNN.[PID (NOD)], qselNN.Tasking
FROM qselNN
UNION SELECT qselSHAM.[PID (NOD)], qselSHAM.Tasking
FROM qselSHAM
) AS ztUnion
GROUP BY ztUnion.[PID (NOD)], ztUnion.Tasking;
Related
I've write a SQL code to combine several columns from different tables.
SELECT *
FROM
(
SELECT PD_BARCODE
FROM docsadm.PD_BARCODE
WHERE SYSTEM_ID = 11660081
) t,
(
SELECT A_JAHRE
FROM docsadm.A_PD_DATENSCHUTZ
WHERE system_ID = 2066
) t2,
(
SELECT PD_PART_NAME
FROM docsadm.PD_FILE_PART
WHERE system_id = 11660082
) t3;
code works fine but if one of my where clause is not found in a table,the result is null even the other columns have value. How you can solve this problem?
It looks like you are doing a cross join between the three subquery tables. This would probably only yield output which makes sense if each subquery return a single value. I might suggest instead that you use a UNION ALL here:
SELECT ISNULL(PD_BARCODE, 'NA' AS value
FROM docsadm.PD_BARCODE WHERE SYSTEM_ID = 11660081
UNION ALL
SELECT ISNULL(A_JAHRE, 'NA')
FROM docsadm.A_PD_DATENSCHUTZ WHERE system_ID = 2066
UNION ALL
SELECT ISULL(PD_PART_NAME, 'NA')
FROM docsadm.PD_FILE_PART WHERE system_id = 11660082
The above union query might require a slight modification if the three columns being select don't all have the same type (which I assume to be varchar in my query).
If you really need these three points of data as separate columns, then you can just include the three subqueries as items in an outer select:
SELECT
(SELECT ISNULL(PD_BARCODE, 'NA')
FROM docsadm.PD_BARCODE WHERE SYSTEM_ID = 11660081) AS PD_BARCODE,
(SELECT ISNULL(A_JAHRE, 'NA')
FROM docsadm.A_PD_DATENSCHUTZ WHERE system_ID = 2066) AS A_JAHRE,
(SELECT ISNULL(PD_PART_NAME, 'NA')
FROM docsadm.PD_FILE_PART WHERE system_id = 11660082) AS PD_PART_NAME;
Note that as the above is written we simply including the subqueries as values in the select statement. But as you wrote your original query, you are joining the subqueries as separate tables.
Here is Query.
You can replace the word 'empty' by your required word or value
SELECT isnull(
(
SELECT PD_BARCODE
FROM docsadm.PD_BARCODE
WHERE SYSTEM_ID = 11660081
), 'Empty') AS PD_BARCODE,
isnull(
(
SELECT A_JAHRE
FROM docsadm.A_PD_DATENSCHUTZ
WHERE system_ID = 2066
), 'Empty') AS A_JAHRE,
isnull(
(
SELECT PD_PART_NAME
FROM docsadm.PD_FILE_PART
WHERE system_id = 11660082
), 'Empty') AS PD_PART_NAME;
This is a special case since you would be getting only one value per query of yours which you are trying to put up as column. In this case, you can use SQL PIVOT clause with UNION ALL of your queries like shown below. MIN can be used for aggregation in this case.
This would mean, you can get your data row-wise as you like, even for multiple different fields and then pivot it into columns in one go.
SELECT * FROM
(
SELECT 'PD_BARCODE' as KeyItem, PD_BARCODE Name from docsadm.PD_BARCODE
where SYSTEM_ID=11660081
UNION ALL
SELECT 'A_JAHRE', A_JAHRE from docsadm.A_PD_DATENSCHUTZ
where system_ID=2066
UNION ALL
select 'PD_PART_NAME', PD_PART_NAME from docsadm.PD_FILE_PART
where system_id=11660082
) VTABLE
PIVOT(MIN(Name)
FOR KeyItem IN ([PD_BARCODE], [A_JAHRE], [PD_PART_NAME])) as pivotData;
I was wondering to figure out this problem.
In the below query am trying to select #transcript as the combination of 2 column values. but when i am using this variable in select statement am getting only 3 results (actual output should be 8 results). where as when i use the condition directly in select statement am getting actual output.
can anybody please help me in figuring out this issue.
declare #transcript varchar(10)
select #transcript = [CAREER_CD]+[CAREER_SUFX_CD] from dbo.SR0DAT
select DISTINCT #transcript transcriptCareerCode,
case #transcript
when 'U1' then 'BACCALAUREATE'
when 'U2' then 'SECOND BACCALAUREATE'
when 'G1' then 'GRADUATE'
when 'L1' then 'LAW'
when 'D1' then 'DENTISTRY'
when 'M1' then 'MEDICINE'
when 'IU' then 'transcriptCareerName'
when 'IG' then 'IEO Graduate'
end as transcriptCareerName
from dbo.SR0DAT
WHERE #transcript <>'G2'
union
select 'IU','IEO Undergraduate'
union
select 'IG','IEO Graduate'
output:
transcriptCareerCode transcriptCareerName
G1 GRADUATE
IG IEO Graduate
IU IEO Undergraduate
2nd code:
select DISTINCT [CAREER_CD]+[CAREER_SUFX_CD] transcriptCareerCode,
case [CAREER_CD]+[CAREER_SUFX_CD]
when 'U1' then 'BACCALAUREATE'
when 'U2' then 'SECOND BACCALAUREATE'
when 'G1' then 'GRADUATE'
when 'L1' then 'LAW'
when 'D1' then 'DENTISTRY'
when 'M1' then 'MEDICINE'
when 'IU' then 'transcriptCareerName'
when 'IG' then 'IEO Graduate'
end as transcriptCareerName
from dbo.SR0DAT
WHERE [CAREER_CD]+[CAREER_SUFX_CD] !='G2'
union
select 'IU','IEO Undergraduate'
union
select 'IG','IEO Graduate'
output:
transcriptCareerCode transcriptCareerName
D1 DENTISTRY
G1 GRADUATE
IG IEO Graduate
IU IEO Undergraduate
L1 LAW
M1 MEDICINE
U1 BACCALAUREATE
U2 SECOND BACCALAUREATE
While both queries are indeed run against your table none of its columns are being used at all in the first one: for each row you're simply returning a fixed/constant expression. The distinct option is helping to mask what's going on and if you remove the that you'll see all of the multiple copies that are being collapsed into one row. If you also say select #transcript, * ... you'll see where the rest of the data is as well.
Are you just trying to accomplish an alias for the transcript expression so you don't have to repeat it?
with T as (
select CAREER_CD + CAREER_SUFX_CD as transcript
from dbo.SR0DAT
)
select DISTINCT
transcript transcriptCareerCode,
case transcript
when 'U1' then 'BACCALAUREATE'
when 'U2' then 'SECOND BACCALAUREATE'
when 'G1' then 'GRADUATE'
when 'L1' then 'LAW'
when 'D1' then 'DENTISTRY'
when 'M1' then 'MEDICINE'
when 'IU' then 'transcriptCareerName'
when 'IG' then 'IEO Graduate'
end as transcriptCareerName
from T
where transcript <> 'G2'
union
select 'IU', 'IEO Undergraduate'
union
select 'IG', 'IEO Graduate'
In your first query you are creating a singular value through the variable #transcript. As a result, your call to:
select #transcript = [CAREER_CD]+[CAREER_SUFX_CD] from dbo.SR0DAT
Only guarantees that the last record in the SELECT statement gets assigned to the #transcript variable.
As a result, only one row from that query will return (you've union'ed the other two rows in your first example).
The second example is using actual database set logic to pull in values across the set - not just the last value in the set.
I am creating a view that is based on multiple tables being unioned together, and I am trying to only include results that have records. this is the code I have so far:
SELECT
'001' AS ReportNumber
,'RPT001' AS ReportName
,ISNULL(SUM(1),0) AS ActiveReportCount
FROM [DBNAME].[dbo].[V_Rpt001]
UNION ALL
SELECT
'002' AS ReportNumber
,'RPT_002' AS ReportName
,ISNULL(SUM(1),0) AS ActiveReportCount
FROM [DBNAME].[dbo].[V_Rpt001]
WHERE SUM(1)> 0
This does not work as it wants it to be grouped by something, but the only other selections are literals.
Any help is appreciated.
Thanks
Is this what you want?
SELECT *
FROM ((SELECT '001' AS ReportNumber, 'RPT001' AS ReportName, ISNULL(SUM(1),0) AS ActiveReportCount
FROM [DBNAME].[dbo].[V_Rpt001]
) UNION ALL
(SELECT '002' AS ReportNumber, 'RPT_002' AS ReportName, ISNULL(SUM(1),0) AS ActiveReportCount
FROM [DBNAME].[dbo].[V_Rpt001]
)
) r
WHERE ActiveReportCount > 0;
When I add a new argument to the SELECT clause of a UNION, I get more records... how can this be? Isn't a UNION just mashing them together? Example:
EDIT: They're absolutely distinct. the code column is either "IN" or "OUT", and that's what I'm using to separate the two.
EDIT2: UNION ALL gives me 80 records, like it should, but it's odd because my two SELECT statements are absolutely distinct.
FINAL EDIT: Ultimate problem was records within one of my SELECT statements being not DISTINCT, not between the two SELECT statements. Thanks all.
-- Yields 76 records
SELECT
f.date
, f.code
, f.cost
FROM a.fact f
WHERE f.code = 'IN'
UNION
SELECT
f2.date
, f2.code
, f2.cost
FROM a.fact2 f2
WHERE f2.code = 'OUT'
;
-- Yields 80 records
SELECT
f.key
, f.date
, f.code
, f.cost
FROM a.fact f
WHERE f.code = 'IN'
UNION
SELECT
f2.key
, f2.date
, f2.code
, f2.cost
FROM a.fact2 f2
WHERE f2.code = 'OUT'
;
change UNION to UNION ALL and you should get the same results. UNION selects distinct rows, UNION ALL should select all.
By default UNION selects distinct results, there must be duplicates between your result sets.
The union all will solve your problem, as mentioned in a previous answer, it selects the distinct values only
References Oracle docs.
I have a field likeļ¼
SELECT * FROM
(
SELECT 'A9t' AS sortField UNION ALL
SELECT 'A10t' UNION ALL
SELECT 'A11t' UNION ALL
SELECT 'AB9F' UNION ALL
SELECT 'AB10t' UNION ALL
SELECT 'AB11t'
) t ORDER BY sortField
and the result is:
sortField
---------
A10t
A11t
A9t
AB10t
AB11t
AB9F
Actually I need is to combine the string and number sorting rules:
sortField
---------
A9t
A10t
A11t
AB9F
AB10t
AB11t
SELECT *
FROM (
SELECT 'A9t' AS sortField UNION ALL
SELECT 'A10t' UNION ALL
SELECT 'A11t' UNION ALL
SELECT 'AB9F' UNION ALL
SELECT 'AB10t' UNION ALL
SELECT 'AB11t'
)
t
ORDER BY LEFT(sortField,PATINDEX('%[0-9]%',sortField)-1) ,
CAST(substring(sortField,PATINDEX('%[0-9]%',sortField),1 + PATINDEX('%[0-9][A-Z]%',sortField) -PATINDEX('%[0-9]%',sortField) ) AS INT),
substring(sortField,PATINDEX('%[0-9][A-Z]%',sortField) + 1,LEN(sortField))
If the first character is always a letter, try:
SELECT * FROM
(
SELECT 'A9t' AS sortField UNION ALL
SELECT 'A10t' UNION ALL
SELECT 'A11t'
) t ORDER BY substring(sortField,2,len(sortField)-1) desc
I would say that you have combined the alpha and numeric sort. But what I think you're asking is that you want to sort letters in ascending order and numbers in descending order, and that might be hard to do in a nice looking way. The previous answers will not working for your problem, the problem is that Martin Smith's solution doesn't take strings with two letters as prefix and Parkyprg doesn't sort numbers before letters as you ask for.
What you need to do is to use a custom order, see example here: http://www.emadibrahim.com/2007/05/25/custom-sort-order-in-a-sql-statement/, but that is a tedious way to do it.
EDIT: Martins Smith's solution is updated and works just fine!