SQL Server select parts, fill with 0 no resultHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableRetrieving the last record in each group - MySQLHow do I UPDATE from a SELECT in SQL Server?

Proving a function is onto where f(x)=|x|.

Indicating multiple different modes of speech (fantasy language or telepathy)

Journal losing indexing services

Greco-Roman egalitarianism

Is there a word to describe the feeling of being transfixed out of horror?

We have a love-hate relationship

Flux received by a negative charge

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

What major Native American tribes were around Santa Fe during the late 1850s?

Global amount of publications over time

Is it improper etiquette to ask your opponent what his/her rating is before the game?

Why did the HMS Bounty go back to a time when whales are already rare?

How to align and center standalone amsmath equations?

Could solar power be utilized and substitute coal in the 19th Century

Is a model fitted to data or is data fitted to a model?

What linear sensor for a keyboard?

What is the difference between "Do you interest" and "...interested in" something?

Customize circled numbers

Wrapping Cryptocurrencies for interoperability sake

Is camera lens focus an exact point or a range?

How to color a curve

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Drawing a topological "handle" with Tikz

If a character with the Alert feat rolls a crit fail on their Perception check, are they surprised?



SQL Server select parts, fill with 0 no result


How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableRetrieving the last record in each group - MySQLHow do I UPDATE from a SELECT in SQL Server?













0















I have the following tables



  • Location (Id, locationName)

  • Inventory (productid, qty, locationid)

With the following data, I need to query to show all locations per productid, even when not in inventory table. example of records below.



Table Location



Id Location Name
--------------------
1 Plant
2 Warehouse
3 Container


Table Inventory:



Productid Qty Locationid
-----------------------------
45 30 1
45 56 2
3 15 1
3 50 3
15 25 3


Desired result for my query:



Productid Qty LocationName
---------------------------------
45 30 Plant
45 56 Warehouse
45 0 Container
3 15 Plant
3 0 Warehouse
3 50 Container
15 0 Plant
15 0 Warehouse
15 25 Container


So far I have tried many different ways but no luck, so any help will be appreciated.










share|improve this question
























  • Can you provide sample script to generate data?

    – Prashant Pimpale
    Mar 8 at 6:41






  • 1





    If you can provide your query, so we could apply fixes from there.

    – jned29
    Mar 8 at 6:43















0















I have the following tables



  • Location (Id, locationName)

  • Inventory (productid, qty, locationid)

With the following data, I need to query to show all locations per productid, even when not in inventory table. example of records below.



Table Location



Id Location Name
--------------------
1 Plant
2 Warehouse
3 Container


Table Inventory:



Productid Qty Locationid
-----------------------------
45 30 1
45 56 2
3 15 1
3 50 3
15 25 3


Desired result for my query:



Productid Qty LocationName
---------------------------------
45 30 Plant
45 56 Warehouse
45 0 Container
3 15 Plant
3 0 Warehouse
3 50 Container
15 0 Plant
15 0 Warehouse
15 25 Container


So far I have tried many different ways but no luck, so any help will be appreciated.










share|improve this question
























  • Can you provide sample script to generate data?

    – Prashant Pimpale
    Mar 8 at 6:41






  • 1





    If you can provide your query, so we could apply fixes from there.

    – jned29
    Mar 8 at 6:43













0












0








0








I have the following tables



  • Location (Id, locationName)

  • Inventory (productid, qty, locationid)

With the following data, I need to query to show all locations per productid, even when not in inventory table. example of records below.



Table Location



Id Location Name
--------------------
1 Plant
2 Warehouse
3 Container


Table Inventory:



Productid Qty Locationid
-----------------------------
45 30 1
45 56 2
3 15 1
3 50 3
15 25 3


Desired result for my query:



Productid Qty LocationName
---------------------------------
45 30 Plant
45 56 Warehouse
45 0 Container
3 15 Plant
3 0 Warehouse
3 50 Container
15 0 Plant
15 0 Warehouse
15 25 Container


So far I have tried many different ways but no luck, so any help will be appreciated.










share|improve this question
















I have the following tables



  • Location (Id, locationName)

  • Inventory (productid, qty, locationid)

With the following data, I need to query to show all locations per productid, even when not in inventory table. example of records below.



Table Location



Id Location Name
--------------------
1 Plant
2 Warehouse
3 Container


Table Inventory:



Productid Qty Locationid
-----------------------------
45 30 1
45 56 2
3 15 1
3 50 3
15 25 3


Desired result for my query:



Productid Qty LocationName
---------------------------------
45 30 Plant
45 56 Warehouse
45 0 Container
3 15 Plant
3 0 Warehouse
3 50 Container
15 0 Plant
15 0 Warehouse
15 25 Container


So far I have tried many different ways but no luck, so any help will be appreciated.







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 7:19









marc_s

582k13011231269




582k13011231269










asked Mar 8 at 6:38









Juan GJuan G

407




407












  • Can you provide sample script to generate data?

    – Prashant Pimpale
    Mar 8 at 6:41






  • 1





    If you can provide your query, so we could apply fixes from there.

    – jned29
    Mar 8 at 6:43

















  • Can you provide sample script to generate data?

    – Prashant Pimpale
    Mar 8 at 6:41






  • 1





    If you can provide your query, so we could apply fixes from there.

    – jned29
    Mar 8 at 6:43
















Can you provide sample script to generate data?

– Prashant Pimpale
Mar 8 at 6:41





Can you provide sample script to generate data?

– Prashant Pimpale
Mar 8 at 6:41




1




1





If you can provide your query, so we could apply fixes from there.

– jned29
Mar 8 at 6:43





If you can provide your query, so we could apply fixes from there.

– jned29
Mar 8 at 6:43












2 Answers
2






active

oldest

votes


















7














You can use the following query:



SELECT p.ProductId, 
COALESCE(qty,0) AS qty,
[Location Name]
FROM LOCATION l
CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
ORDER BY Productid, [Location Name]


The query uses CROSS JOIN to get all possible combinations between locations and products.



Demo here






share|improve this answer






























    0














    Select y., isNull(z.Quantity,0) as Quantity
    From
    (
    Select Location.
    , x.ProductId

    From Location,(Select Distinct ProductId From Inventory) as x
    ) as y
    Left Outer Join Inventory z ON y.Id = z.LocationId
    and y.ProductId = z.ProductId






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057951%2fsql-server-select-parts-fill-with-0-no-result%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      You can use the following query:



      SELECT p.ProductId, 
      COALESCE(qty,0) AS qty,
      [Location Name]
      FROM LOCATION l
      CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
      LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
      ORDER BY Productid, [Location Name]


      The query uses CROSS JOIN to get all possible combinations between locations and products.



      Demo here






      share|improve this answer



























        7














        You can use the following query:



        SELECT p.ProductId, 
        COALESCE(qty,0) AS qty,
        [Location Name]
        FROM LOCATION l
        CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
        LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
        ORDER BY Productid, [Location Name]


        The query uses CROSS JOIN to get all possible combinations between locations and products.



        Demo here






        share|improve this answer

























          7












          7








          7







          You can use the following query:



          SELECT p.ProductId, 
          COALESCE(qty,0) AS qty,
          [Location Name]
          FROM LOCATION l
          CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
          LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
          ORDER BY Productid, [Location Name]


          The query uses CROSS JOIN to get all possible combinations between locations and products.



          Demo here






          share|improve this answer













          You can use the following query:



          SELECT p.ProductId, 
          COALESCE(qty,0) AS qty,
          [Location Name]
          FROM LOCATION l
          CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
          LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
          ORDER BY Productid, [Location Name]


          The query uses CROSS JOIN to get all possible combinations between locations and products.



          Demo here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 6:48









          Giorgos BetsosGiorgos Betsos

          62.3k73265




          62.3k73265























              0














              Select y., isNull(z.Quantity,0) as Quantity
              From
              (
              Select Location.
              , x.ProductId

              From Location,(Select Distinct ProductId From Inventory) as x
              ) as y
              Left Outer Join Inventory z ON y.Id = z.LocationId
              and y.ProductId = z.ProductId






              share|improve this answer



























                0














                Select y., isNull(z.Quantity,0) as Quantity
                From
                (
                Select Location.
                , x.ProductId

                From Location,(Select Distinct ProductId From Inventory) as x
                ) as y
                Left Outer Join Inventory z ON y.Id = z.LocationId
                and y.ProductId = z.ProductId






                share|improve this answer

























                  0












                  0








                  0







                  Select y., isNull(z.Quantity,0) as Quantity
                  From
                  (
                  Select Location.
                  , x.ProductId

                  From Location,(Select Distinct ProductId From Inventory) as x
                  ) as y
                  Left Outer Join Inventory z ON y.Id = z.LocationId
                  and y.ProductId = z.ProductId






                  share|improve this answer













                  Select y., isNull(z.Quantity,0) as Quantity
                  From
                  (
                  Select Location.
                  , x.ProductId

                  From Location,(Select Distinct ProductId From Inventory) as x
                  ) as y
                  Left Outer Join Inventory z ON y.Id = z.LocationId
                  and y.ProductId = z.ProductId







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 12:11









                  NIKHIL THAKURNIKHIL THAKUR

                  211




                  211



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057951%2fsql-server-select-parts-fill-with-0-no-result%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme