Union ALL not giving the column\ names for second group - sql

I am using the tool SQLDBx. which i highly like btw. I have 5 groups of series of Select statements and then I made a UNION ALL after the first group. Now it does run correctly
and displays the output but it does not assign the second group'
s field names.
For example. I am greatly abridging
Completed the SQL's needed to mimic a IBM i display screen. There are 5 product groups making up the screen. I was hoping to have one large Command SQL having the 5 SQLs using UNION ALL. This does 'compile' as a Command. However, it does only brings in the first part fields not the second. So this field is not included in the list of fields tree for COMMAND. PROGR2R2PST,
Is there something not correct how doing the UNION ALL? OUTPUT assigns the column name
of the first group to the second group.
HLDGR1PUN
21454
87273
so if i wanted to have one large SQL with union ALLs, it wont work. is there something other than UNION ALL I should use?
SELECT
count(*) as PROGR1PST,
(
SELECT COALESCE(SUM(OdQty#),0)
FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('RTR','INT','SAM')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR1PUN,
(
SELECT count(*)
FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('RTR','INT','SAM')
AND OHREQD = replace(char(current date, iso), '-', '') AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR1TOD,
(
etc..
UNION ALL
SELECT
count(*) as PROGR2R2PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('CUS','CIN','SMC','COC','DON')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR2PUN,

I don't think you can get the result you want by "nesting" the SQL statements and joining them with UNION.
This structure may work, depending upon your requirements:
SELECT 'Name1' AS Label , COUNT(*) AS The_Count
FROM table1
WHERE ...
UNION ALL
SELECT 'PROGR2R2PST', COUNT(*)
FROM table2
WHERE ...
UNION ALL
SELECT 'Name3', COUNT(*)
FROM table3
WHERE ...
This will give you back one row per select:
Label The_Count
--------------------
Name1 45867
PROGR2R2PST 22
Name3 1234
Note that the column names come from the first select. If this format doesn't match your requirements, please be more explicit in the question or in comments and I will try to help.

Related

SQL - Conditional Selection

I am trying to build an Oracle SQL query where I select users that contain at least one License = '+' while all Default Orgs = '-'. In other words, select users with licenses that have no default organizations. In the example below I would expect only Annete to show in the result.
Table_Users: User, License
Table_Organizations: Default_Org, Org_Name
Query below returns no results:
select User
from Table_Users, Table_Organizations
where Table_Users.User = Table_Organizations.UsrX
and (Default_Org = '+' and Default_Org = '-')*
select User
from Table_Users, Table_Organizations
where Table_Users.User = Table_Organizations.UsrX
and License = '+' and Default_Org = '-'
SELECT
u.User
FROM
Table_Users u
INNER JOIN Table_Organizations o
ON u.User = o.Usrx
GROUP BY
u.User
HAVING
COUNT(CASE WHEN License = '+' THEN 1 END) > 0
AND COUNT(CASE WHEN Default_Org = '+' THEN 1 END) = 0
First I would suggest using Explicit not Implicit join, it is a pet peeve of mine and I think a number of others on this site as Explicit join has been part of the ANSI SQL standard for a lot of years now.
As far as what technique you actually want would be called conditional aggregation. By counting only the values you are looking for you can then use them in a HAVING clause to exclude the record(s) you don't want.
Note COUNT(CASE WHEN... THEN END) will work because only the value you want will have a value to be counted and anything not meeting that condition will be NULL and therefore not counted.
Because I don't know which table has License on it you could also potentially use exists as follows:
SELECT
u.User
FROM
Table_Users u
WHERE
u.License = '+'
AND NOT EXISTS(SELECT 1 FROM Table_Organizations o WHERE u.User = o.Usrx AND Default_Org = '+')
Knowing + comes before - we can use a min aggregate. This assumes license and default org could only have '+' or '-' values.
With cte ("user", license, default_org, org_name) as (
SELECT 'Jeff','+','+', 'Org 1' FROM DUAL UNION ALL
SELECT 'Jeff','-','-', 'Org 2' FROM DUAL UNION ALL
SELECT 'Jeff','-','-', 'Org 3' FROM DUAL UNION ALL
SELECT 'Annete','+','-', 'Org 4' FROM DUAL UNION ALL
SELECT 'Annete','-','-', 'Org 5' FROM DUAL)
SELECT "user", min(license), Min(default_org)
FROM CTE A
GROUP BY "user"
HAVING min(license) = '+'
AND min(Default_org) = '-';
If license and default_org both have indexes with user; this would be pretty fast.

How to merge two columns from CASE STATEMENT of DIFFERENT CONDITION

My expected result should be like
----invoiceNo----
T17080003,INV14080011
But right now, I've come up with following query.
SELECT AccountDoc.jobCode,AccountDoc.shipmentSyskey,AccountDoc.docType,
CASE AccountDoc.docType
WHEN 'M' THEN
JobInvoice.invoiceNo
WHEN 'I' THEN
(STUFF((SELECT ', ' + RTRIM(CAST(AccountDoc.docNo AS VARCHAR(20)))
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON AccountDoc.principalCode = JobInvoice.principalCode AND
AccountDoc.jobCode = JobInvoice.jobCode
WHERE (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.docType = 'I')
AND (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode) FOR XML
PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,2,' '))
END AS invoiceNo
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON JobInvoice.principalCode = AccountDoc.principalCode AND
JobInvoice.jobCode = AccountDoc.jobCode
WHERE (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode)
OUTPUT:
----invoiceNo----
T17080003
INV14080011
Explanation:
I want to select docNo from table AccountDoc if AccountDoc.docType = I.
Or select invoiceNo from table JobInvoice if AccountDoc.docType = M.
The problem is what if under same jobCode there have 2 docType which are M and I, how I gonna display these 2 invoices?
You can achieve this by using CTE and FOR XML. below is the sample code i created using similar tables you have -
Create table #AccountDoc (
id int ,
docType char(1),
docNo varchar(10)
)
Create table #JobInvoice (
id int ,
invoiceNo varchar(10)
)
insert into #AccountDoc
select 1 , 'M' ,'M1234'
union all select 2 , 'M' ,'M2345'
union all select 3 , 'M' ,'M3456'
union all select 4 , 'I' ,'I1234'
union all select 5 , 'I' ,'I2345'
union all select 6 , 'I' ,'I3456'
insert into #JobInvoice
select 1 , 'INV1234'
union all select 2 , 'INV2345'
union all select 3 , 'INV3456'
select *
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id
with cte as
(
select isnull( case t1.docType WHEN 'M' THEN t2.invoiceNo WHEN 'I' then
t1.docNo end ,'') invoiceNo
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id )
select invoiceNo + ',' from cte For XML PATH ('')
You need to pivot your data if you have situations where there are two rows, and you want two columns. Your sql is a bit messy, particularly the bit where you put an entire select statement inside a case when in the select part of another query. These two queries are virtually the same, you should look for a more optimal way of writing them. However, you can wrap your entire sql in the following:
select
Jobcode, shipmentsyskey, [M],[I]
from(
--YOUR ENTIRE SQL GOES HERE BETWEEN THESE BRACKETS. Do not alter anything else, just paste your entire sql here
) yoursql
pivot(
max(invoiceno)
for docType in([M],[I])
)pvt

How to Select * Where Everything is Distinct Except One Field

I'm trying to pull 6 records using the code below but there are some cases where the information is updated and therefore it is pulling duplicate records.
My code:
SELECT column2, count(*) as 'Count'
FROM ServiceTable p
join HIERARCHY h
on p.LOCATION_CODE = h.LOCATION
where Report_date between '2017-04-01' and '2017-04-30'
and Column1 = 'Issue '
and LOCATION = '8789'
and
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
not exists
(
select *
from ServiceTable p2
where p2.record_code = 'INCIDENT'
and p2.incident_id = p.incident_id
)
)
)
The problem is that instead of the six records it is pulling eight. I would just use distinct * but the file_date is different on the duplicate entries:
FILE_DATE Incident_ID Column1 Column2
4/4/17 123 Issue Service - Red
4/4/17 123 Issue Service - Blue
4/5/17 123 Issue Service - Red
4/5/17 123 Issue Service - Blue
The desired output is:
COLUMN2 COUNT
Service - Red 1
Service - Blue 1
Any help would be greatly appreciated! If you need any other info just let me know.
If you turn your original select statement without the aggregation function into a subquery, you can distinct that on your values that are not the changing date, then select a COUNT from there. Don't forget your GROUP BY clause at the end.
SELECT Column2, COUNT(Incident_ID) AS Service_Count
FROM (SELECT DISTINCT Incident_ID, Column1, Column2
FROM ServiceTable p
JOIN HIERARCHY h ON p.LOCATION_CODE = h.LOCATION
WHERE Report_date BETWEEN '2017-04-01' AND '2017-04-30'
AND Column1 = 'Issue '
AND LOCATION = '8789'
AND
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
NOT EXISTS
(
SELECT *
FROM ServiceTable p2
WHERE p2.record_code = 'INCIDENT'
AND p2.incident_id = p.incident_id)
)
)
)
GROUP BY Column2
Also, if you are joining tables it is a good practice to fully qualify the field you are selecting. Example: p.Column2, p.Incident_ID, h.LOCATION. That way, even your distinct fields are easier to follow where they came from and how they relate.
Finally, don't forget that COUNT is a reserved word. I modified your alias accordingly.
If you are using an aggregation function (count), you should use group by for the column not in the aggregation function:
SELECT column2, count(*) as 'Count'
FROM ServiceTable p
join HIERARCHY h
on p.LOCATION_CODE = h.LOCATION
where Report_date between '2017-04-01' and '2017-04-30'
and Column1 = 'Issue '
and LOCATION = '8789'
and
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
not exists
(
select *
from ServiceTable p2
where p2.record_code = 'INCIDENT'
and p2.incident_id = p.incident_id
)
)
)
group by column2

SQL Query takes to long to return data

Looking for a better way to write this query and my SQL skills aren't great, basic really so looking for any pointers to make this better. This is only the first two columns and the full report will have a further 10.
I'm taking a specific set of repair types and doing analysis on them with counts and calculations. The 1st is jobs brought forward to the current financial year and the second is total amount of jobs currently received.
SELECT
"Type",
(
SELECT
NVL (COUNT(jjo.jjobno), 0)
FROM
jjobh jjo
WHERE
jjo.jclcode = 'L'
AND jjo.jstatus <> '6'
AND jjo.year_rec <> (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
AND (
week_comp IS NULL
OR year_comp = (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
)
AND jjo.jrepair_type = "Type"
) AS "B/F",
(
SELECT
NVL (COUNT(jjo.jjobno), 0)
FROM
jjobh jjo
WHERE
jjo.jclcode = 'L'
AND jjo.jstatus <> '6'
AND jjo.year_rec = (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
AND jjo.jrepair_type = "Type"
) AS "Recvd"
FROM
(
SELECT
rep.repair_type_code AS "Type"
FROM
repair_type rep
WHERE
rep.client = 'L'
AND rep.work_centre = '004682'
ORDER BY
rep.repair_type_code
)
ORDER BY
"Type";
Your code is a mess. I suspect you want something like:
SELECT jjo.jrepair_type, count(*) as valbf
FROM (SELECT coalesce(COUNT(jjo.jjobno), 0)
FROM jjobh jjo cross join
(SELECT sub_code
FROM code_table
WHERE main_code = 'YEAR'
) sc
WHERE jjo.jclcode = 'L' AND
jjo.jstatus <> '6' AND
jjo.year_rec <> sc.sub_code AND
(week_comp IS NULL OR
year_comp = sc.sub_code
)
) jjo join
(SELECT rep.repair_type_code AS "Type"
FROM repair_type rep
WHERE rep.client = 'L' AND
rep.work_centre = '004682'
) rtc
on jjo.jrepair_type = rtc.repair_type_code
group by jjo.jrepair_type;
It looks like you want to join the "jjo" table to the "repair type code" table, producing information about each repair type. The order by in the subquery is useless.
My suggestion is to move the "jjo" table to the outer "from". You should also move the WHERE clauses to the outermost WHERE clause (which I didn't do). I haven't quite figured out the date logic, but this might get you on the right track.

sql server query how to have multiple column comparision with subquery

How can I make the update query to work based on the sub query?
How can I compare all these columns in the sub query to the columns in the update statement?
Is there some neat and clean way to do it?
The query I am trying with it is shown below:
UPDATE Temp_CropData
SET RecordStatus = 0,
Remarks = ISNULL(Remarks, '') +' Duplicate Records'
WHERE
(SELECT Commodity ,City,Period,CropCondition
FROM [Temp_CropData]
GROUP BY DDate,Commodity,City,Period,CropCondition
HAVING count(*) >1)
Try using MERGE:
MERGE INTO Temp_CropData
USING (
SELECT Commodity, City, Period, CropCondition
FROM Temp_CropData
GROUP
BY DDate, Commodity, City, Period, CropCondition
HAVING COUNT(*) > 1
) AS source
ON Temp_CropData.Commodity = source.Commodity
AND Temp_CropData.City = source.City
AND Temp_CropData.Period = source.Period
AND Temp_CropData.CropCondition = source.CropCondition
WHEN MATCHED THEN
UPDATE
SET RecordStatus = 0,
Remarks = ISNULL(Remarks, '') + ' Duplicate Records';
I'm slightly suspicious of the fact that your subquery's SELECT and GROUP BY clauses do not match, though (i.e. DDate is in the GROUP BY but not the SELECT).
Try this:
UPDATE cd
SET RecordStatus = 0,
Remarks = ISNULL(Remarks, '') +' Duplicate Records'
FROM Temp_CropData cd
JOIN (SELECT Commodity ,City,Period,CropCondition
FROM [Temp_CropData]
GROUP BY DDate,Commodity,City,Period,CropCondition
HAVING count(*) >1) dup
ON cd.DDate = dup.DDate AND cd.Commodity=dup.Commodity AND cd.City = dup.City
AND cd.Period = dup.Period AND cd.CropCondition = dup.CropCondition