Solved

TSQL Query

Posted on 2015-01-14
6
63 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 500 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

724 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