Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
magmo
Aged Yak Warrior
558 Posts |
Posted - 2015-01-28 : 02:46:20
|
HiI have a table with a "qty" column, I would like to find out which x number of rows to display based on the total sum of "Qty", lets say I need to find out what x numbers of rows total sum of qty give me 50, anyone knows how to make such a Query? |
|
viggneshwar
Yak Posting Veteran
86 Posts |
Posted - 2015-01-28 : 03:05:00
|
For the requirement you have to write a logic using Cursor or While loop. Provide simulation environment script so that i can try to write logic.RegardsViggneshwar A |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2015-01-28 : 03:24:09
|
[code]USE [TestDB]GO/****** Object: Table [dbo].[FileTest] Script Date: 2015-01-28 09:20:02 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[FileTest]( [ID] [int] IDENTITY(1,1) NOT NULL, [FileName] [nvarchar](50) NULL, [Qty] [int] NULL, [DateAdded] [datetime] NULL, CONSTRAINT [PK_FileTest] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOUSE [TestDB]GOINSERT INTO [dbo].[FileTest] ([FileName] ,[Qty],[DateAdded]) VALUES ('123.csv' ,1 ,'2015-01-20 00:00:00.000')INSERT INTO [dbo].[FileTest] ([FileName] ,[Qty],[DateAdded]) VALUES ('456.csv' ,22 ,'2015-01-17 00:00:00.000')INSERT INTO [dbo].[FileTest] ([FileName] ,[Qty],[DateAdded]) VALUES ('789.csv' ,34 ,'2015-01-19 00:00:00.000')INSERT INTO [dbo].[FileTest] ([FileName] ,[Qty],[DateAdded]) VALUES ('777.csv' ,30 ,'2015-01-16 00:00:00.000')INSERT INTO [dbo].[FileTest] ([FileName] ,[Qty],[DateAdded]) VALUES ('888.csv' ,20 ,'2015-01-14 00:00:00.000')GO[/code] |
|
|
Ifor
Aged Yak Warrior
700 Posts |
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2015-01-29 : 03:19:53
|
I suspected that the same thing (middle tier), thanks. |
|
|
|
|
|
|
|