Output of a column to only have two decimal numbers? - sql-server

I'm showing the query output on a default ViewGrid on a VB.NET web application and some of the output values give me 4 decimal values while I only need two decimal values do to the result for showing a currency.
How can I specify in the query to only show me a maximum of two decimal values for the values of D.ORIG_PRC and D.DISC_AMT?
My Query
SELECT H.Emp_ID, H.Cust_ID, H.Rgst_ID, D.TRAN_LN_NUM, D.DISC_CD, D.AUTH_EMP_ID, D.ORIG_PRC, D.DISC_AMT, D.DISC_PCT, D.GL_ACCT_ID
FROM Transaction_Header H
INNER Join LN_Detail L ON (H.Str_ID = L.Str_ID And H.Rgst_ID = L.Rgst_ID And H.Tran_ID = L.Tran_ID)
INNER Join LN_Discount D ON (L.Str_ID = D.Str_ID And L.Rgst_ID = D.Rgst_ID And L.Tran_ID = D.Tran_ID And L.Tran_LN_Num = D.Tran_LN_Num)
WHERE(H.Str_ID = #Str_ID)
And (H.Tran_ID = #Tran_ID)
And ((H.Rgst_ID = #Rgst_ID) Or (#Rgst_ID Is NULL Or #Rgst_ID = ''))

Related

How to Query Data with Specified Multiple Field Values?

I have a table as follows:
Id = stretch(1..100,1000)
area = take(`A`B`C`D`E`F`G`H`I`J,1000)
qyt = rand(100.00,1000)
t = table(Id,area,qyt)
I want to query data with multiple field values.
Query condition:
field value “Id=1“, “area“ = A, F, G;
field value “Id=2“, “area“ = B, C, D;
field value “Id=3“, “area“ = B, C, G;
I tried the following method, querying each record with a where clause.
select * from t where (Id =1 and area = `A) or (Id = 1 and area = `F) or(Id = 1 and area = `G)
The code is simple but long. Is there any easier way to solve this problem?
The query condition involves field “Id“ and “area“. First, you can add a new field “newCol“ that combines these two fields.
temp = select *,string(Id)+area as newCol from t
Then, conduct a query as follows:
aim_row = `1A`1F`1G`2B`2C`2D`3B`3C`3G
result = select Id,area, qyt from temp where newCol in aim_row
Output:
Id area qyt
-- ---- ------------------
1 A 30.710145598277449
1 F 37.276206677779555
1 G 81.456008832901716
2 B 47.396290418691933
2 C 68.868489493615925
2 D 72.536952490918338
3 B 77.235422702506184
3 C 32.31446212157607
3 G 39.283064194023609

SQL query is returning null values after join where there should be numeric values

I am inserting some data from an existing table into a new table. There is a specific column called DescNitro which is either returned as 0 or Null in the new table. I've checked specific cases and the Null values are replacing numeric values on the old table. Any idea why this is happening? Code below:
DECLARE #DataAtual date = '2021-11-01';
DECLARE #UltMes date = DATEADD(MONTH, -1, #DataAtual);
DECLARE #PenUltMes date = DATEADD(MONTH, -2, #DataAtual);
DECLARE #AnoUltMes integer = YEAR(#UltMes);
DECLARE #MesUltMes integer = MONTH(#UltMes);
DECLARE #AnoPenUltMes integer = YEAR(#PenUltMes);
DECLARE #MesPenUltMes integer = MONTH(#PenUltMes);
SELECT
m.datAno
,m.datMes
,m.Segmento
,m.nomDiretoria
,m.nomFilial
,m.Ov
,o.[Região] AS Filial_Pri
,m.[Cod_Rede] AS Cod_Rede
,m.Desc_Rede
,m.CodSegTatica AS cod_Canal_SAP
,m.Canal_Pricing AS cod_Canal_Pri
,m.Des_Canal_Pricing
,m.codMaterial
,m.Vol_Meta AS Vol_Meta_Ciex
,m.Fat_Meta AS Fat_Meta_Ciex
,g.[DescNitro]
INTO
[pri].[tblMetaPrecoteste]
FROM
[pri].[tblDadMetaCiex] m
LEFT JOIN
dbo.tblCadMaterial mat ON mat.codMaterial = m.codMaterial
LEFT JOIN
[pri].[tblDadOvFilial] o ON o.[Organização] = m.Ov
LEFT JOIN
[pri].[tblDadArvore] a ON a.Material = m.codMaterial
AND a.[Filial] = o.[Região]
AND a.[Canal de Vendas] = m.[Des_Canal_Pricing]
LEFT JOIN
(SELECT
t.[codMaterial],
t.[codOrganizacaoVenda],
t.[codSegTatica],
t.[codRede],
SUM(t.[DescNitro]) AS DescNitro
FROM
[MktIntelligence].[dbo].[tbl_F_PM_join_real_fifo_meta_com_gp] t
WHERE
(t.datmes = #MesUltMes AND t.datAno = #AnoUltMes)
OR (t.datmes = #MesPenUltMes AND t.datAno = #AnoPenUltMes)
GROUP BY
t.[codMaterial], t.[codOrganizacaoVenda], t.[codSegTatica], t.[codRede]) g ON m.[codMaterial] = g.[codMaterial]
AND m.[Ov] = g.[codOrganizacaoVenda]
AND m.[codSegTatica] = g.[codSegTatica]
AND m.[Cod_Rede] = g.[codRede]
Since that's a left join therefore if there is no match it will display as NULL otherwise use isnull(columnname,0) so it will display 0 when it's NULL

SQL Server : Sum function is returning wrong value

So this is my query
select a.ERSDataValues_ERSCommodity_ID,c.ersGeographyDimension_country,
b.ERSTimeDimension_Year,
sum(a.ERSDataValues_AttributeValue) as Total
from cosd.ERSDataValues a ,cosd.ERSTimeDimension_LU
b,cosd.ERSGeographyDimension_LU c
where a.ERSDataValues_ERSCommodity_ID in (SELECT
ERSBusinessLogic_InputDataSeries
FROM [AnimalProductsCoSD].[CoSD].[ERSBusinessLogic]
where ERSBusinessLogic_InputGeographyDimensionID = 7493
and ERSBusinessLogic_InputTimeDimensionValue = 'all months'
and ERSBusinessLogic_Type = 'time aggregate')
and a.ERSDataValues_ERSTimeDimension_ID = b.ERSTimeDimension_ID
and c.ersGeographyDimension_country != 'WORLD'
and a.ERSDataValues_ERSGeography_ID=c.ERSGeographyDimension_ID
group by b.ERSTimeDimension_Year,a.ERSDataValues_ERSCommodity_ID,
c.ersGeographyDimension_country
order by b.ERSTimeDimension_Year,a.ERSDataValues_ERSCommodity_ID
This is the updated query, I know it is long but I am facing the same issue the sum does not match with values when I sum up manually
It's most likely a problem with your JOINs (which you aren't technically using). Try removing the SUM and GROUP functions and see if you're getting duplicate rows (maybe limit your time frame to get fewer rows if this is a large data set):
SELECT b.TimeDimension_Year ,
c.GeographyDimension_country ,
a.DataValues_AttributeValue
FROM cosd.DataValues a ,
cosd.TimeDimension_LU b ,
cosd.GeographyDimension_LU c
WHERE a.DataValues_Commodity_ID = 2257
AND a.DataValues_TimeDimension_ID = b.TimeDimension_ID
AND c.GeographyDimension_ID = 7493
AND b.TimeDimension_Year = 2015;

SQL Query for list within select where field = value and otherfield = othervalue

I need to fudge an existing SQL Server procedure rather quickly. It's a bit of a hack job but needs must.
I need for the following to return a list of voucher codes and invoice numbers rather than just one row of data where the comment is (in the WHERE clause):
SELECT TOP 10
IH.INH_Voucher AS [ID], IH.COY_ID AS COY_ID,
IH.INH_DateSupInv AS ORD_UpdatedOn,
V.VES_ID, V.VES_IMOnumber, IH.INH_Order,
IH.INH_ID AS ORD_ID, IH.INH_INDID
FROM
InvoiceHDR IH (NOLOCK)
INNER JOIN
VESSACCOMP VA ON IH.COY_ID = VA.COY_ID
INNER JOIN
Vessel V ON VA.VES_ID = V.VES_ID
WHERE
v.VSS_ID IN ('01') AND
(IH.INH_Status >= 20 AND IH.INH_Status <= 40) AND
--IH.INH_Voucher = '170CH' AND IH.INH_SupInv = '1532' NEED LIST
IH.INH_INDID IS NOT NULL
So I would need
Voucher = '1700CH' AND SupInv = '1235' AND
Voucher = '180CH' AND SupInv = '1111' AND
And so on for many matching VoucherCodes and InvoiceCodes.
I hope this is clear?
Thanks.
You can apply the following WHERE clause to your query:::
WHERE
v.VSS_ID IN ('01') AND
(IH.INH_Status BETWEEN 20 AND 40) AND
((IH.INH_Voucher = '170CH' AND IH.INH_SupInv = '1532')
OR (IH.INH_Voucher = '180CH' AND IH.INH_SupInv = '1111'))
AND IH.INH_INDID IS NOT NULL

Using MAX function for greatest value between a range

Pretty new to tsql functions, and I'm trying to write one that returns a value for the STC_STATUS for the greatest STC_STATUS_DATE where the STC_STATUS_DATE is <= the STC_START_DATE+9. They way I have it now, it returns a null value if there is a STC_STATUS > stc_start_date+9.
SELECT #Result1 = STC_STATUS
FROM STC_STATUSES ss
LEFT OUTER JOIN STUDENT_ACAD_CRED stc ON ss.STUDENT_ACAD_CRED_ID = stc.STUDENT_ACAD_CRED_ID
WHERE ss.STUDENT_ACAD_CRED_ID = ISNULL(#student_acad_cred_id, '0')
AND MAX(ss.STC_STATUS_DATE) <= DATEADD(day,9,stc.STC_START_DATE)
Any help would be greatly appreciated.
EDIT: Sample data per recommendation:
The function returns a null value because pos 1 has a stc_status date that is greater than stc_start_date+9. What I aim to have the function do is return the most recent status date below stc_start_date+9, which would be record 2 in this sample.
you can use TOP 1 to get the correct result
SELECT TOP 1 #Result1 = STC_STATUS
FROM STC_STATUSES ss
LEFT OUTER JOIN STUDENT_ACAD_CRED stc
ON ss.STUDENT_ACAD_CRED_ID = stc.STUDENT_ACAD_CRED_ID
WHERE ss.STUDENT_ACAD_CRED_ID = ISNULL(#student_acad_cred_id, '0')
AND ss.STC_STATUS_DATE <= DATEADD(day,9,stc.STC_START_DATE)
ORDER BY ss.STC_STATUS_DATE desc

Resources