single-row subquery returns more than one row. Query not working with main query - sql

I hve to display several cell values into one cell. So I am using this query:
select LISTAGG(fc.DESCRIPTION, ';'||chr(10))WITHIN GROUP (ORDER BY fc.SWITCH_NAME) AS DESCRIP from "ORS".SWITCH_OPERATIONS fc
group by fc.SWITCH_NAME
It is working fine. But when I am merging this with my main(complete) query then I am getting the error as: Error code 1427, SQL state 21000: ORA-01427: single-row subquery returns more than one row
Here is my complete query:
SELECT
TRACK_EVENT.LOCATION,
TRACK_EVENT.ELEMENT_NAME,
(select COUNT(*) from ORS.TRACK_EVENT b where (b.ELEMENT_NAME = sw.SWITCH_NAME)AND (b.ELEMENT_TYPE = 'SWITCH')AND (b.EVENT_TYPE = 'I')AND (b.ELEMENT_STATE = 'NORMAL' OR b.ELEMENT_STATE = 'REVERSE'))as COUNTER,
(select COUNT(*) from ORS.SWITCH_OPERATIONS fc where TRACK_EVENT.ELEMENT_NAME = fc.SWITCH_NAME and fc.NO_CORRESPONDENCE = 1 )as FAIL_COUNT,
(select MAX(cw.COMMAND_TIME) from ORS.SWITCH_OPERATIONS cw where ((TRACK_EVENT.ELEMENT_NAME = cw.SWITCH_NAME) and (cw.NO_CORRESPONDENCE = 1)) group by cw.SWITCH_NAME ) as FAILURE_DATE,
(select LISTAGG(fc.DESCRIPTION, ';'||chr(10))WITHIN GROUP (ORDER BY fc.SWITCH_NAME) AS DESCRIP from "ORS".SWITCH_OPERATIONS fc
group by fc.SWITCH_NAME)
FROM
ORS.SWITCH_OPERATIONS sw,
ORS.TRACK_EVENT TRACK_EVENT
WHERE
sw.SEQUENCE_ID = TRACK_EVENT.SEQUENCE_ID

Not only are subqueries in the SELECT list required to return exactly one row (or any time they're used for a singular comparison, like <, =, etc), but their use in that context tends to make the database execute them RBAR - Row-by-agonizing-row. That is, they're slower and consume more resources than they should.
Generally, unless the result set outside the subquery contains only a few rows, you want to construct subqueries as part of a table-reference. Ie, something like:
SELECT m.n, m.z, aliasForSomeTable.a, aliasForSomeTabe.bSum
FROM mainTable m
JOIN (SELECT a, SUM(b) AS bSum
FROM someTable
GROUP BY a) aliasForSomeTable
ON aliasForSomeTable.a = m.a
This benefits you in other ways to - it's easier to get multiple columns out of the same table-reference, for example.
Assuming that LISTAGG(...) can be included with other aggregate functions, you can change your query to look like this:
SELECT Track_Event.location, Track_Event.element_name,
Counted_Events.counter,
Failure.fail_count, Failure.failure_date, Failure.descrip
FROM ORS.Track_Event
JOIN ORS.Switch_Operations
ON Switch_Operations.sequence_id = Track_Event.sequence_id
LEFT JOIN (SELECT element_name, COUNT(*) AS counter
FROM ORS.Track_Event
WHERE element_type = 'SWITCH'
AND event_type = 'I'
AND element_state IN ('NORMAL', 'REVERSE')
GROUP BY element_name) Counted_Events
ON Counted_Events.element_name = Switch_Operations.swicth_name
LEFT JOIN (SELECT switch_name,
COUNT(CASE WHEN no_correspondence = 1 THEN '1' END) AS fail_count,
MAX(CASE WHEN no_correspondence = 1 THEN command_time END) AS failure_date,
LISTAGG(description, ';' || CHAR(10)) WITHIN GROUP (ORDER BY command_time) AS descrip
FROM ORS.Switch_Operations
GROUP BY switch_name) Failure
ON Failure.switch_name = Track_Event.element_name
This query was written to (attempt to) preserve the semantics of your original query. I'm not completely sure that's what you actually need but without sample starting data and desired results, I have no way to tell how else to improve this. For instance, I'm a little suspicious of the need of Switch_Operations in the outer query, and the fact that LISTAGG(...) is run over row where no_correspondence <> 1. I did change the ordering of LISTAGG(...), because the original column would not have done anything (because the order way the same as the grouping), so would not have been a stable sort.

Single-row subquery returns more than one row.
This error message is self descriptive.
Returned field can't have multiple values and your subquery returns more than one row.
In your complete query you specify fields to be returned. The last field expects single value from the subquery but gets multiple rows instead.
I have no clue about the data you're working with but either you have to ensure that subquery returns only one row or you have to redesign the wrapping query (possibly using joins when appropriate).

Related

Agregating a subquery

I try to find what I missed in the code to retrieve the value of "Last_Maintenance" in a table called "Interventions".
I try to understand the order rules of SQL and the particularities of subqueries without success.
Did I missed something, something basic or an important step?
---Interventions with PkState "Schedule_Visit" with the Last_Maintenance aggregation
SELECT Interventions.ID AS Nro_Inter,
--Interventions.PlacesList AS Nro_Place,
MaintenanceContracts.Num AS Nro_Contract,
Interventions.TentativeDate AS Schedule_Visit,
--MaintenanceContracts.NumberOfVisits AS Number_Visits_Contracts,
--Interventions.VisitNumber AS Visit_Number,
(SELECT MAX(Interventions.AssignmentDate)
FROM Interventions
WHERE PkState = 'AE4B42CF-0003-4796-89F2-2881527DFB26' AND PkMaintenanceContract IS NOT NULL) AS Last_Maintenance --PkState "Maintenance Executed"
FROM Interventions
INNER JOIN MaintenanceContracts ON MaintenanceContracts.Pk = Interventions.PkMaintenanceContract
WHERE PkState = 'AE4B42CF-0000-4796-89F2-2881527ABC26' AND PkMaintenanceContract IS NOT NULL --PkState "Schedule_Visit"
GROUP BY Interventions.AssignmentDate,
Interventions.ID,
Interventions.PlacesList,
MaintenanceContracts.Num,
Interventions.TentativeDate,
MaintenanceContracts.NumberOfVisits,
Interventions.VisitNumber
ORDER BY Nro_Contract
I try to use GROUP BY and HAVING clause in a sub query, I did not succeed. Clearly I am lacking some understanding.
Output
The output of "Last_Maintenance" is the last date of entire contracts in the DB, which is not the desirable output. The desirable output is to know the last date the maintenance was executed for each row, meaning, for each "Nro-Contract". Somehow I need to aggregate like I did below.
In opposition of what mention I did succeed in another table.
In the table Contracts I did had success as you can see.
SELECT
MaintenanceContracts.Num AS Nro_Contract,
MAX(Interventions.AssignmentDate) AS Last_Maintenance
--MaintenanceContracts.Name AS Place
--MaintenanceContracts.StartDate,
--MaintenanceContracts.EndDate
FROM MaintenanceContracts
INNER JOIN Interventions ON Interventions.PkMaintenanceContract = MaintenanceContracts.Pk
WHERE MaintenanceContracts.ActiveContract = 2 OR MaintenanceContracts.ActiveContract = 1 --// 2 = Inactive; 1 = Active
GROUP BY MaintenanceContracts.Num, MaintenanceContracts.Name,
MaintenanceContracts.StartDate,
MaintenanceContracts.EndDate
ORDER BY Nro_Contract
I am struggling to understanding how nested queries works and how I can leverage in a simple manner the queries.
I think you're mixed up in how aggregation works. The MAX function will get a single MAX value over the entire dataset. What you're trying to do is get a MAX for each unique ID. For that, you either use derived tables, subqueries or windowed functions. I'm a fan of using the ROW_NUMBER() function to assign a sequence number. If you do it correctly, you can use that row number to get just the most recent record from a dataset. From your description, it sounds like you always want to have the contract and then get some max values for that contract. If that is the case, then you're second query is closer to what you need. Using windowed functions in derived queries has the added benefit of not having to worry about using the GROUP BY clause. Try this:
SELECT
MaintenanceContracts.Num AS Nro_Contract,
--MaintenanceContracts.Name AS Place
--MaintenanceContracts.StartDate,
--MaintenanceContracts.EndDate
i.AssignmentDate as Last_Maintenance
FROM MaintenanceContracts
INNER JOIN (
SELECT *
--This fuction will order the records for each maintenance contract.
--The most recent intervention will have a row_num = 1
, ROW_NUMBER() OVER(PARTITION BY PkMaintenanceContract ORDER BY AssignmentDate DESC) as row_num
FROM Interventions
) as i
ON i.PkMaintenanceContract = MaintenanceContracts.Pk
AND i.row_num = 1 --Used to get the most recent intervention.
WHERE MaintenanceContracts.ActiveContract = 2
OR MaintenanceContracts.ActiveContract = 1 --// 2 = Inactive; 1 = Active
ORDER BY Nro_Contract
;

Snowflake: SQL compilation error: not a valid group by expression

I'm trying to call a window function inside of a case statement, as such:
SELECT
DISTINCT properties.property_id
COALESCE(MAX(CASE
WHEN units_count.unit_type = 'NORMAL' THEN units_count.unit_count
END) OVER (PARTITION BY properties.property_id),
0)::INT AS normal_units_count
FROM units_count
JOIN properties ON units_count.property_id = properties.property_id
I'm receiving the following error:
SQL compilation error: [IFF(UNITS_COUNT.UNIT_TYPE = 'NORMAL', UNITS_COUNT.UNIT_COUNT, SYSTEM$NULL_TO_FIXED(null))] is not a valid group by expression
I've tried adding a qualify clause to remove the MAX() function:
SELECT
DISTINCT properties.property_id
COALESCE(CASE
WHEN units_count.unit_type = 'NORMAL' THEN units_count.unit_count
END OVER (PARTITION BY properties.property_id),
0)::INT AS normal_units_count
FROM units_count
JOIN properties ON units_count.property_id = properties.property_id
QUALIFY units_count.unit_count = MAX(units_count.unit_count) OVER (PARTITION BY properties.property_id)
The code executes, but the qualify clause results in unwanted filtering for other fields. Can I keep the existing logic (with MAX()) or do I need to include a qualify clause?
The DISTINCT is in effect a grouping operation, but your MAX is a for every row with the OVER clause.
which implies as "basic SQL" this should work:
SELECT
p.property_id,
MAX(IFF(uc.unit_type = 'NORMAL', uc.unit_count, 0)) as v1 max_units_count
FROM units_count AS uc
JOIN properties AS p
ON uc.property_id = p.property_id
GROUP BY 1
but as your "other fields" implies you are selecting other things and without knowing what/how you are doing that is hard to see what you are wanting todo.
also your max(unit_count) does not so much feel like it is a normal_unit_count.
But you example needs to pull in a second+ column of what you are wanting to do to see how they should be co-handled. But I would be inclined with zero information to suggest you use a CTE to find the per property_id the MAX unit_count and then join that result to a second read from your data. Because with zero insights to the other operations and how they could re-use that read, I have experienced it is better to GROUP/JOIN then WINDOW(over partition)/ANYVALUE. But that is another option.
Thus (without testing) this might work:
SELECT
DISTINCT p.property_id
ANYVALUE(COALESCE(MAX(CASE
WHEN uc.unit_type = 'NORMAL' THEN uc.unit_count
END) OVER (PARTITION BY p.property_id),
0)::INT) AS normal_units_count
FROM units_count AS uc
JOIN properties AS p
ON uc.property_id = p.property_id

Getting count of latest items from secondary view

I've got a problem constructing a somewhat advanced query.
I have two views - A and B where B is the child of A.
This relationship is handled by
A vw_StartDate.MapToID
=
B vw_TrackerFeaturesBasic.StartDateMapToID.
What I need to do is grab every parent A and a count of the LATEST added children B.
This is a query that gets the latest children B in a SSRS-report: (This does not use A at all!):
/****** Selecting the incomplete, applicable issues of the latest insert. ******/
SELECT DISTINCT [TRK_Feature_LKID]
,[TrackerGroupDescription]
,[ApplicableToProject]
,[ReadyForWork]
,[DateStamp]
FROM [vw_TRK_TrackerFeaturesBasic] as temp
WHERE ApplicableToProject = 1
AND DateStamp = (SELECT MAX(DateStamp) from [vw_TRK_TrackerFeaturesBasic] WHERE [TRK_StartDateID] = #WSCTrackerID AND StartDateMapToID = #HierarchyID AND [TRK_Feature_LKID] = temp.TRK_Feature_LKID )
ORDER BY DateStamp DESC
I've tried a few different ways, but I can't figure out how to get the latest added children from the subquery (I've mainly used a subquery nestled in a COUNT / Case + SUM).
Since SQL Server doesn't really allow us to use aggregate functions in aggregate functions I'm not sure how to get the latest added item in a subquery as the subquery most likely has to be nested in a COUNT or something similar.
Below is a version I'm working on (doesn't work):
Column 'vw_TRK_TrackerFeaturesBasic.StartDateMapToID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT b.TRK_StartDateID
,(SELECT COUNT(b.TRK_Feature_LKID) FROM b )
FROM vw_TRK_StartDate as a
LEFT JOIN vw_TRK_TrackerFeaturesBasic as b
ON b.StartDateMapToID = a.MapToID AND b.DateStamp = (SELECT MAX(DateStamp) FROM [vw_TRK_TrackerFeaturesBasic] WHERE [TRK_StartDateID] = 47 AND [StartDateMapToID] = 13)
WHERE MapToId = 13
--(SELECT MAX(DateStamp) from [vw_TRK_TrackerFeaturesBasic] WHERE [TRK_StartDateID] = #WSCTrackerID AND StartDateMapToID = #HierarchyID AND [TRK_Feature_LKID] = temp.TRK_Feature_LKID
GROUP BY b.TRK_StartDateID
Your question is a bit hard to follow, because I don't see a relationship between your queries and this request:
What I need to do is grab every parent A and a count of the LATEST
added children B.
Focusing on this statement, you can do this readily with window functions:
SELECT b.StartDateMapToID, COUNT(*)
FROM (SELECT tfb.*,
MAX(tfb.DateStamp) OVER (PARTITION BY tfb.StartDateMapToID) as max_DateStamp
FROM vw_TRK_TrackerFeaturesBasic tfb
) tfb
GROUP BY b.StartDateMapToID;

IBM DB2: Using MINUS to exclude information in the subselect statement

Currently I am having an issue bringing back the correct data for this particular query below. I am attempting to bring back data that excludes select criteria from the subselect statement after MINUS keyword.
SELECT
DISTINCT ORDER.OWNER, ORDER_H.PO_ID
FROM ORDER ORDER
WHERE ORDER.TYPE != 'X'
AND ORDER.STATUS='10'
AND ORDER.CLOSE_DATE IS NULL MINUS
(
SELECT
DISTINCT ORDER.OWNER, ORDER.PO_ID
FROM ORDER ORDER
INNER JOIN COST COST ON COST.PO_ID = ORDER.PO_ID
AND COST.CODE IN
(
'LGSF',
'DFCDC',
'BOF',
'TFR',
'RFR',
'TFLHC',
'BF',
'CBF',
'CHAP',
'DYPH' ,
'OFFP',
'PTWT',
'DTEN',
'OTHR',
'DMSG',
'STOR',
'TOF',
'ANTCV',
'ANTIP',
'CVD',
'TRAN'
)
WHERE ORDER.TYPE != 'OTR'
AND ORDER.STATUS = '10'
AND (COST.E_AMT > 0 AND COST.A_AMT IS NULL)
)
FOR READ ONLY WITH UR
The data coming back includes the data within the subquery instead of excluding this data from the resultset. I cannot figure out why this is the case. Does anyone have any idea why after MINUS it doesn't exclude this data and is bringing back data where COST.E_AMT is actually greater than 0 and COST.A_AMT is actually populated for each CODE listed in the subquery? Any help would be appreciated, thanks.

MAX Subquery in SQL Anywhere Returning Error

In sqlanywhere 12 I wrote the following query which returns two rows of data:
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Which returns:
'**United Natural Foods IN','2018-02-07 15:05:15.513'
'Flora ','2018-02-07 14:40:07.491'
I would like to only return the row with the maximum timestamp in the column "ORD_Timestamp". After simply trying to select by MAX("eDatabase"."OrderingInfo"."ORD_Timestamp") I found a number of posts describing how that method doesn't work and to use a subquery to obtain the results.
I'm having difficulty creating the subquery in a way that works and with the following query I'm getting a syntax error on my last "ON":
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM ( "eDatabase"."OrderingInfo"
JOIN
"eDatabase"."OrderingInfo"
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK" )
INNER JOIN
(SELECT "eDatabase"."Vendor"."VEN_CompanyName", MAX("eDatabase"."OrderingInfo"."ORD_Timestamp")
FROM "eDatabase"."OrderingInfo")
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Does anyone know how I can adjust this to make the query correctly select only the max ORD_Timestamp row?
try this:
SELECT TOP 1 "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
order by "ORD_Timestamp" desc
this orders them biggest on to and say only hsow the top row