Solved

sql table1 rows Vs table2 rows

Posted on 2014-10-10
4
69 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
  • 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 48

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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now