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 8635703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:02:44+00:00 2026-06-12T10:02:44+00:00

I have table Table_Codes in my SQL Server database Code | Description | Owner

  • 0

I have table “Table_Codes” in my SQL Server database

Code | Description | Owner | Location
A202   SomeDescr1    Owner1       M-3
B301   SomeDescr2    Owner2       M-3
C898   SomeDescr3    Owner3       M-2
B744   SomeDescr4    Owner4       M-3
V123   SomeDescr5    Owner5       M-3         - code is not define in second table

Also I have table “Table_Stock”

Code | Qty |     DC
A202    10       M-3
B301    12       M-2      !-not the same location like in 1st table for this code
C898    90       M-2
B744   120       M-3

I can’t to build correct query to select next result from this tables:

    Code |    Qty(Location=DC) |  Qty on alternate DC (Location from 1st table <> DC)
    A202             10                       0
    B301              0                      12
    C898             90                       0
    B744            120                       0
    V123   Not in stock            Not in stock

I can do it by using auxiliary tables. But I want to know how to do it by using single SELECT query?

  • 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-12T10:02:45+00:00Added an answer on June 12, 2026 at 10:02 am

    Something like this (SQL Fiddle)?

    SELECT c.Code,
        CASE WHEN s.Code IS NULL THEN 'Not in stock' 
            ELSE CAST(SUM(CASE WHEN s.DC = c.Location THEN s.Qty ELSE 0 END) AS VARCHAR(20)) END,
        CASE WHEN s.Code IS NULL THEN 'Not in stock' 
            ELSE CAST(SUM(CASE WHEN s.DC <> c.Location THEN s.Qty ELSE 0 END) AS VARCHAR(20)) END
    FROM Table_Codes c
    LEFT OUTER JOIN Table_Stock s ON c.Code = s.Code
    GROUP BY c.Code, s.Code
    

    Note the only way to do this in one query is to cast the second and third columns to a character type. Depending on what you want to do on the other end of the query, you might want to consider returning NULL instead of ‘Not in stock’.

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

Sidebar

Related Questions

I have an SQL Server database table created by deploying the following description in
enter code here I have a table on SQL server 2005 with bigint primary
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
I have a subroutine to insert a record to sql server table. The code:
I have a table named t_Student in Microsoft SQL Server 2005 database. In that
I have created a database and table using Visual Studio's SQL Server Compact 3.5
I have a four column table in a SQL Server database. The info for
In our DB (on SQL Server 2005) we have a Customers table, whose primary
I'm trying to import an XML file into a SQL Server database. Please have
I'm experiencing something a bit strange. I have a table on SQL Server 2008,

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.