How to use decode in IICS when field data contain single quote? - decode

I am new to IICS and my fields that I want to decode looks like below. As you can see the "Outlet_Name" column contains row "NH D'needs". My requirement is to use both columns i.e. Outlet_name and Department and generate an output field call "OD_ID" which consist of integers. So Decode will look like below. However, when I run below decode expression I am receiving validation error saying missing operator. I believe this has to do with the single quote present between D and needs in "NH D'needs". Not sure how to proceed from here ?
Thanks in advance for your time & efforts!
IICS Decode code
DECODE(True,
Outlet_Name = 'NH D'needs' AND Department ='Marketing' , 1,
Outlet_Name = 'NH Mart' AND Department ='Finance' ,2,
Outlet_Name = 'NH Groceries' AND Department ='Inventory' ,3,
Outlet_Name= 'NH Lifestyle' AND Department ='Marketing' , 4,
Outlet_Name = 'NH Digital' AND Department ='Logistics' , 5,
0
)

Please use CHR(39) to create singe quote. Below should work.
DECODE(True,
Outlet_Name = 'NH D'|| CHR(39)||'needs' AND Department ='Marketing' , 1,
Outlet_Name = 'NH Mart' AND Department ='Finance' ,2,
Outlet_Name = 'NH Groceries' AND Department ='Inventory' ,3,
Outlet_Name= 'NH Lifestyle' AND Department ='Marketing' , 4,
Outlet_Name = 'NH Digital' AND Department ='Logistics' , 5,
0
)

Related

Join 3 tables without losing ability to refer each table

I have 3 tables with this mock data
Item *id, name*
1, coke
2, fanta
3, juice
Branch *id, name*
1, store
2, warehouse
3, shop
BranchItem *item_id, branch_id, qty*
1, 1, 100
1, 2, 30
2, 2, 10
I want to query for an item(coke for example) and get its quantity in all branches( even the ones it doesn't exist in, those should have NULL for qty column)
So the result should look like
1, coke, store, 100
1, coke, warehouse, 30
1, coke, shop, NULL
I have a query that can do this, but because of aliasing tables, I lose the ability to refer to the column of the result table. The parsing of the result is done in an ORM object which preferably shouldn't be rewritten
The query I have
Select * from item left join (select * from branch left join ( select * from branchitem where item_id = 1) branchitem on branch.id = branchitem.branch_id) JOINEDNAME on true where item.id = 1;
My question is I don't want to Elias the join of branch and brunch item as I lose the ability to refer to them separately in the ORM. How can this query be re-written so the tables retain their names?
You don't need to use subqueries:
SELECT Item.id,
Item.name,
Branch.name,
BranchItem.qty
FROM Item
CROSS JOIN Branch
LEFT JOIN BranchItem ON Item.id = BranchItem.item_id
AND Branch.id = BranchItem.branch_id
WHERE Item.id = 1; -- or put it into the branch join

Oracle SQL conditional query

I have a table as ITEM_LOC with the following format:
ITEM LOC UNIT RETAIL
100 KS 20
101 JS 22
102 RS null
I need to find the unit retail,
if the record if present for loc: RS, it should get that.
If it doesn't exist for RS, it should find for JS.
If it doesn't exist for JS, it should find for KS
and if it doesn't exist of KS, it should return NULL.
How do I go about this case ?
You could do this with a sub-select, that translates the LOC into an number ordered by priority, and which takes the best numeric value present:
SELECT *
FROM ITEM_LOC
WHERE decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, null) =
(
SELECT min(decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, 4))
FROM ITEM_LOC
)
Note that if none of these LOC codes exist, you will get an empty result set.
If there are duplicate codes, you can have more than one result.

How to deal with multi-modality?

I have the following data set in the form of {id, sample#, zone}:
[(1, 4, 5), (2, 3, 5), (3, 2, 2), (4, 1, 2)]
The following code:
cur.execute("SELECT zone FROM zone_temp GROUP BY zone HAVING COUNT(*) = (SELECT MAX(cnt) FROM(SELECT COUNT(*) as cnt FROM zone_temp GROUP BY zone) tmp)")
or:
cur.execute("SELECT zone FROM zone_temp GROUP BY zone ORDER BY COUNT(zone)")
returns only the first value that occurs the most (5). In this data set 5 and 2 are equally frequent and as such there really isn't a "most frequent" value. What is the best way to deal with this?

ASP.net - Add multiple rows based on a textbox value

I’m trying to figure out the best way to add multiple rows into an existing SQL table using ASP.NET. I'm new to VBA programming so am a little lost and need some help with this problem.
Example, the user will enter.
2 (LotNo)
5 (itemNo) – Add multiple rows depending on this value.
100 (cartNo)
20120202 (Date)
Result:
2, 1, 100, 20120202
2, 2, 100, 20120202
2, 3, 100, 20120202
2, 4, 100, 20120202
2, 5, 100, 20120202
You can try with this code - based on Table Value Parameter
Dim sc As New SqlCommand(
"INSERT INTO MyNewTable (field1, field2,...)"&
"SELECT field1, field2,... FROM #MyTable;", connection)
sc.Parameters.AddWithValue("#MyTable", MyTable)
sc.ExecuteNonQuery()
Link :http://msdn.microsoft.com/en-us/library/bb510489.aspx

sql query with multiple checkboxlist selected

I have a complex query that includes some dynamic sql which partially depends upon a checkboxlist. Here's the part that has me stumped right now (brain fart?).
Simple example:
Table A (id, name)
Table B (id, Aid, Cid)
Table C (id, color)
So lets say Table A has:
1, Bob
2, Tim
3, Pete
and Table C has:
1, Red
2, Blue
3, Green
Now Table B has
1, 1, 1
2, 1, 2
3, 3, 2
So that Bob's favorite colors are Red and Blue and Pete's favorite colors are only Blue.
How do I query so that I only retrieve rows from Table A that have favorite colors of both Red and Blue. I don't want to see Pete in my resultset.
You want to use the INTERSECT operator to get those that match both, this is SQL 2005+ only, however.
SELECT name FROM TableA
WHERE ID IN (SELECT Aid FROM TableB WHERE CId = 1
INTERSECT
SELECT Aid FROM TableB WHERE CId = 2)
SELECT sr.receiving_id, sc.collection_id FROM stock_collection as sc, stock_requisation as srq, stock_receiving as sr WHERE (sc.stock_id = '" & strStockID & "' AND sc.datemm_issued = '" & strMM & "' AND sc.qty_issued >= 0 AND sc.collection_id = srq.requisition_id AND srq.active_status = 'Active') OR (sr.stock_id = '" & strStockID & "' AND sr.datemm_received = '" & strMM & "' AND sr.qty_received >= 0)

Resources