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

  • SEARCH
  • Home
  • 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 6802873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:16:33+00:00 2026-05-26T19:16:33+00:00

I’m not exactly sure if what I’m needing is possible. I have two tables

  • 0

I’m not exactly sure if what I’m needing is possible. I have two tables that are joined, one being a list of items, and one being when there was any movement for those items on specified date. The movements are specified by totalizers, in this example 1=Purchased, 2=Sold, 3=Adjusted. The coding I have so far gives me a separate row for each totalizer. I am needing to combine the rows into one for each item.

SELECT [Totalizer]=COALESCE(t2.[F1034],0)
      ,[UPC]=t1.[F01]
      ,[QTY]=sum(coalesce(t2.[F64],0))
      ,[Total Amount]=sum(COALESCE(t2.[F65],0))
FROM [STORESQL].[dbo].[COST_TAB] t1
LEFT OUTER JOIN [STORESQL].[dbo].[RPT_ITM_D] t2 
ON t1.F01=t2.F01 AND (F254='2011-10-1') and (F1034=1 or F1034=2 or F1034=3)
group by t1.F01,F1034
order by t1.F01

COST_TAB table consists of:

UPC
1
2
3
4

RPT_ITM_D Consists of the item movement:

UPC     Date       Totalizer     QTY     Total Amount
1       2011-10-1  1             1       9.00
1       2011-10-1  2             1       9.99
2       2011-10-1  1             2       6.00
2       2011-10-1  2             1       3.99
2       2011-10-1  3             1       3.00
3       2011-10-1  1             1       1.00

The SQL Code I have now results in:

UPC     Date       Totalizer     QTY     Total Amount
1       2011-10-1  1             1       9.00
1       2011-10-1  2             1       9.99
2       2011-10-1  1             2       6.00
2       2011-10-1  2             1       3.99
2       2011-10-1  3             1       3.00
3       2011-10-1  1             1       1.00
4       2011-10-1  0             0       0.00

I am needing it to result in:

UPC     Date       Purchased  AMT   Sold  AMT    Adjusted  AMT
1       2011-10-1  1          9.00  1     9.99   0         0.00
2       2011-10-1  2          6.00  1     3.99   1         3.00
3       2011-10-1  1          1.00  0     0.00   0         0.00
4       2011-10-1  0          0.00  0     0.00   0         0.00

I realize I will probably have to completely rework my columns, but I don’t know where to start with this, or if I can even do 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-05-26T19:16:34+00:00Added an answer on May 26, 2026 at 7:16 pm

    Assuming you are on MS SQL Server you can use PIVOT to achieve that – for explanation and some examples see http://msdn.microsoft.com/en-us/library/ms177410.aspx and http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

    Another option is to use subselects:

    SELECT
    T1.F01 AS UPC,
    T2.F254 AS TheDate,
    (SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 1 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS PURCHASED,
    (SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 1 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_P,
    (SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 2 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS SOLD,
    (SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 2 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_S,
    (SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 3 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS ADJUSTED,
    (SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 3 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_A
    FROM [STORESQL].[dbo].[COST_TAB] t1
    LEFT OUTER JOIN [STORESQL].[dbo].[RPT_ITM_D] t2 
    ON t1.F01=t2.F01 AND (F254='2011-10-1') and (F1034=1 or F1034=2 or F1034=3)
    group by T1.F01, T2.F254
    ORDER BY T1.F01, T2.F254
    

    You should compare both option regarding performance/execution plan.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't

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.