Solved

SQL command

Posted on 2011-02-14
3
325 Views
Last Modified: 2012-05-11
I have  the folloiwng table structure

partstable
partno (PK) partname
P101           part1

stocktable
partno (PK) storeID(PK) <---Combined PK stock
P101             s1                        100
p101             s2                         50

How can I retrive all rows from store stocktable and related records from partstable? I want the result as follows

PartNo  partname   s1   s2
P101       part1   100   50

pls. help

ayha
0
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 34889580
this seems like a pivot statement ... unless you know the s1 and s2 dynamically, in which case it becomes a dynamic pivot
let's see if this helps:
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34889970
I am not able to follow it. could u pls help me.

thanks

ayha
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34890037
let's see with a non-dynamic version
SELECT PartNo  
     , PartName
     , s1
     , s2
  FROM ( SELECT p.partno, p.partname, s.stockno, sum(s.saleamount) sales_amount
           FROM partstable p
           LEFT JOIN SalesTable s
             ON p.partno = s.partno 
          GROUP BY p.partno, p.partname, s.stockno
        ) p
    PIVOT ( SUM(sales_amount)
             FOR StockNo IN ([s1],[s2] )
           ) AS pvt

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question