I am trying to use a simple subquery to get a value but I get an error about cardinality
Query as follows:
SELECT va.variantId AS ItemNo,
c.Season,
SUBSTRING(va.variantId, 0, 7) AS ProductNo,
SUBSTRING(va.variantId, 0, 10) AS ArticleNo,
SUBSTRING(va.variantId, 0, 13) AS VariantNo,
(
SELECT VALUE p["value"]
FROM c
JOIN p IN c.OriginalData.presentation.productNameLong
WHERE c.ItemNo = '123456'
AND p.locale = 'en-GB'
AND c.Season = '201808'
AND c.brand = 'xxx'
) AS Title
FROM c
JOIN sm IN c.OriginalData.base.sales.summary.salesMarkets
JOIN ar IN sm.articles
JOIN va IN ar.variants
JOIN ch IN va.channels
WHERE c.ItemNo = '123456'
AND sm.salesMarket = 'SE'
AND ch.channelName = 'xxx'
The error is as follows:
Failed to query item for container rawdata:
Gateway Failed to Retrieve Query Plan: Message: {"errors":[{"severity":"Error","location":{"start":227,"end":498},"code":"SC2201","message":"The cardinality of a scalar subquery result set cannot be greater than one."}]}
ActivityId: 11ff3b08-d8a7-4737-9111-81f319cf1dc5, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0
The result of that subquery can only ever be 1 single result so I am not sure what it is complaining about
You need to use ARRAY expression to construct an array from subquery's results.
Please try this SQL:
SELECT va.variantId AS ItemNo,
c.Season,
SUBSTRING(va.variantId, 0, 7) AS ProductNo,
SUBSTRING(va.variantId, 0, 10) AS ArticleNo,
SUBSTRING(va.variantId, 0, 13) AS VariantNo,
ARRAY(
SELECT VALUE p["value"]
FROM c
JOIN p IN c.OriginalData.presentation.productNameLong
WHERE c.ItemNo = '123456'
AND p.locale = 'en-GB'
AND c.Season = '201808'
AND c.brand = 'xxx'
) AS Title
FROM c
JOIN sm IN c.OriginalData.base.sales.summary.salesMarkets
JOIN ar IN sm.articles
JOIN va IN ar.variants
JOIN ch IN va.channels
WHERE c.ItemNo = '123456'
AND sm.salesMarket = 'SE'
AND ch.channelName = 'xxx'
Related
There are two types of rows in document_files table - that have active=1 or active=0. Each document (document_id) can have multiple rows of each type. The goal is to for every document select all rows that have active=1, but only the latest one of all rows that have active=0.
select *
from documents d
inner join document_files df on df.document_id = d.id
where ...
-- df.active = 1, select them all
-- or df.active = 0, only select max(df.last_stamp) of all df.active=0 rows for this df.document_id
How to achieve this?
I'd use union and group by, but the problem is that a lot of columns are returned, and it wouldn't be optimal to group them all.
This section only get the document files with no active record and after that using max () gets the most recent record for the item:
select document_id
from (select * from document_files f where f.document_id not in
(select document_id from document_files where active = 1)) dff where dff.last_stamp =
(
select max(i.last_stamp) from document_files i where i.document_id = dff.document_id
)
My final sql :
select *
from documents d
inner join document_files df on df.document_id = d.id
where df.active = 1 or
df.document_id in
(
select document_id
from (select * from document_files f where f.document_id not in
(select document_id from document_files where active = 1)) dff where dff.last_stamp =
(
select max(i.last_stamp) from document_files i where i.document_id = dff.document_id
)
)
Please add some sql fiddle next time so we can test the result
In this query, I want to use with-statement. I have a subquery that calculates A union all B and I want to use it with with-statement. But when I use with-statement I face the error that says "table or view does not exist".
what surprises me is when I replace the first part with with-statement it works correctly. But when I replace the second part, I face this error!!
select
deposit.BRNCH_COD||'-'||deposit.DP_TYPE_COD||''||deposit.CUSTOMER_NUM||'-
'||deposit.DEPOSIT_SERIAL AS DEPOSIT_NUMBER,
deposit.IBAN AS IBAN,
deposit.CURRENCY_DESC AS DEPOSIT_CURRCOD,
deposit.BRNCH_COD AS BRNCH_COD,
MAIN_7.Still_Days
AS Still_Lenght,
to_char(MAIN_7.Startdate, 'yyyy/mm/dd' ,'nls_calendar=persian') AS
START_DATE,
MAIN_7.AMOUNT
AS TOTAL_AMOUNT,
MAIN_7.TRN_Count
AS TRN_Count
from
(
select Trans_Table.DEPOSIT_KEY AS DEPOSIT_KEY,
Trans_Table.TRN_Start_DATE AS Startdate,
MAX(Active_Time_Table.EFFECTIVE_DATE) AS Lastdate,
H.PASSIVE_DAYS AS Still_Days,
SUM(Active_Time_Table.AMOUNT) AS AMOUNT,
Count(Active_Time_Table.AMOUNT) AS TRN_Count
from
(
Select F.DEPOSIT_KEY,
SUM (F.AMOUNT) AS TRN_AMOUNT,
MIN (F.EFFECTIVE_DATE) AS TRN_Start_DATE
from
(
A
union all
B
)F
Group by (F.DEPOSIT_KEY)
Having ( SUM (F.AMOUNT) >10000000000)
)Trans_Table
inner join
H
on (Trans_Table.DEPOSIT_KEY = H.DEPOSIT_KEY and
Trans_Table.TRN_Start_DATE-1 = H.EFFECTIVE_DATE)
inner join
(
A
union all
B
)Active_Time_Table
on (Trans_Table.DEPOSIT_KEY = Active_Time_Table.DEPOSIT_KEY and
Active_Time_Table.EFFECTIVE_DATE - Trans_Table.TRN_Start_DATE< 4 and
Active_Time_Table.EFFECTIVE_DATE - Trans_Table.TRN_Start_DATE>=0)
group by ( Trans_Table.DEPOSIT_KEY ,
Trans_Table.TRN_Start_DATE,H.PASSIVE_DAYS)
Having (SUM(Active_Time_Table.AMOUNT)) > 10000000000
)MAIN_7
inner join dimamldeposit deposit
on deposit.DEPOSIT_KEY = MAIN_7.DEPOSIT_KEY
***********************************************************
with rep as
(A union all B)
select
deposit.BRNCH_COD||'-'||deposit.DP_TYPE_COD||'-
'||deposit.CUSTOMER_NUM||'-'||deposit.DEPOSIT_SERIAL AS DEPOSIT_NUMBER,
deposit.IBAN AS IBAN,
deposit.CURRENCY_DESC AS DEPOSIT_CURRCOD,
deposit.BRNCH_COD AS BRNCH_COD,
MAIN_7.Still_Days AS Still_Lenght,
to_char(MAIN_7.Startdate, 'yyyy/mm/dd' ,'nls_calendar=persian') AS START_DATE,
MAIN_7.AMOUNT AS TOTAL_AMOUNT,
MAIN_7.TRN_Count AS TRN_Count
from
(
select Trans_Table.DEPOSIT_KEY AS DEPOSIT_KEY,
Trans_Table.TRN_Start_DATE AS Startdate,
MAX(rep.EFFECTIVE_DATE) AS Lastdate,
H.PASSIVE_DAYS AS Still_Days,
SUM(rep.AMOUNT) AS AMOUNT,
Count(rep.AMOUNT) AS TRN_Count
from
(
Select rep.DEPOSIT_KEY,
SUM (rep.AMOUNT) AS TRN_AMOUNT,
MIN (rep.EFFECTIVE_DATE) AS TRN_Start_DATE
from
rep
Group by (rep.DEPOSIT_KEY)
Having ( SUM (rep.AMOUNT) >10000000000)
)Trans_Table
inner join
H
on (Trans_Table.DEPOSIT_KEY = H.DEPOSIT_KEY and Trans_Table.TRN_Start_DATE-1 = H.EFFECTIVE_DATE)
inner join
rep rep
on (Trans_Table.DEPOSIT_KEY = rep.DEPOSIT_KEY and rep.EFFECTIVE_DATE - Trans_Table.TRN_Start_DATE< 4 and rep.EFFECTIVE_DATE - Trans_Table.TRN_Start_DATE>=0)
group by ( Trans_Table.DEPOSIT_KEY , Trans_Table.TRN_Start_DATE,H.PASSIVE_DAYS)
Having (SUM(rep.AMOUNT)) > 10000000000
)MAIN_7
inner join dimamldeposit deposit
on deposit.DEPOSIT_KEY = MAIN_7.DEPOSIT_KEY
That's a lot of code, but - to make it simple, I'd suggest you use WITH factoring clause as the first command, include all tables you use into it, and then - as the final SELECT - fetch data from all those CTEs. Something like this:
with
a as (select ... from ...),
b as (select ... from ...),
f as (select ... from ...),
...
select a.col1, b.col2, f.col3
from a join b on a.id = b.id
left join f on f.id = b.id
where ...
I am writing this code in SQL and getting the following error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
7/2 PL/SQL: SQL Statement ignored
Statement:
CREATE OR REPLACE FUNCTION GET_C_O(vretail_outlet_id int,
vtran_year varchar)
RETURN number
AS
stock number;
BEGIN
select vretail_outlet_id,
SUM(GET_C_O_STOCKFN_TEST(61, a.BATCHID, 1,
to_date('204-04-01','yyyy-mm-dd'),
vretail_outlet_id, vtran_year, 1) * QP.PRODUCTRATE) AS OPENING
From Promas
inner join Product A
on Promas.Pcode = A.Pcode and
vretail_outlet_id = A.Retail_outlet_id and
vtran_year = a.tran_year
inner Join QryProductrate Qp
on Qp.batchid = a.batchid and
QP.Retail_outlet_id = a.REtail_outlet_id and
Qp.Tran_year = a.tran_year
where a.tran_year = '2014_2015' and
1=1
group by vretail_outlet_id)
into stock
from dual;
Return Stock;
END;
The SELECT statement is not correct. Try:
select SUM(GET_C_O_STOCKFN_TEST(61, a.BATCHID, 1,
to_date('2014-04-01','yyyy-mm-dd'),
vretail_outlet_id, vtran_year, 1) * QP.PRODUCTRATE) AS OPENING
into stock
From Promas
inner join Product A
on Promas.Pcode = A.Pcode and
vretail_outlet_id = A.Retail_outlet_id and
vtran_year = a.tran_year
inner Join QryProductrate Qp
on Qp.batchid = a.batchid and
QP.Retail_outlet_id = a.REtail_outlet_id and
Qp.Tran_year = a.tran_year
where a.tran_year = '2014_2015';
Share and enjoy.
I'm creating an e-commerce benchmark website. I have some problems in my GETSITES stored procedure:
Code:
CREATE PROC USER_S_GETSITES
#CategoryID int,
#List int,
#IsPopular bit,
#MaxID int,
#Status bit,
#Word text
AS
SELECT s.*, c.*, AVG(r.Rate) AS RateAVG, Count(r.ID) AS RateCount
FROM Sites AS s
INNER JOIN Rates AS r ON r.SiteID = s.ID
INNER JOIN Categories AS c ON c.ID = s.CategoryID
WHERE (#CategoryID is NULL) OR s.CategoryID = #CategoryID
AND (#MaxID is NULL) OR s.ID < #MaxID
AND (#Status is NULL) OR s.Status = #Status
AND r.Status=1
AND (#Word is NULL) OR s.Name LIKE #Word OR s.Description LIKE #Word
AND (#IsPopular is NULL) OR s.IsPopular=#IsPopular
ORDER BY
CASE #List WHEN 1 THEN s.ID END ASC,
CASE #List WHEN 2 THEN s.ID END DESC,
CASE #List WHEN 3 THEN RateAVG END DESC,
CASE #List WHEN 4 THEN RateCount END DESC
And my problems :
Msg 8120, Level 16, State 1, Procedure USER_S_GETSITES, Line 9
Column 'Sites.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 207, Level 16, State 1, Procedure USER_S_GETSITES, Line 23
Invalid column name 'RateAVG'.
Msg 207, Level 16, State 1, Procedure USER_S_GETSITES, Line 24
Invalid column name 'RateCount'.
I can't solve these problems. What should I do?
Error 1: include column Sites.ID in GROUP BY clause like group by Sites.ID, ....
Error 2: CASE #List WHEN 3 THEN AVG(r.Rate) AS RateAVG END DESC
Error 3: CASE #List WHEN 4 THEN Count(r.ID) AS RateCount END DESC
I'm trying to query the model from my Symfony2 project and I am not able to replace wildcards into my DQL query. Look;
$q2 =
'SELECT
p.codigo,
p.descripcion,
SUM(l.cantidad) as cantidad,
SUM(l.cantidad*l.pvp) as euros
FROM
MGFAppBundle:LineaVenta l
JOIN
MGFAppBundle:Producto p
WITH
l.producto = p.id
JOIN
l.venta v
WITH
l.venta = v.id
WHERE
l.producto IN (:array)
AND
v.farmacia = :farmacia
GROUP BY
p.codigo';
$query2 = $this->em->createQuery($q2)
->setParameter('farmacia', $farmacia)->setParameter('array', $array);
$porFarmacia = $query2->getResult();
// This does not return a single value, when it should return 2 lines.
echo $query2->getSQL();
// Returns:
// SELECT p0_.codigo AS codigo0, p0_.descripcion AS descripcion1, SUM(l1_.cantidad) AS sclr2, SUM(l1_.cantidad * l1_.pvp) AS sclr3 FROM LineaVenta l1_ INNER JOIN Producto p0_ ON (l1_.lineaventas_id = p0_.id) INNER JOIN Venta v2_ ON l1_.venta_id = v2_.id AND (l1_.venta_id = v2_.id) WHERE l1_.lineaventas_id IN (?) AND v2_.farmacia_id = ? GROUP BY p0_.codigo
So, question marks where parameters should be. Any hint? Thanks in advance.