Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:40:19+00:00 2026-06-06T15:40:19+00:00

I want to consolidate values in my table using SELECT Query. My table has

  • 0

I want to consolidate values in my table using SELECT Query.
My table has 5 Columns: Name, Date [max three dates (consecutives buisness days)], and three values in each day.

I want an output like: strName, SUM(Value1 + Value2 – Value3) from Date0, SUM(Value1 + Value2 – Value3) from Date1, SUM(Value1 + Value2 – Value3) from Date2.

Input:

strName myDate  Value1  Value2  Value3
X4  2012-06-14  26300   0   0
X4  2012-06-15  0   4000    0
X4  2012-06-18  0   5600    0
X9  2012-06-14  764 0   0
A3  2012-06-14  7850    0   0
B   2012-06-14  5500    0   0
C3  2012-06-14  269100  0   0
D3  2012-06-14  395100  0   0
D3  2012-06-15  0   0   500

Ideal Output:

X4, 26300, 4000,5600
X9, 760, 0, 0
A3, 7850
...
D3, 395100, 0, -500

My table:

USE [MyTestBD]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbX](
[strName] [varchar](max) NOT NULL,
[Value1] [float] NOT NULL,
[myDate] [date] NOT NULL,
[Value2] [float] NOT NULL,
[Value3] [float] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 26300, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 0, CAST(0xCC350B00 AS Date), 4000, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 0, CAST(0xCF350B00 AS Date), 5600, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X9', 764, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'A3', 7850, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'B', 5500, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'C3', 269100, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D3', 395100, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D3', 0, CAST(0xCC350B00 AS Date), 0, 500)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D4', 400, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D4', 0, CAST(0xCC350B00 AS Date), 400, 6600)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'E3', 361349, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'F6', 45500, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'F6', 0, CAST(0xCC350B00 AS Date), 11600, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 55979, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 0, CAST(0xCC350B00 AS Date), 0, 19100)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 0, CAST(0xCF350B00 AS Date), 0, 38300)

PS: I didn’t found a nice Title for my question. Feel free to edit it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:40:22+00:00Added an answer on June 6, 2026 at 3:40 pm

    Try this:

    SELECT
        strName,
        SUM(CASE rn WHEN 1 THEN Value1 + Value2 - Value3 END),
        SUM(CASE rn WHEN 2 THEN Value1 + Value2 - Value3 END),
        SUM(CASE rn WHEN 3 THEN Value1 + Value2 - Value3 END)
    FROM
    (
        SELECT *, ROW_NUMBER() OVER (PARTITION BY strName ORDER BY mydate) AS rn
        FROM tbX
    ) AS T1
    

    Results:

    A3  7850        (null)  (null)
    B   5500        (null)  (null)
    C3  269100      (null)  (null)
    D3  395100      -500    (null)
    D4  400         -6200   (null)
    E3  361349      (null)  (null)
    F6  45500       11600   (null)
    G4  55979       -19100  -38300
    X4  26300       4000    5600
    X9  764         (null)  (null)
    

    sqlfiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table, category , which has 3 columns, id ,
want to know why String behaves like value type while using ==. String s1
Want the function to sort the table by HP but if duplicate HPs then
Want to develop a query refinement tool just like Google Suggest so that i
i want a macro to consolidate the data form multiple sheets to one sheet..
I want to consolidate my PHP files into a common class file, but I
I have 3 columns in my table X: Id State Type 1 NJ Form1
I have multiple lists of strings, IList<string> , that I want to consolidate in
I've mocked up a page using 960.gs that has has several elements fixed in
I want to consolidate a set of records (id) / (referencedid) 1 10 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.