473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Case statements in MS SQL

Dear Folks,

I have an query which something looks like this

select ILTRDJ AS YEAR, DATEPART(MONTH,ILTRDJ) AS MONTH,
FMSTR.IMITM AS CODE,FMSTR.IMSRP6_UDC AS DESCRIPTION, FTRANS.ILDCT [Recieved] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS
WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'OV')
THEN 'OV'
ELSE 'OTHERS'
END,

[Issued Job] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'IJ')
THEN 'IJ',
ELSE 'OTHERS'
END,
[Inventory Adjustment] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS
WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'IA')
THEN 'IA'
ELSE 'OTHERS'
END ,ABS(ILTRQT) AS RECV_QTY,ILUNCS AS UNIT_PRICE
FROM [TP].[dbo].[F4111Selected] FTRANS INNER JOIN dbo.F4101Sel FMSTR
ON FMSTR.IMITM = FTRANS.ILITM
GROUP BY FMSTR.IMITM,FMSTR.IMSRP6_UDC,FTRANS.ILDCT,ILTRDJ,I LTRQT,ILUNCS
ORDER BY ILTRDJ,DATEPART(MONTH,ILTRDJ),FMSTR.IMITM


In the dataset we have an column named document type which has three entries like Issues, Recipts and adjustement.
and we also have another columns like QTY, and Unitprice for these three entries in the same table. My requirment is some thing like I need to segregate these three entries as seperate columns like Recipts, Issues and Adjustment in my target table with there corresponding entries (qty and unitprice)!!!

I have used an case statement in the above mentioned in query whiile at the time of query execution it is giving me something like

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'THEN'.
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'THEN'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'THEN'.

Pls do help me out to resolve this query as this is an urgent requirement!!!

Kindly feel free to revert back for clarrifications!!!

Thanks and Regards
Arun Menon D
Mar 23 '08 #1
2 1979
ck9663
2,878 Expert 2GB
Dear Folks,

I have an query which something looks like this

select ILTRDJ AS YEAR, DATEPART(MONTH,ILTRDJ) AS MONTH,
FMSTR.IMITM AS CODE,FMSTR.IMSRP6_UDC AS DESCRIPTION, FTRANS.ILDCT [Recieved] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS
WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'OV')
THEN 'OV'
ELSE 'OTHERS'
END,

[Issued Job] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'IJ')
THEN 'IJ',
ELSE 'OTHERS'
END,
[Inventory Adjustment] =
CASE
WHEN (select FTRANS.ILDCT FROM [TP].[dbo].[F4111Selected] FTRANS
WHERE FMSTR.IMITM = FTRANS.ILITM and FTRANS.ILDCT = 'IA')
THEN 'IA'
ELSE 'OTHERS'
END ,ABS(ILTRQT) AS RECV_QTY,ILUNCS AS UNIT_PRICE
FROM [TP].[dbo].[F4111Selected] FTRANS INNER JOIN dbo.F4101Sel FMSTR
ON FMSTR.IMITM = FTRANS.ILITM
GROUP BY FMSTR.IMITM,FMSTR.IMSRP6_UDC,FTRANS.ILDCT,ILTRDJ,I LTRQT,ILUNCS
ORDER BY ILTRDJ,DATEPART(MONTH,ILTRDJ),FMSTR.IMITM


In the dataset we have an column named document type which has three entries like Issues, Recipts and adjustement.
and we also have another columns like QTY, and Unitprice for these three entries in the same table. My requirment is some thing like I need to segregate these three entries as seperate columns like Recipts, Issues and Adjustment in my target table with there corresponding entries (qty and unitprice)!!!

I have used an case statement in the above mentioned in query whiile at the time of query execution it is giving me something like

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'THEN'.
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'THEN'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'THEN'.

Pls do help me out to resolve this query as this is an urgent requirement!!!

Kindly feel free to revert back for clarrifications!!!

Thanks and Regards
Arun Menon D

I don't think you still need subquery.

Try following this pattern:

Expand|Select|Wrap|Line Numbers
  1. [Recieved] = 
  2. CASE
  3. WHEN FTRANS.ILDCT = 'OV'
  4. THEN 'OV'
  5. ELSE 'OTHERS'
  6. END,
  7.  
  8. [Issued Job] =  
  9. CASE
  10. WHEN FTRANS.ILDCT = 'IJ'
  11. THEN 'IJ',
  12. ELSE 'OTHERS'
  13. END,
  14. [Inventory Adjustment] = 
  15. CASE 
  16. WHEN FTRANS.ILDCT = 'IA'
  17. THEN 'IA'
  18. ELSE 'OTHERS'
This portion "FMSTR.IMITM = FTRANS.ILITM" has been taken care of by your JOIN.

-- CK
Mar 24 '08 #2
Hi,

Thanks for your reply and it worked well!!!!

Regards
Arun Menon D


I don't think you still need subquery.

Try following this pattern:

Expand|Select|Wrap|Line Numbers
  1. [Recieved] = 
  2. CASE
  3. WHEN FTRANS.ILDCT = 'OV'
  4. THEN 'OV'
  5. ELSE 'OTHERS'
  6. END,
  7.  
  8. [Issued Job] =  
  9. CASE
  10. WHEN FTRANS.ILDCT = 'IJ'
  11. THEN 'IJ',
  12. ELSE 'OTHERS'
  13. END,
  14. [Inventory Adjustment] = 
  15. CASE 
  16. WHEN FTRANS.ILDCT = 'IA'
  17. THEN 'IA'
  18. ELSE 'OTHERS'
This portion "FMSTR.IMITM = FTRANS.ILITM" has been taken care of by your JOIN.

-- CK
Mar 24 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
1
by: richasaraf | last post by:
Hello everyone, I'm facing problem in converting CASE statements into DECODE. As i have PL/SQL 8i, so it does not handle CASE statements. Please send me the solutions . Then basic problem is the...
0
by: richasaraf | last post by:
Hello everyone, Please HELP !!!!! I'm facing problem in converting CASE statements into DECODE. As i have PL/SQL 8i, so it does not handle CASE statements. Please send me the solutions . Then...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
10
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
10
by: Brett Romero | last post by:
If I want to use: switch (AppName) { case ApplicationName.App1: loadApp1Logo(); break; case ApplicationName.App2: loadApp2Logo(); break;
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
4
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.