Solved

TSQL Query

Posted on 2015-01-14
6
61 Views
Last Modified: 2015-01-18
How to write a TSQL to reformt the column as attached file ? Tks
0
Comment
Question by:AXISHK
  • 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 65

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 65

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Query 1 17
MS SQL Server select from Sub Table 14 22
Do Wend Macro not working 21 24
partitioning database after decade growth 8 18
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

766 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