Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TSQL Query

Posted on 2015-01-14
6
Medium Priority
?
65 Views
Last Modified: 2015-01-18
How to write a TSQL to reformt the column as attached file ? Tks
0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40548437
Hi, please post the attachment you refer to in your question. Thanks.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40548904
ALTER TABLE YourTableName
ALTER COLUMN YourColumnName NewDatatype

Beyond that, give us more details, and we'll give you a more detailed answer.
0
 

Author Comment

by:AXISHK
ID: 40550331
Attached file for your comment, Tks
TSQL.xlsx
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Expert Comment

by:Simon
ID: 40550340
No idea. There's not enough information to determine how to generate your 'after' worksheet from the 'before' worksheet.
Some further data or rules are required I think.
0
 

Author Comment

by:AXISHK
ID: 40550439
In Before worksheet, STYPE  = 4 will be summarized up. STYPE =0 and STYPE = 1 will be joined respectively. They will be grouped into separated column. Tks
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40550481
Not entirely sure based on your writeup, but this should get you most of the way there.
IF OBJECT_ID('tempdb..#test') IS NOT NULL
   DROP TABLE #test
GO

CREATE TABLE #test (
   INVENTBATCHID varchar(25), 
   ITEMID2 varchar(10), 
   INVENTDTYPEID varchar(10), 
   STYPE int,
   RESULT varchar(10)) 

insert into #test (	INVENTBATCHID, ITEMID2, INVENTDTYPEID, STYPE, RESULT) 
values 
   ('P000160279', 'N02927', '18KT', 	4	, '1.08'), 
   ('P000160279', 'N02927', '18KT', 	0	, '105DFA0.57'), 
   ('P000160279', 'N02927', '18KT', 	0	, '19DFB0.32'), 
   ('P000160279', 'N02927', '18KT', 	4	, '17.21'), 
   ('P000160279', 'N02927', '18KT', 	1	, '6XAM3.71'), 
   ('P000160279', 'N02927', '18KT', 	1	, '3XBT3.13'), 
   ('P000160279', 'N02927', '18KT', 	1	, '3XCQ2.16'), 
   ('P000160279', 'N02927', '18KT', 	1	, '8XCT5.47'), 
   ('P000160279', 'N02927', '18KT', 	1	, '3XGA1.93'), 
   ('P000160279', 'N02927', '18KT', 	1	, '2XLB1.74'), 
   ('P000160279', 'N02927', '18KT', 	1	, '3XLQ1.47'), 
   ('P000160279', 'N02927', '18KT', 	1	, '8XSQ4.88')

SELECT DISTINCT INVENTBATCHID, ITEMID2, INVENTDTYPEID,
   STUFF(
   (
      SELECT '  ' + CAST(RESULT AS VARCHAR)
      FROM #test AS t2
      WHERE t2.INVENTBATCHID = t.INVENTBATCHID AND t2.ITEMID2 = t.ITEMID2 AND  t2.INVENTDTYPEID = t.INVENTDTYPEID 
         AND STYPE = 0
      FOR XML PATH('')
   ), 1, 1, '') AS STYPE0,
   STUFF(
   (
      SELECT '  ' + CAST(RESULT AS VARCHAR)
      FROM #test AS t2
      WHERE t2.INVENTBATCHID = t.INVENTBATCHID AND t2.ITEMID2 = t.ITEMID2 AND  t2.INVENTDTYPEID = t.INVENTDTYPEID 
         AND STYPE <> 0
      FOR XML PATH('')
   ), 1, 1, '') AS STYPE1
FROM #test AS t

Open in new window


Also, just to set expectations, 'write a query to make this set of data show as that set of data' is a good start, but it helps to provide details as to how you want the data transformed.  If experts have to ask multiple clarifying questions there's a risk that they will lose interest and not answer the question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question