how to divide data in multiple columns in tsql

僤鯓⒐⒋嵵緔 提交于 2020-01-06 01:18:05

问题


i have around 1000 of rows in database which i want to divide in column group wise. i mean in following format

SlNo.    Name    Price      SlNo.    Name    Price      SlNo.    Name    Price

how i can write query to show data in above format as in rdlc report vs 2008 i am unable to show data in this format. Any help would be appreciated...

Sales Table Structure

CREATE TABLE [dbo].[Sales](
[SalesId] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[CustomerID] [int] NULL,
[SalesmanID] [int] NULL,
[DisRate] [int] NULL,
[CoolingCh] [int] NULL,
[GrandTotal] [int] NULL,
[SubTotal] [int] NULL,
[BillDate] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[AddedOn] [datetime] NULL,
 CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED 
(
[SalesId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

thanks.........


回答1:


This should be a simple

SELECT 
    SalesId, [Name], GrandTotal as 'Price'
FROM 
    dbo.Sales 
WHERE
    (some condition)

to retrieve all the data you need, and then in the SQL Server Report Designer make your report display the three columns repeated three times across...

Use SQL Server and T-SQL to retrieve the data, and use SQL Server Reporting Services to format the output to your needs.




回答2:


I think something could be done along the lines of...

SELECT
    SELECT *
        FROM (SELECT SalesId, Name, Price
                  FROM Sales S2
                  WHERE S2.SalesId = S1.SalesId    ) as Tab1,
    SELECT *
        FROM (SELECT SalesId, Name, Price
                  FROM Sales S2
                  WHERE S2.SalesId = S1.SalesId + 1) as Tab2,
    SELECT *
        FROM (SELECT SalesId, Name, Price
                  FROM Sales S2
                  WHERE S2.SalesId = S1.SalesId + 2) as Tab3
    FROM Sales
    WHERE ...every third row...;

I can't fill in the full details because I'm no expert on TSQL, and also the table doesn't contain a SlNo, Name or Price which you presumably have to get from somewhere. Whether that's an improvement on going to code as Ronald Wildenberg suggests is a matter of taste.



来源:https://stackoverflow.com/questions/3173930/how-to-divide-data-in-multiple-columns-in-tsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!