Create hierarchical data from 2 tables - sql

I have two tables like this:
ProdId ProdName
1 Mike
2 Carol
3 John
4 Marc
OrdId OrdName ProdId
100 Pizza 1
101 Subs 1
102 Calzone 2
102 Sausage 2
102 Beef 2
105 Pork 4
106 Coke 4
I need a query to populate a result like this. The hierarchy should be parent and their child and again parent followed by child.
MarkerId MarkerName MarkerParentId
1 Mike NULL
100 Pizza 1
101 Subs 1
2 Carol NULL
102 Calzone 2
103 Sausage 2
104 Beef 2
3 John NULL
4 Marc NULL
105 Pork 4
106 Coke 4
Please help . Thanks.

Has nothing to do with recursion. Basically you can get what you want with UNION
SELECT ProdId as MarkerId, ProdName as MarkerName, NULL as MarkerParentId from t1
UNION ALL
SELECT OrdId as MarkerId, OrdName as MarkerName, ProdId MarkerParentId from t2
ORDER BY MarkerId, MarkerParentId

Related

SQL query to select specific fields, process sum and count from two tables

i have these two tables
ORDERS
id order_id e_id e_name
1 1000 1001 Tom
2 1009 1001 Tom
3 1010 1001 Tom
4 1011 1002 Parker
5 1012 1002 Parker
6 1013 1003 Rohan
Transactions
id order_id amount status
1 1000 100 success
2 1009 80 success
3 1010 100 failed
4 1011 50 success
6 1012 50 success
7 1013 100 failed
i would like to join two tables, select fields, process sum count and filter like this
e_id e_name amount_sum total_counts total_success_amount success_count
1001 Tom 280 3 180 2
1002 Parker 100 2 100 2
1003 Rohan 100 1 0 0
this is what i tried
use card;
SELECT COUNT(orders.order_id) as `total_counts`,
COUNT(CASE WHEN transactions.status = 'success' THEN 1 END) as `success_count`,
SUM(0 + transactions.amount) as `amount_sum`, orders.e_id,
orders.e_name
FROM orders
LEFT JOIN transactions
ON transactions.order_id=orders.order_id
GROUP BY (orders.e_id), (orders.order_id),
(orders.e_name), (transactions.amount), (transactions.status);
i tried many queries also not able to achieve it. Suggest me query to get my operation.

Exclude rows where keys match, but are on different rows

I'm looking for the best way to produce the result set in the scenario provided. My cust3 column isn't identifying the repeated values in the indvid2 column. The end result I'm looking for is to exclude the rows where key1 and key2 match (ids:1,2,6 and 7), then sum accounts where the acctids match.If there's a better way to code this, I welcome all suggestions. Thanks!
WITH T10 as (
SELECT acctid,invid,(
case
when invid like '%-R' then left (InvID,LEN(invid) -2) else InvID
END) as InvID2
FROM table x
GROUP BY acctID,invID
),
T11 as (
SELECT acctid, Invid2, COUNT(InvID2) as cust3
FROM T10
GROUP BY InvID2,acctid
HAVING
COUNT (InvID2) > 1
)
select DISTINCT
a.acctid,
a.name,
b.invid,
C.invid2,
D.cust3,
b.amt,
b.key1,
b.key2
from table a
inner join table b (nolock) on a.acctid = b.acctid
inner join T10 C (nolock) on b.invid = c.invid
inner join T11 D (nolock) on C.invid2 = D.invid2
Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 2 $350 NULL NULL
4 123 James 103 103 2 $200 NULL NULL
5 246 Tony 98-R 98 2 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 2 ($50) 6157 NULL
9 753 George 95 95 2 $365 NULL NULL
10 753 George 108 108 2 $100 NULL NULL
Desired Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 1 $350 NULL NULL
4 123 James 103 103 1 $200 NULL NULL
5 246 Tony 98-R 98 1 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 1 ($50) 6157 NULL
9 753 George 95 95 1 $365 NULL NULL
10 753 George 108 108 1 $100 NULL NULL
Then to sum amt by acctid
id acctid name amt
1 123 James $550
2 246 Tony ($750)
3 359 Stan ($50)
4 753 George $465
Something like:
;WITH Keys as (
SELECT Key1.acctID, [Key] = Key1.Key1
FROM YourTable as Key1
INNER JOIN YourTable as Key2
ON Key1.Key1 = Key2.Key2 and Key1.acctID = Key2.acctID
)
SELECT t.acctID, t.name, amt = SUM(t.amt)
FROM YourTable as t
LEFT JOIN Keys as k
ON t.acctID = k.acctID and (t.Key1 = [Key] or t.Key2 = [Key])
WHERE k.acctID is Null
GROUP BY t.acctID, t.name

Show Budget column to Position Column in sql

My Table Structure
Designation_id Budget
102 6
105 5
106 2
Need Output.
Designation_ID Position
102 1
102 2
102 3
102 4
102 5
102 6
105 1
105 2
105 3
105 4
105 5
106 1
106 2
Is this is possible.
You need a table of numbers. This can be created on the fly. Here is typical syntax:
select s.designation_id, n.n as position
from (select 1 as n union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6
) n join
structure s
on s.budget >= n.n;
The exact syntax for the subquery may vary, depending on the database.

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo

Update table values using 2 tables

I have 2 tables as follows:
COMMENT TEXT
RECORD_ID_FK SORT_ORDER COMMENT_TEXT
-----------------------------------------
101 1 TEST TEXT
101 1 SOME MORE TEST TEXT
101 1 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 3 SOME MORE...
101 3 SOME MORE...
101 3 SOME MORE...
102 1 TEST TEXT
102 1 SOME MORE TEST TEXT
102 1 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 3 SOME MORE...
102 3 SOME MORE...
102 3 SOME MORE...
PRODUCTS
PRODUCT_ID SORT_ORDER RECORD_ID_FK
-----------------------------------------
111 1 101
222 2 101
333 3 101
444 1 102
555 2 102
666 3 102
I need to be able to change the sort order of the comments from values supplied by a form.
Any SORT_ORDER entry in the comments table can be swapped around, so all SORT_ORDER value 3 can be swapped to value 2, or all SORT_ORDER value 1's can be swapped for value 2 and value 2 can be swapped with value 3 etc etc.
The function where the update sql will reside excepts the SORT_ORDER and a PRODUCT_ID value and is hit each time via a loop in an action file.
I have tried this by using an sql update statement and using EXISTS() so looking to see if the PRODUCT_ID exists and joining the RECORD_ID and SORT_ORDER but this fails as soon as the SORT_ORDER is changed the next row to change fails as the SORT_ORDER is no longer valid.
I don't think this is acheivable with what I have here, as I am limited to how I can link the 2 tables to change the SORT_ORDER en masse.
I am using Oracle 11g and Coldfusion 9.
Any ideas would be greatly appeciated or confirmation that this is not actually possible with what I have here.
Thanks
JC