Solved

sql table1 rows Vs table2 rows

Posted on 2014-10-10
4
79 Views
Last Modified: 2015-01-13
Hi

need help on a sql query. my problem is i have table (table1) that has say 3 rows (3 col's) and i have this other table (table2) that is empty, but only gets filled up when i enter data into it and the data i do enter into it matches the data that is in table 1.

table 1
----------
  Job   Component   ReqQty

1. test1, comp1,     10
2. test2, comp2,     10
3. test3, comp3,     10

when i enter in the data to this table2 below, comp1 can have one insertion with a qty of 10, comp2 can have 2 insertions each with a qty of 5 making table2 have 3 rows - now is my problem, when i run my query it sees that there are 3 rows in each table and i havnt scanned in comp3, can someone help me amend my query or think of a better way to achieve this?

as i want this select statement to bring me back row 3 in table1(test3, comp3, 10), so i know that i have data to scan into my table2. so effectivley table2 can grow more rows, but based on qty in table1 over table2 i can still bring back table1 data that has not be entered into table2.

Table2
---------
1. test1, comp1,     10
2. test2, comp2,     5
3. test2, comp2,     5


                                    SELECT
               TB.Job as 'JOB'
             , TB.Component as 'SERIAL'
             , TB.ReqQty as 'QTY'
            FROM (
                  select row_number() over (partition by Job,Component ORDER BY ReqQty) rownum, *
                  from table1 WITH (NOLOCK)) TB
            LEFT JOIN (
                  select row_number() over (partition by WORKORDER,PARTNUMBER ORDER BY QTY) rownum, *
                  from table2) CKV ON ((TB.Job = CKV.[WORKORDER])
                  AND TB.Component = CKV.[PARTNUMBER])
                  AND CKV.rownum = TB.rownum
            WHERE ((CKV.[WORKORDER] IS NULL)
                  AND (CKV.[PARTNUMBER]) IS NULL
                  AND (TB.Job = @JOB))


I am using sql server 2012.

hope makes sense...

Thanks
0
Comment
Question by:razza_b
[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
  • 2
4 Comments
 
LVL 1

Author Comment

by:razza_b
ID: 40374185
and...another example

if i have this below with data entered into table2, i still need to bring back comp2 from table1  as there is still qty to be inserted into table2

Table2
 ---------
 1. test1, comp1,     10
 2. test2, comp2,     5  <--- still to enter a qty of 5
 3. test3, comp3,     10


i should also mention that i can enter in data into table2 with a qty greater than or equal to reqQty. and if lower than the reqQty then thats how multi rows happen but as long as it reaches it reqQty.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40374350
Job, Component, ReqQty   :: table1
WORKORDER, PARTNUMBER, QTY  :: table2

and you want to compare table1.ReqQty to SUM(table2.QTY)


SELECT
      Job
    , Component
    , ReqQty
    , sum_qty
FROM table1
      LEFT JOIN (
                  SELECT
                        WORKORDER
                      , PARTNUMBER
                      , SUM(QTY) AS sum_qty
                  FROM table2
                  GROUP BY
                        WORKORDER
                      , PARTNUMBER
            ) AS t2
                  ON table1.Job = t2.WORKORDER
                        AND table1.Component = t2.PARTNUMBER
WHERE t2.sum_qty <> table1.ReqQty
      OR t2.WORKORDER IS NULL
ORDER BY
      Job
    , Component

Open in new window

0
 
LVL 1

Author Comment

by:razza_b
ID: 40374371
Thanks

if i where to pass in a param @JOB into my SP how would i then filter it out?
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40374389
you would just add a where clause that require the data to be for that job/WORKORDER

between lines 12 & 13

and amend the existing where clause to include that logic

this I don't need to do for you I hope
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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