How to copy a table but randomise the rows with respect to one of the columns? - sql

Does anyone know how can I mix up the data order while insert the records into another table in SQL?
Example:
I have 2 tables, Table A and Table B, I would like to insert the record from Table A to Table B, how ever I would like the records mix up while insert into Table B insert of follow the order in Table A. Will it be possible to do it in SQL?
Table A:
ID Postcode Total
1 3000 10
2 3000 20
3 3000 5
4 3001 10
5 3001 6
6 3002 6
7 3002 9
8 3002 10
9 3003 85
10 3004 1
After insert into Table B (the records randomly mixed up):
ID Postcode Total
1 3001 10
2 3002 20
3 3000 5
4 3003 85
5 3002 6
6 3001 6
7 3002 9
8 3000 20
9 3000 10
10 3004 1

Yes you can:
INSERT INTO TableB
SELECT *
FROM (SELECT TOP 10 * FROM TableA ORDER BY NEWID()) AS tmp
Make sure TableB does not have a clustered key or else it won't work. Change TOP 10 to the number of rows you have in TableA.
Edit: a commenter pointed that the original version won't work in SQL Server 2012 and above. In that case you need a more heavy handed version (updated).

Related

SQL return row when sum value is null

I got two tables. One with a bill of material and one with purchasing orders. Now I want to display the full bill of material of a product with the total on order amounts from the table purchasing.
**Billofmaterials**
BOM_ID BOM_Prod_id BOM_item_id
1 5 11
2 5 13
3 6 11
4 6 15
5 6 20
Example prod_id (product id) 6 has 3 items (id 11, 15 and 20).
**Purchasing**
PU_ID PU_item_id PU_amount PU_status
1 11 100 On order
2 11 650 On order
3 11 40 Received
4 20 600 On order
5 8 10 On order
6 15 150 Received
Now i got the following SQL
SELECT
BOM_item_id,
SUM(DISTINCT purchasing.PU_amount) as total_on_order
FROM Billofmaterials
LEFT JOIN purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
AND purchasing.PU_status != 'Received'
AND BOM_prod_id = 6
GROUP BY BOM_item_id
This query returns the following:
**Query result**
BOM_item_id total_on_order
11 750
20 600
Because there is only one received purchase order for BOM_item_id 15 it doesn't return a value. Now i want to retun BOM_item_id 15 also but with a total_on_order as 0 like:
**Expected result**
BOM_item_id total_on_order
11 750
15 0
20 600
What SQL feature/function do I need to use to get the expected result?
You can try the below -
SELECT BOM_item_id,coalesce(total_on_order,0) as total_on_order
FROM Billofmaterials left join
(
select PU_item_id,SUM(purchasing.PU_amount) as total_on_order
from purchasing
where purchasing.PU_status != 'Received'
group by PU_item_id
) purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
where BOM_prod_id = 6

postgresql query to delete duplicate entries in a table [duplicate]

This question already has answers here:
How to delete duplicate entries?
(16 answers)
Closed 6 years ago.
I have a table as :
id product id merchant id price upc
1 124 2 2000000 1234XDE
2 124 2 200000 1234XDE
3 124 2 200000 1234XDE
4 124 2 200000 1234XDE
5 124 2 200000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
I want to delete all the multiple entries from the table.
Delete from
table where id not in (Select min(id) from table group by(merchant id))
but no success. I want resulting table as:
id product id merchant id price upc
1 124 2 2000000 1234XDE
5 124 2 2000000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
Can someone help me in writing a query for this.
This should do it:
delete from flash
where id not in (select min(id)
from flash
group by product_id, merchant_id, upc);
SQLFiddle example: http://sqlfiddle.com/#!15/9edef/1

How to assign the Parent Group IDs to each record of a hierarchical table in Oracle 11g?

Based on the following sample hierarchical data that exists within the TECH_VALUES table, how can I create a view, say TECH_VALUES_VW that will take this same data but have an additional column, namely GROUP_ID_PARENT that will show the group id where the parent group id is 0 against the row that child belongs to, see new column data sample:
ID GROUP_ID LINK_ID PARENT_GROUP_ID TECH_TYPE GROUP_ID_PARENT
------- ------------- ------------ -------------------- ---------- ---------------
1 100 LETTER_A 0 100
2 200 LETTER_B 0 200
3 300 LETTER_C 0 300
4 400 LETTER_A1 100 A 100
5 500 LETTER_A2 100 A 100
6 600 LETTER_A3 100 A 100
7 700 LETTER_AA1 400 B 100
8 800 LETTER_AAA1 700 C 100
9 900 LETTER_B2 200 B 200
10 1000 LETTER_BB5 900 B 200
12 1200 LETTER_CC1 300 C 300
13 1300 LETTER_CC2 300 C 300
14 1400 LETTER_CC3 300 A 300
15 1500 LETTER_CCC5 1400 A 300
16 1600 LETTER_CCC6 1500 C 300
17 1700 LETTER_BBB8 900 B 200
18 1800 LETTER_B 0 1800
19 1900 LETTER_B2 1800 B 1800
20 2000 LETTER_BB5 1900 B 1800
21 2100 LETTER_BBB8 1900 B 1800
So based on the above, I want to take the table definition:
Table Name: TECH_VALUES:
ID,
GROUP_ID,
LINK_ID
PARENT_GROUP_ID,
TECH_TYPE
and create a new view
View Name: TECH_VALUES_VW:
ID,
GROUP_ID,
LINK_ID
PARENT_GROUP_ID,
TECH_TYPE,
GROUP_ID_PARENT
based on the above sample data from the TECH_VALUES table.
I am looking to create a new query to build this new view which will only use the GROUP_IDs for the PARENT_GROUP_IDs that are 0 for each row.
Updated
Just to make things a whole lot clearer of exactly what I am after is if I take out only the records where the PARENT_GROUP_ID is 0 within the TECH_VALUES table, i.e.
ID GROUP_ID LINK_ID PARENT_GROUP_ID
------- ------------- ------------ --------------------
1 100 LETTER_A 0
2 200 LETTER_B 0
3 300 LETTER_C 0
18 1800 LETTER_B 0
Using just the GROUP_ID values for these 4 records, assign this GROUP_ID to all of the children records for each of these parent link ids as a new column in the TECH_VALUES_VW as well as to the original link ids (where PARENT_GROUP_ID is 0) as shown in the sample data set above.
If I understood your question correctly, then this might be what you're after:
CREATE OR REPLACE VIEW tech_values_vw
AS
SELECT TV.*,
CASE WHEN LEVEL = 1 THEN group_id ELSE connect_by_root(group_id) END AS group_id_parent
FROM tech_values TV
START WITH parent_group_id = 0
CONNECT BY PRIOR group_id = parent_group_id
;

How to combine queries using Access sql

I have a table like below and i'm trying to select 1 series EID's i.e 1001,1002,1003,1004,1005 and 2 series EID's as seperate queries
EID PCNum
1001 8.6
1002 10
1003 9
1004 8
1005 7
2001 4
2002 1
2003 2
2004 3
2005 6
3001 8
3002 0
3003 7
3004 4
3005 4
And queries combined like below
EID PCNum EID PCNum EID PCNum
1001 8.6 2001 4 3001 8
1002 10 2002 1 3002 8
1003 9 2003 2 3003 8
1004 8 2004 3 3004 8
1005 7 2005 6 3005 8
How do i specify that in a sql query? Any suggestions? I tried UNION but giving some errors.
Normally, you would have a column which would separate out EID from series, leaving you with just values like 001, 002, etc.
In this case you could JOIN the data on itself +1000 times the series number. Something like this:
SELECT
t1.EID, t1.PCNum,
t2.EID, t2.PCNum,
t3.EID, t3.PCNum
FROM
table_name t1
INNER JOIN table_name t2 on t1.EID+1000 = t2.EID
INNER JOIN table_name t3 on t1.EID+2000 = t3.EID
WHERE t1.EID BETWEEN 1000 AND 1999
ORDER BY t1.EID

Find sequential child rows that have amounts which add up to parent row's amount

I have a table like following
ID DATE ACCT TYPE AMOUNT SEQ CHK# TRC
1 6/5/2014 1234 C 10,000 1 1001
2 6/5/2014 3333 3,000 2 123 1002
3 6/5/2014 4444 5,000 3 234 1003
4 6/5/2014 5555 2,000 4 345 1004
5 6/5/2014 2345 C 3,000 1 1007
6 6/5/2014 5555 2,500 2 255 1008
7 6/5/2014 7777 500 3 277 1009
8 6/6/2014 1234 C 5,000 1 2001
9 6/6/2014 7777 3,000 2 278 2002
10 6/6/2014 8888 2,000 3 301 2003
The rows with TYPE = C are parent rows to the child rows that follow sequentially.
The parent rows do not have CHK# and child rows do have CHK#. Each
parent row has seq# = 1 and child rows have sequential numbers. (if it
matters) From above table, row ID 1 is the parent row to the rows with
ID 2 ~ 4. The AMOUNT on the child rows add up to the parent row's
amount.
Querying for transaction for date of '6/5/2014' on account # 2345 with
the amount of 3,000 - result should be rows with ID 6 and 7.
Is such query possible using MS-SQL 2008? If so, could you let me
know?
Well, based on the data that you have, you can use the id column to find the rows that you want. First, look for the one that has the check in that amount. The look for the subsequent ids with the same group. How do you define the group? That is easy. Take the difference between id and seq. This difference is constant for the parent and child rows.
So, here is goes:
select t.*
from table t
where (t.id - t.seq) = (select t2.id - t2.seq
from table t2
where t2.type = 'C' and
t2.acct = '2345' and
t2.date = '6/5/2014'
) and
t.type is null;