No column name was specified for column 4 of 'a' - sql

select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm)
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
When I execute this code, I get this error :
No column name was specified for column 4 of 'a'.
Does anyone know what the problem? This section is where I encounter the error :
as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode

Give alias name for all your sub query columns :
SELECT .....,
.....,
....., MIN(c.CreatedDtTm) AS CreatedDT
In your sub query SELECT clause give alias name for min(c.CreatedDtTm) column

Related

Conversion error when adding additional clauses

need your expertise on the "Conversion failed when converting date and/or time from character string". Much written about it and tried looking though it and understanding it to the extend I could but it did not solve the issue.
My code includes a number on conversations from dates and strings to the common numerical format. The conversation work separately form the main code and even together when the code is loose. However, when I start adding more conditions in the "where" function, SQL doesn't seem to like it. Do you possible know how to handle it best? Any help is much appreciated.
SELECT DISTINCT
DOTWHL as "DC",
DORELD as "Release date",
DODLDT as "Prelim ETA",
DOITNO as "Item #",
MMITDS as "Item name",
MMSTCN as "Lkr",
DOPPQT as "Plan Qty",
DOPPQT/MMCFI2 as "Plan Qty, pall",
DOPSTS as "Sts",
SUM(MHFOQT) as "FC between ETA & BBD",
MLSTQT as "Stock, LHU",
MMCFI2 as "LHU/PAL",
MMCFI3 as "LHU/Layer",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
END AS "FEFO violation necessary?",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE MLSTQT
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE DOPPQT
END
END AS "Recommended Qty, LHU",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE MLSTQT/MMCFI2
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE DOPPQT/MMCFI2
END
END AS "Recommended Qty, pall",
MLBREF as "Batch",
CASE WHEN MLSTQT > DOPPQT
THEN CASE
WHEN DOPPQT < SUM(MHFOQT)
THEN SUM(MHFOQT)-DOPPQT
ELSE NULL
END
ELSE NULL
END AS "Fill Up possibility, LHU",
Sales.Sold_Qty as "Sold Qty 10 weeks",
Sales.Forecasted_Qty as "FC Qty 10 weeks",
ISNULL(Sales.Sold_Qty/NULLIF(Sales.Forecasted_Qty,0), 0) as "Sold/FC, %",
MBBUYE as "NP",
MMRESP as "DmP",
CASE
WHEN Other_Assortments.item <> 0 THEN 'Many Markets'
ELSE 'DC 091 only'
END as "Distributed on"
FROM MVXJDTA.MDOPLP
LEFT JOIN MVXJDTA.MITMAS
ON DOCONO=MMCONO AND DOITNO=MMITNO
LEFT JOIN MVXJDTA.MITBAL
ON DOCONO=MBCONO AND DOFWHL=MBWHLO AND DOITNO=MBITNO
LEFT JOIN MVXJDTA.MITSTA
ON DOCONO=MHCONO AND DOTWHL=MHWHLO AND DOITNO=MHITNO
LEFT JOIN MVXJDTA.MITLOC
ON DOCONO=MLCONO AND DOFWHL=MLWHLO AND DOITNO=MLITNO
LEFT JOIN
(
SELECT DISTINCT
MHITNO as "Item_#",
sum(MHSOQT) as "Sold_Qty",
sum(MHFOQT) as "Forecasted_Qty"
FROM MVXJDTA.MITSTA
WHERE MHCONO=1
AND MHWHLO='091'
AND MHCYP6 between
(cast(datepart(yyyy, dateadd(ww,-10,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-10,getdate())) as varchar),2)) and
(cast(datepart(yyyy, dateadd(ww,-1,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-1,getdate())) as varchar),2))
AND right(MHCYP6, 2) <>'00'
group by MHITNO
)
as Sales
on DOITNO=Sales.Item_#
LEFT JOIN
(
select distinct
OIITNO as "item"
from MVXJDTA.OCUSMA
left join MVXJDTA.OASCUS
on OKCONO=OCCONO and OKCUNO=OCCUNO
left join MVXJDTA.OASITN
on OKCONO=OICONO and OCASCD=OIASCD
where OKCONO=1
and OITDAT='99999999'
and OKCSCD<>'KR'
and OKCSCD<>'HK'
and OKCSCD<>'TW'
and OKCSCD<>'SG'
and OKCSCD<>'MY'
and OKCSCD<>'TH'
and OKCSCD<>'PH'
group by OIITNO) as Other_Assortments
on Other_Assortments.item=DOITNO
WHERE DOCONO=1
AND DOTWHL='091'
AND DOFWHL='010'
AND MHCYP6 NOT IN (
SELECT
MHCYP6
FROM MVXJDTA.MITSTA
WHERE MHWHLO='091'
AND MHCONO=1
AND RIGHT(CONVERT(VARCHAR, MHCYP6, 102), 2) ='00'
)
AND MHCYP6 BETWEEN
CASE
WHEN DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103))<10
THEN CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)), 0,
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
ELSE CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)),
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
END
CASE
WHEN MLBREF IS NULL
THEN '999999'
ELSE CASE
WHEN DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103)))<10
THEN CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))), 0,
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
ELSE CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))),
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
END
END
AND (MLSTQT-MLALQT)<>0
GROUP BY DOTWHL, DORELD, MLBREF, DODLDT, DOITNO, DOPPQT, MLSTQT, MMITDS, MLALQT, MBPLCD, MMSTCN, MMCFI2, MMCFI3, DOPSTS, Sales.Sold_Qty, Sales.Forecasted_Qty, MBBUYE, MMRESP, Other_Assortments.item
ORDER BY DOTWHL, DORELD

If true do something else skip SQL

Hello, I have data that looks like this:
I'm trying to come with the code that will calculate the 'MERGE' column.
Basically, I should check, if CLM_x >0 then take the value from SZ_x and concat with the value in CLM_x.
I'm trying to use case when, however I don't know how to skip merging if CLM_x =0:
CASE WHEN CLM_TBL1 > 0 THEN ('Size ' + SZ_1 + '-Qty '+CLM_1) else ... end ...
Please advise, Thank you!
Yuck. This is a bunch of string arithmetic:
select stuff( ((case when clm1_1 > 0 then concat(', Size ', sz_1, '-Qty ', clm1_1) else '' end) +
(case when clm1_2 > 0 then concat(', Size ', sz_2, '-Qty ', clm1_2) else '' end) +
(case when clm1_3 > 0 then concat(', Size ', sz_3, '-Qty ', clm1_3) else '' end)
), 1, 2, ''
) as merge_column
You just need to string the case statements together.
merge =
case
when CLM_1 > 0 then 'Size ' + SZ_1 + '-Qty '+ CLM_1 + ' '
else ''
end
+
case
when CLM_2 > 0 then 'Size ' + SZ_2 + '-Qty '+ CLM_2 + ' '
else ''
end
+
case
when CLM_3 > 0 then 'Size ' + SZ_3 + '-Qty '+ CLM_3 + ' '
else ''
end

Crystal report does not display the page

#BarId AS INTEGER
AS
BEGIN
SET NOCOUNT ON
BEGIN
select a.*, b.remark from
(
SELECT
min(bardetailid) as BarDetailId,
min(barid) as BarId,
min(Member) as Member,
min(MemberNo) as MemberNo,
min(BarMark) as BarMark,
[TypeSize] ,
min(BarLength) as BarLength,
min(BarNo) as BarNo,
min(BarTotal) as BarTotal,
[ShapeCode] ,
CAST(MAX(CAST(ShapeImage AS VARBINARY(MAX))) AS IMAGE) as ShapeImage,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS Dimension,
min(DimA) AS DimA,
min(DimB) AS DimB,
min(DimC) AS DimC,
min(DimD) AS DimD,
min(DimE) AS DimE,
min(DimF) AS DimF,
min(DimG) AS DimG,
min(DimN) AS DimN,
min(Active) AS Active,
min(c.CreatedDtTm) AS CreatedDtTm,
min(createdby) as CreatedBy,
min(createdip) AS [CreatedIp],
min(UpdatedDtTm) as [UpdatedDtTm],
min(UpdatedBy) AS [UpdatedBy],
min(UpdatedIp) AS [UpdatedIp]
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize],
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
ORDER BY
substring(a.TypeSize,1,1) desc,
BarLength desc
END
END
Crystal report does not display the page when i debug the code as showed above. When i execute the code in my Sql Server, everything is executed correctly. Due to the limitation of my knowledge in programming. Can someone identify the issue that i am facing.
I have solved the issue by adding this code a.BarDetailId = b.BarDetailId and a.shapecode = b.shapecode and it worked.

How to perform group by in sql

SELECT [BarDetailId]
,[BarId]
,[Member]
,[MemberNo]
,[BarMark]
,[TypeSize]
,[BarLength]
,[BarNo]
,[BarTotal]
,[ShapeCode]
,[ShapeImage]
,(SELECT CASE WHEN DimA <> 0 THEN 'A=' + CONVERT(VARCHAR, CAST(DimA AS
Decimal(18, 0)))
ELSE '' END + CASE WHEN DimB <> 0 THEN
', B=' + CONVERT(VARCHAR, CAST(DimB AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimC <> 0 THEN
', C=' + CONVERT(VARCHAR, CAST(DimC AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimD <> 0 THEN
', D=' + CONVERT(VARCHAR, CAST(DimD AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimE <> 0 THEN
', E=' + CONVERT(VARCHAR, CAST(DimE AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimF <> 0 THEN
', F=' + CONVERT(VARCHAR, CAST(DimF AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimG <> 0 THEN
', G=' + CONVERT(VARCHAR, CAST(DimG AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimN <> 0 THEN
', N=' + CONVERT(VARCHAR, CAST(DimN AS Decimal(18, 0)))
ELSE '' END) AS Dimension
,[DimA]
,[DimB]
,[DimC]
,[DimD]
,[DimE]
,[DimF]
,[DimG]
,[DimN]
,[Remark]
,[Active]
,[CreatedDtTm]
,[CreatedBy]
,[CreatedIp]
,[UpdatedDtTm]
,[UpdatedBy]
,[UpdatedIp]
FROM BAR_SCHEDULE_DETAIL WHERE Active = 1 AND BarId = #BarId
How to group them based on TypeSize, Shape Code. I want to include the group by statement but receive this error message.
Column 'BAR_SCHEDULE_DETAIL.BarDetailId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
GROUP BY
Is used during an aggregate function like
sum(), count(), min(), max(), avg()
If you want to group the records together, each column is required to be either part of the
GROUP BY
clause, or be aggregated in some way using one of the functions above. More info can be found here:
http://www.sqlcourse2.com/agg_functions.html
I suspect you might mean you want the results ordered so that the groups of records are together, and you might be looking for the
ORDER BY
clause.
If you would only like all the combinations of the columns you commented on before, the query would look like this:
SELECT DISTINCT ,
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension
FROM bar_schedule_detail
WHERE active = 1
AND barid = #BarId
In order to include the "top comment", if that means the first comment posted - then something like this would do it:
select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm) as CreatedDtTm
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
From here it gets increasingly more difficult without a copy of the DDL and some rows to test this answer with, but if you want to order by some arbitrary pieces of attributes, you might try this:
SELECT DISTINCT ,
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension
FROM bar_schedule_detail
WHERE active = 1
AND barid = #BarId
In order to include the "top comment", if that means the first comment posted - then something like this would do it:
select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm) as CreatedDtTm
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
ORDER BY
substring(a.TypeSize,1,1) desc,
substring(a.TypeSize,2,len(a.TypeSize)-1)

Why is the field empty when I use a multiple case when?

I have this SQL Code:
SELECT ( P1.Name
+ (CASE P1.Vorname WHEN '' THEN '' ELSE ', ' + P1.Vorname END)
+ (CASE (AP1.Postleitzahl + AP1.Ort)
WHEN '' THEN ''
ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort)
END)
+ (CASE (PKT1.Kennung + PKM1.Kennung)
WHEN ''
THEN
''
ELSE
' - '
+ (CASE PKT1.Kennung
WHEN '' THEN ''
ELSE 'Tel: ' + PKT1.Kennung
END)
+ (CASE PKM1.Kennung
WHEN '' THEN ''
ELSE ' Mobil: ' + PKM1.Kennung
END)
END))
AS [IO1] FROM XYZ
Now when one field is not filled, the cell is empty.
When everything is filled with data the cell gets filled.
So what is the Problem with that code?
I have to work with MSSQL Server 2005 so CONCAT or whatever will not work.
This is your query:
SELECT (P1.Name +
(CASE P1.Vorname WHEN '' THEN '' ELSE ', ' + P1.Vorname END) +
(CASE (AP1.Postleitzahl + AP1.Ort) WHEN '' THEN '' ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort) END) +
(CASE (PKT1.Kennung + PKM1.Kennung) WHEN '' THEN '' ELSE ' - ' + (CASE PKT1.Kennung WHEN '' THEN '' ELSE 'Tel: ' + PKT1.Kennung END) +
(CASE PKM1.Kennung WHEN '' THEN '' ELSE ' Mobil: ' + PKM1.Kennung END) END)
) AS [IO1]
FROM XYZ;
It is going to produce NULL if any of the arguments are NULL. Presumably one or more of the columns is NULL. You can fix this by wrapping things in coalesce() and/or changing the case statements. For instance:
SELECT (coalesce(P1.Name, '') +
(CASE WHEN P1.Vorname = '' or P1.Vorname is null THEN '' ELSE ', ' + P1.Vorname END) +
(CASE WHEN (AP1.Postleitzahl + AP1.Ort) = '' or (AP1.Postleitzahl + AP1.Ort) is null THEN '' ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort) END) +
(CASE WHEN (PKT1.Kennung + PKM1.Kennung) = '' or (PKT1.Kennung + PKM1.Kennung) is null THEN '' ELSE ' - ' + (CASE WHEN PKT1.Kennung = '' or PKT1.Kennung is null THEN '' ELSE 'Tel: ' + PKT1.Kennung END) +
(CASE WHEN PKM1.Kennung = '' or PKM1.Kennung is null THEN '' ELSE ' Mobil: ' + PKM1.Kennung END) END)
) AS [IO1]
FROM XYZ;