The following query uses PIVOT to show how many times each quantity of a given product was ordered for quantities 1 thru 6.
The results are too granular, I would like the columns to reflect ranges e.g. [1-2], [3-4], [5-6] by summing the results.
How do I do this?
WITH Quantities AS
(
SELECT ProductID, Quantity
FROM QuantityTest
)
SELECT * FROM Quantities
PIVOT(COUNT(Quantity) FOR Quantity IN ([1],[2],[3],[4],[5],[6]))AS Pvt;
CREATE TABLE [dbo].[QuantityTest]
(
[ProductID] [int] NOT NULL,
[Quantity] [int] NOT NULL
)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (71, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (72, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (74, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (14, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (9, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (62, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (58, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (45, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (43, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (47, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (42, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (41, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (8, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (70, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (30, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (54, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (25, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (62, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (65, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (63, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (32, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (58, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (29, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (22, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (49, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (26, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (33, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (38, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (55, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (72, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (14, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (71, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (7, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (9, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (32, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (10, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (65, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (30, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (7, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (54, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (29, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (55, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (61, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (70, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (26, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (25, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 3)
GO
You could try using conditional aggregation:
SELECT
ProductId,
[1-2] = SUM(CASE WHEN Quantity >= 1 AND Quantity <= 2 THEN 1 ELSE 0 END),
[3-4] = SUM(CASE WHEN Quantity >= 3 AND Quantity <= 4 THEN 1 ELSE 0 END),
[5-6] = SUM(CASE WHEN Quantity >= 5 AND Quantity <= 6 THEN 1 ELSE 0 END)
FROM QuantityTest
GROUP BY ProductId
SQL Fiddle
It's nice and refreshing to see when someone posts DDL and sample data. Thanks for making this an easy solution!
WITH Quantities AS
(
SELECT ProductID, Quantity
FROM QuantityTest
)
SELECT ProductID, [1]+[2] as [1-2], [3]+[4] as [3-4], [5]+[6] as [5-6] FROM Quantities
PIVOT(COUNT(Quantity) FOR Quantity IN ([1],[2],[3],[4],[5],[6]))AS Pvt;
Related
I have a question around SQL Server. How to take data backup without prefix N' in each value before.
Database ... > Tasks ... > Generate Scripts ... > Next ... >
select specific database objects...>choose table..>next..> open new querywindow..>
advanced ..>type of datatoscript..>schema only...next
The insert script I get looks like this:
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (1, N'abc', 10.0000, N'unknowN')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (2, N'unknown', 20.0000, N'che')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (3, N'nkiln', 89.0000, N'nl')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (4, N'ravin', 0.0000, N'nrN')
GO
I want an insert script without the prefix N' like this:
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (1, 'abc', 10.0000, 'unknowN')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (2, 'unknown', 20.0000, 'che')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (3, 'nkiln', 89.0000, 'nl')
GO
INSERT [dbo].[emp] ([id], [name], [sal], [loc]) VALUES (4, N'ravin', 0.0000, N'nrN')
GO
Please tell me any setting need and how to take insert scripts backup without prefix N' .
I have added the db script with data.
I need to fetch the records from table
"providers" whose values are available in "providersavl" table
by making sure that the values from both tables are not present in the third table "providerbreak" ( where SQL not in)
I need the following result using the query
DECLARE #InString VARCHAR(50)= '10,11,12,13';
DECLARE #InTemp TABLE(KEYY TINYINT);
INSERT #InTemp(KEYY)
SELECT VALUE FROM STRING_SPLIT(#InString,',')
SELECT X.PROVIDERID,X.VAL FROM
(
SELECT P.PROVIDERID,P.val,
COUNT(*)OVER (PARTITION BY P.PROVIDERID)XCOL
FROM DBO.PROVIDERAVL AS P
JOIN #InTemp T ON P.val=T.KEYY
WHERE T.KEYY NOT IN
(SELECT VAL FROM PROVIDERBREAK)
)X WHERE X.XCOL=(SELECT COUNT(*)FROM #InTemp)
ProviderId VAL
3 10
3 11
3 12
3 13
but its returning empty result.
Can anyone please help me??
GO
/****** Object: Table [dbo].[provideravl] Script Date: 6/3/2022 10:41:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[provideravl](
[id] [int] IDENTITY(1,1) NOT NULL,
[providerid] [int] NULL,
[val] [int] NULL,
CONSTRAINT [PK_provideravl] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[providerbreak] Script Date: 6/3/2022 10:41:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[providerbreak](
[id] [int] IDENTITY(1,1) NOT NULL,
[providerid] [int] NULL,
[val] [int] NULL,
CONSTRAINT [PK_providerbreak] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[providers] Script Date: 6/3/2022 10:41:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[providers](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[val] [int] NULL,
CONSTRAINT [PK_providers] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[provideravl] ON
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (1, 1, 1)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (2, 1, 2)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (3, 1, 3)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (4, 1, 4)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (5, 1, 5)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (6, 1, 6)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (7, 1, 7)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (8, 1, 8)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (9, 1, 9)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (10, 1, 10)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (11, 2, 5)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (12, 2, 6)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (13, 2, 7)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (14, 2, 8)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (15, 2, 9)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (16, 2, 10)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (17, 2, 11)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (18, 2, 12)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (19, 2, 13)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (20, 2, 14)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (21, 2, 15)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (22, 2, 16)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (23, 2, 17)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (24, 2, 18)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (25, 2, 19)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (26, 2, 20)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (27, 3, 9)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (28, 3, 10)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (29, 3, 11)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (30, 3, 12)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (31, 3, 13)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (32, 3, 14)
GO
INSERT [dbo].[provideravl] ([id], [providerid], [val]) VALUES (33, 3, 15)
GO
SET IDENTITY_INSERT [dbo].[provideravl] OFF
GO
SET IDENTITY_INSERT [dbo].[providerbreak] ON
GO
INSERT [dbo].[providerbreak] ([id], [providerid], [val]) VALUES (1, 2, 10)
GO
SET IDENTITY_INSERT [dbo].[providerbreak] OFF
GO
SET IDENTITY_INSERT [dbo].[providers] ON
GO
INSERT [dbo].[providers] ([id], [name], [val]) VALUES (1, N'Asif', 1)
GO
INSERT [dbo].[providers] ([id], [name], [val]) VALUES (2, N'Sajid', 2)
GO
INSERT [dbo].[providers] ([id], [name], [val]) VALUES (21, N'Qasim', 3)
GO
SET IDENTITY_INSERT [dbo].[providers] OFF
GO
USE [master]
GO
ALTER DATABASE [TEST] SET READ_WRITE
GO
DECLARE #InString VARCHAR(50)= '9,10,11,12,13';
DECLARE #InTemp TABLE(KEYY TINYINT);
INSERT #InTemp(KEYY)
SELECT VALUE FROM STRING_SPLIT(#InString,',')
SELECT X.NAME,X.VAL FROM
(
SELECT P.NAME,P.val,
COUNT(*)OVER (PARTITION BY P.NAME)XCOL
FROM DBO.PROVIDERS AS P
JOIN #InTemp T ON P.val=T.KEYY
)X WHERE X.XCOL=(SELECT COUNT(*)FROM #InTemp)
I hope it can be somehow simplified
CREATE TABLE [dbo].[Sale](
[ID] [int] NOT NULL,
[SaleDate] [date] NOT NULL,
[CustomerRef] [varchar](20) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Verification](
[CustomerRef] [varchar](20) NOT NULL,
[VerificationDate] [date] NOT NULL
) ON [PRIMARY]
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (1, CAST(N'2022-02-01' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (2, CAST(N'2022-02-02' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (3, CAST(N'2022-02-03' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (4, CAST(N'2022-02-13' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (5, CAST(N'2022-02-14' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (6, CAST(N'2022-02-15' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (7, CAST(N'2022-02-16' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (8, CAST(N'2022-03-08' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (9, CAST(N'2022-03-08' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (10, CAST(N'2022-03-10' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (11, CAST(N'2022-03-11' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (12, CAST(N'2022-03-12' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (13, CAST(N'2022-03-13' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (14, CAST(N'2022-02-20' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (15, CAST(N'2022-03-14' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (16, CAST(N'2022-02-10' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (17, CAST(N'2022-02-11' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (18, CAST(N'2022-02-12' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (19, CAST(N'2022-03-18' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (20, CAST(N'2022-03-19' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (21, CAST(N'2022-03-20' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (22, CAST(N'2022-02-15' AS Date), N'3')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (23, CAST(N'2022-02-16' AS Date), N'3')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (24, CAST(N'2022-02-20' AS Date), N'4')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (25, CAST(N'2022-02-21' AS Date), N'4')
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'1', CAST(N'2022-02-01' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'2', CAST(N'2022-02-10' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'3', CAST(N'2022-02-15' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'4', CAST(N'2022-02-20' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'1', CAST(N'2022-03-10' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'2', CAST(N'2022-03-20' AS Date))
GO
I have written the following query to get the number of sales in the 2-week period from the 1st sale date after the verification date and prior to the next verification date.
;WITH CTE AS(
SELECT *,LEAD(VERIFICATIONDATE,1,'20990101') OVER(PARTITION BY CUSTOMERREF ORDER BY VERIFICATIONDATE) AS NEXTVERIFICATIONDATE
FROM VERIFICATION A
)
SELECT *
FROM CTE A
--GET FIRST SALE DATE POST VERIFICATION DATE AND PRIOR TO NEXT VERIFICATION DATE
CROSS APPLY
(SELECT MIN(SaleDate) FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= A.VERIFICATIONDATE
AND B.SALEDATE < NEXTVERIFICATIONDATE) Z
--GET COUNT OF SALES IN 2 WEEKS FROM THE FIRST SALE DATE POST VERIFICATION DATE (AND PRIOR TO NEXT VERIFICATION DATE)
CROSS APPLY
(SELECT COUNT(*) COUNT
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
AND SALEDATE < DATEADD(WEEK, 2, Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate)
AND SALEDATE < NEXTVERIFICATIONDATE) U
Against each row, how can I retrieve the sale date value when the count crossed the value of 4?
Expected output is (look at the last column):
+-------------+------------------+----------------------+----------------------------------------------------------------+-------+------------+
| CustomerRef | VerificationDate | NEXTVERIFICATIONDATE | FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate | COUNT | TARGETDATE |
+-------------+------------------+----------------------+----------------------------------------------------------------+-------+------------+
| 1 | 2022-02-01 | 2022-03-10 | 2022-02-01 | 5 | 2022-02-13 |
| 1 | 2022-03-10 | 2099-01-01 | 2022-03-10 | 5 | 2022-03-13 |
| 2 | 2022-02-10 | 2022-03-20 | 2022-02-10 | 4 | 2022-02-20 |
| 2 | 2022-03-20 | 2099-01-01 | 2022-03-20 | 1 | |
| 3 | 2022-02-15 | 2099-01-01 | 2022-02-15 | 2 | |
| 4 | 2022-02-20 | 2099-01-01 | 2022-02-20 | 2 | |
+-------------+------------------+----------------------+----------------------------------------------------------------+-------+------------+
I tried adding another CROSS APPLY as shown below:
CROSS APPLY
(SELECT B.SaleDate
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
AND SALEDATE < DATEADD(WEEK, 2, Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate)
AND SALEDATE < NEXTVERIFICATIONDATE
AND ROW_NUMBER()over(partition by b.customerref order by b.saledate)=4) V
This gives an error: Windowed functions can only appear in the SELECT or ORDER BY clauses.
Based on one of the comments I have written the following which I suppose is the answer:
OUTER APPLY(
SELECT SALEDATE FROM(
SELECT SALEDATE,ROW_NUMBER()OVER(PARTITION BY B.CUSTOMERREF ORDER BY B.SALEDATE) RNO
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
AND SALEDATE < DATEADD(WEEK, 2, Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate)
AND SALEDATE < NEXTVERIFICATIONDATE
) TBL
WHERE RNO=4
) V
You can just do a running count within the second APPLY, then add a conditional aggregation over that count, all within the APPLY
;WITH CTE AS(
SELECT *,LEAD(VERIFICATIONDATE,1,'20990101') OVER(PARTITION BY CUSTOMERREF ORDER BY VERIFICATIONDATE) AS NEXTVERIFICATIONDATE
FROM VERIFICATION A
)
SELECT *
FROM CTE A
--GET FIRST SALE DATE POST VERIFICATION DATE AND PRIOR TO NEXT VERIFICATION DATE
CROSS APPLY
(SELECT MIN(SaleDate) FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= A.VERIFICATIONDATE
AND B.SALEDATE < NEXTVERIFICATIONDATE) Z
--GET COUNT OF SALES IN 2 WEEKS FROM THE FIRST SALE DATE POST VERIFICATION DATE (AND PRIOR TO NEXT VERIFICATION DATE)
CROSS APPLY (
SELECT
COUNT(*) COUNT,
MIN(CASE WHEN RunningCount = 4 THEN SALEDATE END) DateAt4
FROM (
SELECT *,
COUNT(*) OVER (ORDER BY B.SALEDATE ROWS UNBOUNDED PRECEDING) RunningCount
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
AND SALEDATE < DATEADD(WEEK, 2, Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate)
AND SALEDATE < NEXTVERIFICATIONDATE
) U
) U
db<>fiddle
CREATE TABLE [dbo].[Sale](
[ID] [int] NOT NULL,
[SaleDate] [date] NOT NULL,
[CustomerRef] [varchar](20) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Verification](
[CustomerRef] [varchar](20) NOT NULL,
[VerificationDate] [date] NOT NULL
) ON [PRIMARY]
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (1, CAST(N'2022-02-01' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (2, CAST(N'2022-02-02' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (3, CAST(N'2022-02-03' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (4, CAST(N'2022-02-13' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (5, CAST(N'2022-02-14' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (6, CAST(N'2022-02-15' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (7, CAST(N'2022-02-16' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (8, CAST(N'2022-03-08' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (9, CAST(N'2022-03-08' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (10, CAST(N'2022-03-10' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (11, CAST(N'2022-03-11' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (12, CAST(N'2022-03-12' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (13, CAST(N'2022-03-13' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (14, CAST(N'2022-02-20' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (15, CAST(N'2022-03-14' AS Date), N'1')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (16, CAST(N'2022-02-10' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (17, CAST(N'2022-02-11' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (18, CAST(N'2022-02-12' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (19, CAST(N'2022-03-18' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (20, CAST(N'2022-03-19' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (21, CAST(N'2022-03-20' AS Date), N'2')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (22, CAST(N'2022-02-15' AS Date), N'3')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (23, CAST(N'2022-02-16' AS Date), N'3')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (24, CAST(N'2022-02-20' AS Date), N'4')
GO
INSERT [dbo].[Sale] ([ID], [SaleDate], [CustomerRef]) VALUES (25, CAST(N'2022-02-21' AS Date), N'4')
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'1', CAST(N'2022-02-01' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'2', CAST(N'2022-02-10' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'3', CAST(N'2022-02-15' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'4', CAST(N'2022-02-20' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'1', CAST(N'2022-03-10' AS Date))
GO
INSERT [dbo].[Verification] ([CustomerRef], [VerificationDate]) VALUES (N'2', CAST(N'2022-03-20' AS Date))
GO
Each customer has a unique CustomerRef. The same person (CustomerRef) can be verified multiple times, so may have multiple records in the Verification table.
For each row in the Verification table, I want to get the number of Sales that have happened within 2 weeks from the first Sale after the verification date.
Here is the CROSS APPLY query:
SELECT *
FROM VERIFICATION A
--GET NEXT VERIFICATION DATE
CROSS APPLY
(SELECT MIN(VerificationDate) NextVerificationDate
FROM VERIFICATION B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.VERIFICATIONDATE > A.VERIFICATIONDATE) X
--GET FIRST SALE DATE POST VERIFICATION DATE
CROSS APPLY
(SELECT MIN(SaleDate) FirstSaleDatePostVerificationDate
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= A.VERIFICATIONDATE) Y
--GET FIRST SALE DATE POST VERIFICATION DATE AND PRIOR TO NEXT VERIFICATION DATE
CROSS APPLY
(SELECT MIN(SaleDate) FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= A.VERIFICATIONDATE
AND B.SALEDATE < ISNULL(X.NextVerificationDate,'20990101')) Z
--GET COUNT OF SALES IN 2 WEEKS FROM THE FIRST SALE DATE POST VERIFICATION DATE (AND PRIOR TO NEXT VERIFICATION DATE)
CROSS APPLY
(SELECT COUNT(*) COUNT
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate
AND SALEDATE < DATEADD(WEEK, 2, Z.FirstSaleDatePostVerificationDateAndPriorToNextVerifiationDate)) U
I believe this works by 1st computing the main table, and then applying each cross apply for each row. Is there a better way to do this?
For example - I tried clubbing both the CROSS APPLY's into 1 CROSS APPLY using the SQL LEAD, but it gives an error:
Windowed functions can only appear in the SELECT or ORDER BY clauses.
SELECT *
FROM verification A
CROSS APPLY
(SELECT COUNT(*) COUNT
FROM SALE B
WHERE A.CUSTOMERREF = B.CUSTOMERREF
AND B.SALEDATE >= A.VERIFICATIONDATE
AND B.SALEDATE < LEAD(A.VERIFICATIONDATE) OVER
(PARTITION BY CUSTOMERREF ORDER BY VERIFICATIONDATE)) X
You need to put LEAD into a derived table in order to use it in other parts of the query
You also need to take into account there may not be a "next" row, so give LEAD a default. This will replace the first APPLY
Your second APPLY seems entirely unnecessary, and can be removed. The third and fourth must remain.
SELECT v.*, s.*
FROM (
SELECT *,
NextDate = LEAD(v.VerificationDate, 1, '20990101') OVER (PARTITION BY v.CustomerRef ORDER BY v.VerificationDate)
FROM Verification v
) v
OUTER APPLY (
SELECT TOP (1) sFirst.*
FROM Sale sFirst
WHERE sFirst.CustomerRef = v.CustomerRef
AND sFirst.SaleDate < v.NextDate
ORDER BY sFirst.SaleDate
) sFirst
CROSS APPLY (
SELECT count = COUNT(*)
FROM Sale s
WHERE s.CustomerRef = v.CustomerRef
AND s.SaleDate > sFirst.SaleDate
AND s.SaleDate < DATEADD(day, 14, sFirst.SaleDate)
AND s.SaleDate < v.NextDate
) s
db<>fiddle
I have 2 tables, for this example "table1" and "table2".
table1 contains 100 entries and its structure is like this:
id (int), count(int), code(varchar(50))
table2 contains many thousands of rows and its structure is like this:
id (int), code(varchar(50))
I would need to make a select on table1, select all rows, all the data, then switch to table 2 and copy the code to X number of rows. where X is the number extracted from the table 1.
so if for example table1 contains a line "1","17","abc123" and a line "2","26","cde456" then it should update the first 17 lines of table2 with the code "abc123" and the next 26 lines with "cde456".
can this be done with a query or do I have to do it with a dedicated softare?
Edit:
It is not working properly. this is what I tested so far from the answer below:
WITH Grps AS(
SELECT LAG([qty],1,1) OVER (ORDER BY timecol ASC) AS CountStart,
[qty] AS CountEnd,
sscc,
ROW_NUMBER() OVER (ORDER BY timecol asc) AS RN
FROM QtyInfo),
CTE AS(
SELECT id,
sscc,
ROW_NUMBER() OVER (ORDER BY id asc) AS RN
FROM CrateCodes)
UPDATE C
SET sscc = G.sscc
FROM CTE C
JOIN Grps G ON C.RN BETWEEN G.CountStart AND G.CountEnd where G.RN < 10;
what it does is: on table 2 the update begins always from row 0. so it keeps overwriting the same rows continuously.
here is some sample data:
USE [Line01]
GO
/****** Object: Table [dbo].[Table1] Script Date: 24.11.2020 16:58:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table1](
[id] [int] IDENTITY(1,1) NOT NULL,
[count] [int] NULL,
[code] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Table2] Script Date: 24.11.2020 16:58:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table2](
[id] [int] IDENTITY(1,1) NOT NULL,
[code] [varchar](50) NULL CONSTRAINT [DF_Table2_code] DEFAULT ('XXX')
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Table1] ON
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (1, 2, N'ABC')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (2, 5, N'BCD')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (3, 1, N'DEF')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (4, 2, N'EFG')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (5, 5, N'HIJ')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (6, 3, N'IJK')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (7, 2, N'LMN')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (8, 3, N'OPQ')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (9, 4, N'RST')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (10, 4, N'UVW')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (11, 1, N'XYZ')
GO
INSERT [dbo].[Table1] ([id], [count], [code]) VALUES (12, 2, N'TZU')
GO
SET IDENTITY_INSERT [dbo].[Table1] OFF
GO
SET IDENTITY_INSERT [dbo].[Table2] ON
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (1, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (2, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (3, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (4, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (5, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (6, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (7, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (8, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (9, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (10, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (11, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (12, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (13, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (14, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (15, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (16, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (17, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (18, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (19, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (20, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (21, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (22, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (23, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (24, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (25, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (26, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (27, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (28, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (29, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (30, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (31, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (32, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (33, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (34, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (35, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (36, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (37, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (38, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (39, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (40, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (41, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (42, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (43, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (44, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (45, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (46, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (47, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (48, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (49, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (50, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (51, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (52, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (53, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (54, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (55, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (56, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (57, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (58, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (59, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (60, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (61, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (62, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (63, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (64, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (65, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (66, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (67, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (68, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (69, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (70, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (71, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (72, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (73, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (74, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (75, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (76, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (77, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (78, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (79, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (80, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (81, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (82, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (83, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (84, N'XXX')
GO
INSERT [dbo].[Table2] ([id], [code]) VALUES (85, N'XXX')
GO
SET IDENTITY_INSERT [dbo].[Table2] OFF
GO
This might be what you are after:
CREATE TABLE dbo.Table1 (ID int IDENTITY,
[Count] int,
Code varchar(6));
GO
CREATE TABLE dbo.Table2 (ID int IDENTITY,
Code varchar(6));
GO
INSERT INTO dbo.Table1
VALUES(5,'abc123'),(3,'def456');
GO
INSERT INTO dbo.Table2
VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL);
GO
WITH Grps AS(
SELECT ISNULL(SUM([Count]) OVER (ORDER BY ID ASC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),1) AS CountStart,
SUM([Count]) OVER (ORDER BY ID ASC) AS CountEnd,
Code
FROM Table1),
CTE AS(
SELECT ID,
Code,
ROW_NUMBER() OVER (ORDER BY ID) AS RN
FROM Table2)
UPDATE C
SET Code = G.Code
FROM CTE C
JOIN Grps G ON C.RN BETWEEN G.CountStart AND G.CountEnd;
GO
SELECT *
FROM dbo.Table2;
GO
DROP TABLE dbo.Table1;
DROP TABLE dbo.Table2;