SQL Server: I need to create copies of records from 2 tables and ensure FK reflects these copies - sql

In SQL Server 2016, I need to create nearly exact copies of records from 2 tables. The only difference will be their primary keys, one other column that I'm resetting to zero, and a foreign key in my 2nd table (which is the PK of my 1st table). I can create copies of both tables fine, however I don't know how to assign new FK values to the 2nd table, to correctly reflect the new primary keys from the 1st table.
Here are the current records in both tables:
Table 1: Batches
+-----------------------+-----------+----------------+------------+
| BatchID (pk identity) | StartDate | ProcessingStep | BatchCount |
+-----------------------+-----------+----------------+------------+
| 1 | 5/10/2019 | 2 | 8203 |
| 2 | 5/11/2019 | 2 | 345 |
| 3 | 5/12/2019 | 2 | 5014 |
+-----------------------+-----------+----------------+------------+
Table 2: ItemList
+--------------------------+---------+--------+-----------+-------------+
| ItemListID (pk identity) | BatchID | ItemID | Processed | ProcessDate |
+--------------------------+---------+--------+-----------+-------------+
| 1000 | 1 | 201 | 1 | 5/10/2019 |
| 1001 | 1 | 689 | 1 | 5/10/2019 |
| 1002 | 2 | 548 | 1 | 5/11/2019 |
| 1003 | 2 | 693 | 1 | 5/11/2019 |
| 1004 | 3 | 123 | 1 | 5/12/2019 |
| 1005 | 3 | 999 | 1 | 5/12/2019 |
+--------------------------+---------+--------+-----------+-------------+
I now want to create copies of these records with the following exceptions:
Batches.ProcessingStep for all records will now be set to zero
ItemList's Processed & ProcessDate are reset to zero & null respectively
Update ItemList.BatchID to reflect the new PK of the copied Batches records (this is where I'm having trouble)
Currently, my script for updating my tables is as follows:
INSERT INTO Batches(StartDate, ProcessingStep, BatchCount)
SELECT StartDate, 0, BatchCount
FROM Batches
WHERE BatchID IN (1,2,3)
INSERT INTO ItemList(BatchID, ItemID, Processed, ProcessDate)
SELECT <<?? not sure ??>>, ItemID, 0, NULL
WHERE ItemListID BETWEEN 1000 AND 1005
And here would be my final results:
Table 1: Batches
+---------+-----------+----------------+------------+
| BatchID | StartDate | ProcessingStep | BatchCount |
+---------+-----------+----------------+------------+
| 1 | 5/10/2019 | 2 | 8203 |
| 2 | 5/11/2019 | 2 | 345 |
| 3 | 5/12/2019 | 2 | 5014 |
| 4 | 5/10/2019 | 0 | 8203 |
| 5 | 5/11/2019 | 0 | 345 |
| 6 | 5/12/2019 | 0 | 5014 |
+---------+-----------+----------------+------------+
Table 2: ItemList
+------------+---------+--------+-----------+-------------+
| ItemListID | BatchID | ItemID | Processed | ProcessDate |
+------------+---------+--------+-----------+-------------+
| 1000 | 1 | 201 | 1 | 5/10/2019 |
| 1001 | 1 | 689 | 1 | 5/10/2019 |
| 1002 | 2 | 548 | 1 | 5/11/2019 |
| 1003 | 2 | 693 | 1 | 5/11/2019 |
| 1004 | 3 | 123 | 1 | 5/12/2019 |
| 1005 | 3 | 999 | 1 | 5/12/2019 |
| 1006 | 4 | 201 | 0 | NULL |
| 1007 | 4 | 689 | 0 | NULL |
| 1008 | 5 | 548 | 0 | NULL |
| 1009 | 5 | 693 | 0 | NULL |
| 1010 | 6 | 123 | 0 | NULL |
| 1011 | 6 | 999 | 0 | NULL |
+------------+---------+--------+-----------+-------------+
How would I go about populating that ItemList.BatchID foreign key correctly?
Thanks.

Related

PostgresSql:Comparing two tables and obtaining its result and compare it with third table

TABLE 2 : trip_delivery_sales_lines
+-------+---------------------+------------+----------+------------+-------------+--------+--+
| Sl no | Order_date | Partner_id | Route_id | Product_id | Product qty | amount | |
+-------+---------------------+------------+----------+------------+-------------+--------+--+
| 1 | 2020-08-01 04:25:35 | 34567 | 152 | 432 | 2 | 100 | |
| 2 | 2021-09-11 02:25:35 | 34572 | 130 | 312 | 4 | 150 | |
| 3 | 2020-05-10 04:25:35 | 34567 | 152 | 432 | 3 | 123 | |
| 4 | 2021-02-16 01:10:35 | 34572 | 130 | 432 | 5 | 123 | |
| 5 | 2020-02-19 01:10:35 | 34567 | 152 | 432 | 2 | 600 | |
| 6 | 2021-03-20 01:10:35 | 34569 | 152 | 123 | 1 | 123 | |
| 7 | 2021-04-23 01:10:35 | 34570 | 152 | 432 | 4 | 200 | |
| 8 | 2021-07-08 01:10:35 | 34567 | 152 | 432 | 3 | 32 | |
| 9 | 2019-06-28 01:10:35 | 34570 | 152 | 432 | 2 | 100 | |
| 10 | 2018-11-14 01:10:35 | 34570 | 152 | 432 | 5 | 20 | |
| | | | | | | | |
+-------+---------------------+------------+----------+------------+-------------+--------+--+
From Table 2 : we had to find partners in route=152 and find the sum of product_qty of the last 2 sale [can be selected by desc order_date]
. We can find its result in table 3.
34567 – Serial number [ 1,8]
34570 – Serial number [ 7,9]
34569 – Serial number [6]
TABLE 3 : RESULT OBTAINED FROM TABLE 1,2
+------------+-------+
| Partner_id | count |
+------------+-------+
| 34567 | 5 |
| 34569 | 1 |
| 34570 | 6 |
| | |
+------------+-------+
From table 4 we want to find the above partner_ids leaf count
TABLE 4 :coupon_leaf
+------------+-------+
| Partner_id | Leaf |
+------------+-------+
| 34567 | XYZ1 |
| 34569 | XYZ2 |
| 34569 | DDHC |
| 34567 | DVDV |
| 34570 | DVFDV |
| 34576 | FVFV |
| 34567 | FVV |
| | |
+------------+-------+
From that we can find result as:
34567 – 3
34569-2
34570 -1
TABLE 5: result obtained from TABLE 4
+------------+-------+
| Partner_id | count |
+------------+-------+
| 34567 | 3 |
| 34569 | 2 |
| 34570 | 1 |
| | |
+------------+-------+
Now we want compare table 3 and 5
If partner_id count [table 3] > partner_id count [table 4]
Print partner_id
I want a single query to do all these operation
distinct partner_id can be found by: fROM TABLE 1
SELECT DISTINCT partner_id
FROM trip_delivery_sales ts
WHERE ts.route_id='152'
GROUP BY ts.partner_id
This answers the original version of the problem.
You seem to want to compare totals after aggregating tables 2 and 3. I don't know what table1 is for. It doesn't seem to do anything.
So:
select *
from (select partner_id, sum(quantity) as sum_quantity
from (select tdsl.*,
row_number() over (partition by t2.partner_id order by order_date) as seqnum
from trip_delivery_sales_lines tdsl
) tdsl
where seqnum <= 2
group by tdsl.partner_id
) tdsl left join
(select cl.partner_id, count(*) as leaf_cnt
from coupon_leaf cl
group by cl.partner_id
) cl
on cl.partner_id = tdsl.partner_id
where leaf_cnt is null or sum_quantity > leaf_cnt

How I can I add a count to rank null values in SQL Hive?

This is what I have right now:
| time | car_id | order | in_order |
|-------|--------|-------|----------|
| 12:31 | 32 | null | 0 |
| 12:33 | 32 | null | 0 |
| 12:35 | 32 | null | 0 |
| 12:37 | 32 | 123 | 1 |
| 12:38 | 32 | 123 | 1 |
| 12:39 | 32 | 123 | 1 |
| 12:41 | 32 | 123 | 1 |
| 12:43 | 32 | 123 | 1 |
| 12:45 | 32 | null | 0 |
| 12:47 | 32 | null | 0 |
| 12:49 | 32 | 321 | 1 |
| 12:51 | 32 | 321 | 1 |
I'm trying to rank orders, including those who have null values, in this case by car_id.
This is the result I'm looking for:
| time | car_id | order | in_order | row |
|-------|--------|-------|----------|-----|
| 12:31 | 32 | null | 0 | 1 |
| 12:33 | 32 | null | 0 | 1 |
| 12:35 | 32 | null | 0 | 1 |
| 12:37 | 32 | 123 | 1 | 2 |
| 12:38 | 32 | 123 | 1 | 2 |
| 12:39 | 32 | 123 | 1 | 2 |
| 12:41 | 32 | 123 | 1 | 2 |
| 12:43 | 32 | 123 | 1 | 2 |
| 12:45 | 32 | null | 0 | 3 |
| 12:47 | 32 | null | 0 | 3 |
| 12:49 | 32 | 321 | 1 | 4 |
| 12:51 | 32 | 321 | 1 | 4 |
I just don't know how to manage a count for the null values.
Thanks!
You can count the number of non-NULL values before each row and then use dense_rank():
select t.*,
dense_rank() over (partition by car_id order by grp) as row
from (select t.*,
count(order) over (partition by car_id order by time) as grp
from t
) t;

Join Distinct or First

I have a table structure for SalesItems, and Sales.
SalesItems is setup something like this
| SaleItemID | SaleID | ProductID | ProductType |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 3 | 1 | 15 | 1 |
| 4 | 2 | 5 | 2 |
| 5 | 3 | 1 | 1 |
| 6 | 3 | 8 | 5 |
And Sales is setup something like this
| Sale | Cash |
| 1 | 1.00 |
| 2 | 10.00 |
| 3 | 28.50 |
I am trying to export a basic 'Daily History' that uses joins to spit out the information like this.
| Date | StoreID | Type1Sales | Type2Sales | ... | Cash Taken |
| 5/2 | 50 | 50 | 40 | ... | 39.50 |
| 5/3 | 50 | 10 | 32.50 | ... | 48.50 |
The issue I'm having is if I do an inner join From Sales to Sales Items, I'll end up with this.
| SaleItemID | SaleID | ProductID | ProductType | Sale | Cash |
| 1 | 1 | 1 | 1 | 1 | 1.00 |
| 2 | 1 | 2 | 2 | 1 | 1.00 |
| 3 | 1 | 15 | 1 | 1 | 1.00 |
| 4 | 2 | 5 | 2 | 2 | 10.00 |
| 5 | 3 | 1 | 1 | 3 | 28.50 |
| 6 | 3 | 8 | 5 | 3 | 28.50 |
So if I do a SUM(Cash), then I'll end up returning $70.00, instead of the correct $39.50. I'm not the best with joins, so I've been researching outer joins and such, but none of those seem to work as it's still matching up. Is there a way to only match on the FIRST instance, and return NULL for the rest? For example, something like this
| SaleItemID | SaleID | ProductID | ProductType | Sale | Cash |
| 1 | 1 | 1 | 1 | 1 | 1.00 |
| 2 | 1 | 2 | 2 | 1 | NULL |
| 3 | 1 | 15 | 1 | 1 | NULL |
| 4 | 2 | 5 | 2 | 2 | 10.00 |
| 5 | 3 | 1 | 1 | 3 | 28.50 |
| 6 | 3 | 8 | 5 | 3 | NULL |
Or do you have any other suggestions for returning back the correct amount of Cash for each particular day?
Use DISTINCT(SaleID) in your SELECT to return a single row for each Sale ID.

Union of two tables in a 3rd table with additional flag column using SQL

I have 2 tables:
One is Promotion
| PromoId |Promo Decription|
----------------------
| 101 | abc|
| 102 | pqr|
| 103 | alp|
| 104 | adc|
| 201 | abc|
and the other is PromotionType
| PromoId | PromoType |
----------------------
| 101 | 1 |
| 121 | 2 |
| 188 | 3 |
| 104 | 4 |
| 191 | 4 |
| 102 | 4 |
I want a resultant table
| PromoId | Flag |Promo Decription |PromoType |
----------------------
| 101 | 1 | | 1 |
| 121 | 0 | | 2 |
| 188 | 0 | | 3 |
| 104 | 1 | adc | 4 |
| 191 | 0 | | 4 |
| 102 | 1 | pqr | 4 |
| 103 | 1 | alp | |
| 201 | 0 | abc | |
i.e I want a resultant table , which is the union of two tables .It should not contain duplicate values and the value of flag is set to true for all the values of PromoId's which are common to both tables.
I am using Sql Server as our database.
You can use a FULL OUTER JOIN to perform this:
select
coalesce(p.promoid, t.promoid) promoid,
case when p.promoid = t.promoid then 1 else 0 end flag
from promotion p
full outer join promotiontype t
on p.promoid = t.promoid
order by promoid
See SQL Fiddle with Demo
Result:
| PROMOID | FLAG |
------------------
| 101 | 1 |
| 102 | 1 |
| 103 | 0 |
| 104 | 1 |
| 121 | 0 |
| 188 | 0 |
| 191 | 0 |
| 201 | 0 |
Edit, even with your changes to the data sample the query will still produce the result:
select
coalesce(p.promoid, t.promoid) promoid,
case when p.promoid = t.promoid then 1 else 0 end flag,
isnull(p.[Promo Decription], '') [Promo Decription],
isnull(t.PromoType, null) PromoType
from promotion p
full outer join promotiontype t
on p.promoid = t.promoid
order by
case when PromoType is not null then 0 else 1 end, promotype, promoid
See SQL Fiddle with Demo
Result is:
| PROMOID | FLAG | PROMO DECRIPTION | PROMOTYPE |
-------------------------------------------------
| 101 | 1 | abc | 1 |
| 121 | 0 | | 2 |
| 188 | 0 | | 3 |
| 102 | 1 | pqr | 4 |
| 104 | 1 | adc | 4 |
| 191 | 0 | | 4 |
| 103 | 0 | alp | (null) |
| 201 | 0 | abc | (null) |
You can use the following script:
select a.PromoID,
coalesce((case when b.promoID=a.promoID then '1'
when b.promoID<>a.promoID then '0'
end),'0') flag
from hr.promotion_type a
LEFT OUTER join hr.promotion b
on(a.promoID= b.promoid)
here, the HR is the schema I used, you can use your corresponding schema

SQL Combining two tables with one same column without losing values of a different one

I've been struggling with this for a while, I have two different tables that share a column but both have different amount of rows.
One of the tables is for money requests (table 1) and the other one is for proving the expenses (table 2)
Table 1
+-----------+-----------+
|expenseid | requestid |
+-----------+-----------+
| 16333 | 7454 |
| NULL | 7455 |
| 16336 | 7456 |
| 16338 | 7457 |
| NULL | 7458 |
| 16341 | 7459 |
| 16345 | 7460 |
| NULL | 7461 |
| NULL | 7462 |
+-----------+-----------+
Table 2
+-----------+-----------+
|expenseid | amount |
+-----------+-----------+
| 16333 | 200 |
| 16334 | 150 |
| 16335 | 300 |
| 16336 | 900 |
| 16337 | 100 |
| 16338 | 120 |
| 16339 | 700 |
| 16340 | 431 |
| 16341 | 420 |
| 16342 | 150 |
| 16343 | 240 |
| 16344 | 465 |
| 16345 | 200 |
| 16346 | 120 |
| 16347 | 90 |
| 16348 | 50 |
| 16349 | 245 |
+-----------+-----------+
As you can see the tables share the same column 'expenseid', but the amount of rows is different and there are two different columns that don't correspond to each other, i would like to have a table as follows
Combined table
+-----------+-----------+-----------+
|expenseid | amount | requestid |
+-----------+-----------+-----------+
| 16333 | 200 | 7454 |
| NULL | NULL | 7455 |
| 16334 | 150 | NULL |
| 16335 | 300 | NULL |
| 16336 | 900 | 7456 |
| 16337 | 100 | NULL |
| 16338 | 120 | 7457 |
| NULL | NULL | 7458 |
| 16339 | 700 | NULL |
| 16340 | 431 | NULL |
| 16341 | 420 | 7459 |
| 16342 | 150 | NULL |
| 16343 | 240 | NULL |
| 16344 | 465 | NULL |
| 16345 | 200 | 7460 |
| NULL | NULL | 7461 |
| NULL | NULL | 7462 |
| 16346 | 120 | NULL |
| 16347 | 90 | NULL |
| 16348 | 50 | NULL |
| 16349 | 245 | NULL |
+-----------+-----------+-----------+
I've managed to merge both tables in a way it shows the null values for expenseid related to table 1 with a left outer join, but it doesn't show the null values for the column requestid, any ideas on how to do this?
You need a FULL OUTER JOIN instead of a LEFT OUTER JOIN.
SELECT
COALESCE(Table1.expenseid, Table2.expenseid) AS expenseid,
amount,
requestid
FROM Table1
FULL OUTER JOIN Table2
ON Table1.expenseid = Table2.expenseid
Results:
EXPENSEID AMOUNT REQUESTID
16333 200 7454
(null) (null) 7455
16336 900 7456
16338 120 7457
...etc...
See it working online: sqlfiddle