how to set a condition for arrays in where clause [BigQuery] - sql

I have an array in my table - something like this:
I need to take into account only rows where 'top_authors.author' = 'Caivi" and 'top_authors.total_score' = 3
I was trying to use unnest function but still I get the error "No matching signature for operator = for argument types: ARRAY, STRING. Supported signatures: ANY = ANY"
Could you help mi with that?

You can unnest() in a subquery in the where clause:
where exists (select 1
from unnest(top_authors) ta
where ta.author = 'Caivi' and ta.total_score = 3
)
Or you can do this in the main query:
select . . .
from t cross join
unnest(top_authors) ta
where ta.author = 'Caivi' and ta.total_score = 3;
Assuming you don't have duplicates in the array, these should produce equivalent results.

Related

An object or column name is missing or empty. But its not?

I am on the 7th set of queries I have been working on and all of them have used SELECT * INTO some_table without an issue. For some reason tho the below query in SQL Server is throwing the error
An object or column name is missing or empty. For SELECT INTO
statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed.
Change the alias to a valid name.
Any Idea on what it could be?
Note that running the query without the select into will result in data being returned and displayed as expected.
Select * into MYDB.MY_TBL
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHAA"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'
You have two selects. I think you just want:
SELECT OT.U_ID AS "U_ID",
. . .
INTO MYDB.MY_TBL
FROM . . .
The INTO should follow the SELECT column list.
Or alternatively, you could use a subquery, but that does not seem necessary.
Where #GordonLinoff example works I realized what I forgot and it was my FROM () sub-query setup I normally use.
I will provide an example in contrast to Gordon's method.
IE:
Select * into MYDB.MY_TBL
FROM(
SELECT OT.U_ID AS "U_ID"
...
FROM MYDB.OTHER_TBL OT
WHERE OT.U_ID = 'TEST'
) SUB
Please note these two points:
1.You have two columns with given "CHA" alias
2.It Seems you need to rewrite the query as:
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA_DUPLICATE"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHA_DUPLICATE2"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
INTO MYDB.MY_TBL -- <<<<<<< NOTE
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

ADD a column to SQL table

I am trying to add a column to this query in SQL as below-
you can use xml to store all result from query to a column
CUSIP=(Select gs.holdingsymbol
from dbo.holdingsymbol_tbl gs where gs.holdingsymboltypeid=2 for xml path(''))
You don't need a subquery, you just need another condition on your JOIN, like so:
SELECT DISTINCT
h.tradingitemid
,h.securityid
,h.currencyId
,CUSIP = hs.holdingsymbol
FROM cpr..holding_tbl h
INNER JOIN cpr..HoldingSymbol_tbl hs ON h.holdingid = hs.holdingId AND hs.holdingsymboltypeid = 2
WHERE h.userCompanyId = 10;
You can try do this :
            
CUSIP=(Select TOP 1 gs.holdingsymbol
from dbo.holdingsymbol_tbl gs where gs.holdingsymboltypeid=2)

How to pass parameter to exists clause?

I have the following method in my model:
def is_user_in_role (security_user_id, role)
SecurityUser.joins(:security_users_roles)
.where(security_users_roles:{role:role})
.exists?("security_users.id=#{security_user_id}")
end
The issue is that the "security_user_id" is not "translated" correctly in the SQL statements. It is always interpreted as "0".
This is a simple output of the generated SQL passing 'Instructor' and '9' as parameters values:
SecurityUser Exists (0.0ms) SELECT 1 AS one FROM security_users INNER JOIN security_users_manage_securities ON security_users_manage_securities.security_user_id = security_users.id INNER JOIN security_users_roles ON security_users_roles.id = security_users_manage_securities.security_users_role_id WHERE security_users_roles.role = 'Instructor' AND security_users.id = 0 FETCH FIRST ROW ONLY
You can see at the end:
security_users.id = 0
Could you tell me how should I transform the exists clause in order to use it with parameter?
I have found it. In order to pass parameters in the exists clause, you should use an array like this:
def is_user_in_role (security_user_id, role)
SecurityUser.joins(:security_users_roles)
.where(security_users_roles:{role:role})
.exists?(["security_users.id=#{security_user_id}"])
end

sql select with one value of two where

This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.