ADD a column to SQL table - sql

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)

Related

SQL Query Returning Unwanted Data Which was Filtered on Where Statement

I've got the following query: When I execute the query, I get a lot of results for ad.Branch, including 106804. If I remove BOR.SSN like and COB.SSN like, and just leave branch, it returns only 106804 records which is the desired result. So I believe the like statement is what's causing the issues.
declare #Social varchar(5);
set #social = '%####'
SELECT
ABC.AccountNumber
FROM gcc.AccountDailyFact ABC
JOIN gcc.Today T ON ABC.CalendarDate = T.Today
JOIN gcc.ADReference AD ON ABC.ADReferenceRowId = AD.ADReferenceRowId
JOIN gcc.CDAccountData BOR ON ABC.BDLeaseHolderRowId = BOR.CDAccountDataRowId
JOIN gcc.CDAccountData COB ON ABC.CoBDLeaseHolderRowId = COB.CDAccountDataRowId
JOIN gcc.VDAssetFile VD ON ABC.VDAssetFileRowID = VD.VDAssetFileRowID
WHERE BOR.SSN like #social or COB.Ssn like #social and ad.Branch in
('106804')
I think you just need parentheses:
WHERE (BOR.SSN like #social or COB.Ssn like #social) and
ad.Branch in ('106804')

Pentaho CDE SQL Query with numerical param

I would like to know if it is possible to perform an SQL query and pass it a numerical parameter. Let's suppose that I have the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As quarter
, ROUND(SUM(fact_Ventas.cantidad * precioUnitario),0) as amount
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
I want to divide the column amount, by a numerical value extracted from a simple parameter. I managed to use filters in the where clause, but I can not make a division in a column.
Try the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As cuatrimestre
, ROUND( (SUM(fact_Ventas.cantidad * precioUnitario)/${paramValue}),0) as Importe
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
But it gives an error and the data is not loaded. The syntax of the query was tested in the database and is correct.
Did you set the parameter type to Numeric? It defaults to String which was probably fine for your other parameters.
The problem was that I forgot to assign the parameter to the component, I had associated it with SQL Query but not with the component.

Group_concat is not a built in function - HeidiSQL

I have a query with a group_concat in it but it returns an error that the group_concat is not a built in function. Please help me to resolve this. My query is like this:
SELECT cast(user_id as varchar(255)) AS member_id,skincareproductbarcode.barcode, skincareproduct.SCP_id, skincabinet.beautybox_id,
skincabinet.skpid, skincareproduct.photos, skincabinet.create_date, GROUP_CONCAT(MirrorProfile.serial_number, '') As Serial_Number
from skincabinet join skincareproduct on skincareproduct.scp_id = skincabinet.skpid
full join skincareproductbarcode on skincabinet.skpid = skincareproductbarcode.scp_id
full join membermirrorprofile on skincabinet.user_id = membermirrorprofile.member_id
full join mirrorprofile on MemberMirrorProfile.mirror_id = mirrorProfile.mirror_id
where skincareproduct.approval_flag = 'N' and skincareproduct.photos != ''
and substring(photos,52,3) = 'scp' order by skincareproduct.SCP_id group by skincareproduct.scp_id desc
How can I use group_concat or is there any other way around?
the concat of fields should not be the output of the SQL query. It is not made for that. Use the tool which receive the data to make this action.

SQL query to combine 2 columns possible?

This query is not what we need. the 2 ENT# need to be IHGRC# if = adent# + adsfx. that is how they store in the app. Can that be combined that way? For example
HRGRC# = 2558807001
adent# = 2558807
adsfx# = 001
this would be a match.
SELECT OEINH2.IHDOCD, OEINH2.IHINV#, OEINH2.IHGRC#, ADRES1.ADINTA, ADRES1.ADENT#, ADRES1.ADSFX#
FROM ASTDTA.OEINH2
INNER JOIN ASTDTA.ADRES1 ON OEINH2.IHENT#=ADRES1.ADENT#
ORDER BY OEINH2.IHINV#
If you just want to concatenate the values, you can use concat():
select concat(adent#, adsfx) as HRGRC#
or use the concatenation operator:
select(adent# || adsfx) as HRGRC#

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.