I am facing a very little problem but I am totally confused how to resolve it
I have two tables in my access database which have similar items in it from the first table i.e tbl_reci_inds
I have selected reci_prod_code which is a product code, also in this table this product code is used multiple times for different items e.g.
+----+----------------+-----------+----------+
| ID | reci_prod_code | reci_inde | reci_qty |
+----+----------------+-----------+----------+
| 74 | 123518233 | hello | 15 |
| 75 | 123518233 | New Item | 80 |
| 77 | 123518233 | Egg | 1 |
+----+----------------+-----------+----------+
Now I want to select each item from this table having same prod_code and remove its quantity from the second table which is basically used for stocks the stock table is tblCafe
+---------+-----------+----------+
| Item_id | Item_name | Item_qty |
+---------+-----------+----------+
| 1 | hello | 425 |
| 2 | New Item | 500 |
| 3 | Egg | 1000 |
+---------+-----------+----------+
Now the thing is when I select product number 1001 and put its quantity e.g. 2
It should select every item from table 1 and multiple the usage which makes 2xqty and then subtract the answer from table 2 which is stock
My code looks fine but the only problem I am facing is it only subtracts the first item from the table2 It may be because of query statements and close connections but please and verify what I am doing wrong and how can I resolve this
Here is my code
If Text3.Text = "Café Items" Then
If rs.State = 1 Then rs.Close
sql = "SELECT * FROM tbl_reci_inds WHERE reci_prod_code = '" & Text1.Text & "'"
rs.Open sql, cn
With rs
Dim my_prod As String
myprod = !reci_prod_code
Do Until .EOF
If Text1.Text = myprod Then
Dim final_prod As String
Dim final_qty As String
final_prod = !reci_inde
final_qty = Val(Text5.Text) * Val(!reci_qty)
sql = "SELECT * FROM tblCafe WHERE Item_name = '" & final_prod & "'"
rs.Execute sql
With rs
.Update
!Item_qty = Val(!Item_qty) - final_qty
.Update
End With
End If
.MoveNext
Loop
End With
End If
Related
I have this written and working as a raw SQL query, but I am trying to convert it to a more Laravel eloquent / query builder design instead of just a raw query.
My table structure like this:
Table One (Name model)
______________
| id | name |
|------------|
| 1 | bob |
| 2 | jane |
--------------
Table Two (Date Model)
_________________________________
| id | table_1_id | date |
|-------------------------------|
| 1 | 1 | 2000-01-01 |
| 2 | 1 | 2000-01-31 |
| 4 | 1 | 2000-02-28 |
| 5 | 1 | 2000-03-03 |
| 6 | 2 | 2000-01-03 |
| 7 | 2 | 2000-01-05 |
---------------------------------
I am returning only the the highest (most recent) dates from table 2 (Dates model) that match the user bob from table 1 (Name model).
For instance, in the example above, I return this from my query
2000-01-31
2000-02-28
2000-03-03
Here is what I am doing now (which works), but i'm just not sure how to use YEAR, MONTH and MAX with laravel.
DB::select(
DB::raw("
SELECT MAX(date) as max_date
FROM table_2
INNER JOIN table_1 ON table_1.id = table_2.table_1_id
WHERE table_1.name = 'bob'
GROUP BY YEAR(date), MONTH(date)
ORDER BY max_date DESC
")
);
Try this code if any problem then,
DB::table('table_1')->join('table_2', 'table_1.id','=','table_2.table_1_id')
->select(DB::raw('MAX(date) as max_date'),DB::raw('YEAR(date) year, MONTH(date) month'),'table_1.name')
->where('name','bob')
->groupBy('year','month')
->orderBy('max_date')
->get();
If any problem with above code then feel free to ask.
I have the below two tables, I need to find a way to get the new_plan_id from table 2, based on the values found in table 1 Plan_ids value (15,25,1700).
I am using Oracle SQL developer, is this request possible? If so how? I'm not even sure what the syntax would start to look like. Thanks in advance for any assistance.
Table_1
instance_id | var_name | var_txt_value
22222 | Plan_ids | 15,25,1709
22222 | Case_id | 1245784544
22222 | Client_id | Company
table_2
old_plan_id | old_plan_name | new_plan_id | new_plan_name
15| plan_saver | 2485 | company_plan_saver
25 | eco_plan | 1425 | company_eco_plan
1709| gand_plan | 7777 | company_grand_plan
Do you want to update table_1? Try below code,
BEGIN
FOR tab2_rec IN (SELECT *
FROM table_2)
LOOP
UPDATE table_1
SET var_txt_value = REPLACE(var_txt_value, tab2_rec.old_plan_id, tab2_rec.new_plan_id)
WHERE var_name = 'Plan_ids';
END LOOP;
END;
/
I have a set of data below.
| AdviserName | PolicyNumber | Product | Status | Duration |
|-------------|--------------|---------|--------|----------|
| Andy | LIF123 | ANZ | New | 2 |
| Andy | X224 | AXA | Lapsed | 3 |
| George | KL1122 | TAL | New | 0 |
| George | OLK43 | AXA | Lapsed | 5 |
| Ben | LIF98 | ANZ | New | 0 |
| Ben | KL343 | TAL | Lapsed | 1 |
I want to get the set of data where the status = 'Lapsed' duration between 1 to 3. However I still want to show correspond status = 'New' for each adviser. Below is the expected result that I want to see.
| AdviserName | PolicyNumber | Product | Status | Duration |
|-------------|--------------|---------|--------|----------|
| Andy | LIF123 | ANZ | New | 2 |
| Andy | X224 | AXA | Lapsed | 3 |
| Ben | LIF98 | ANZ | New | 0 |
| Ben | KL343 | TAL | Lapsed | 1 |
I created 2 parameters :
MinYear with the value set to 1
MaxYear with the value set to 3
I created 2 calculated fields :
MinInvYear which is iif([Duration]>=[Min Year] and [Status] = 'Lapsed', 1, 0)
MaxInvYear which is iif([Duration]<=[Max Year] and [Status] = 'Lapsed', 1, 0)
Last step, I put both calculated fields into the filters and tick the filter = 1 only. Unfortunately what I got is the table below. I need to show the status 'New' as well for Andy and Ben.
| AdviserName | PolicyNumber | Product | Status | Duration |
|-------------|--------------|---------|--------|----------|
| Andy | X224 | AXA | Lapsed | 3 |
| Ben | KL343 | TAL | Lapsed | 1 |
I tried to modify the calculated field using Level of Detail :
MinInvYear which is {include [Status] = 'New Business' : iif([Duration]>=[Min Year] and [Status] = 'Lapsed', 1, 0)} and I got error message 'Result of a level of detail expression must be aggregate.'
What am I missing?
FYI, I'm using Tableau 10.
Thanks all for your help.
Instead of the 2 calculated fields MinInvYear and MaxInvYear, try adding a calculated field (let's say Filter for Status and Duration) which returns a boolean value based on your condition
IF ([Status] == 'Lapsed' OR ([MinYear] < [Duration] AND [Duration] < [MaxYear])) THEN
TRUE
ELSE
FALSE
END
Now, add a filter for this calculated field to show only records which result in True.
try this, Bit lengthy but works for sure:
You are manipulating on only one aspect (Lapsed) of the field status, hence you are applying the parameter values on only that value. So here you need to independently work on that column to get the required output. Follow the below process:
Create 2 sets for each value in status column:
Set 1: Lapsed
Here create a set and just select checkbox `Lapsed`.
Set 2: New
Here create a set and just select checkbox `New`.
Create two calculated fields to extract the required data separately for Lapsed and New
Calculated Field 1: Lapsed_Status
IF [Duration] >= [Min] and [Duration] <= [Max]
THEN [Lapsed] //Set
END
Calculated Field 2: New Status
IF [New]=[New]
THEN [New] //Set
END
Create two more sets out of 2 calculated fields (created in step 2) to just select only those values that are needed for display on sheet:
Set 3: Display_Lapsed //From Calculated Field 1: New Status
Select checkbox True
Set 4: Display_New //From Calculated Field 2: Lapsed_Status
Select checkbox True
Now we have our required data in 2 sets, Create one more calculated field and use in filter
Calculated Field 'Display_sheet'
Display_Lapsed <> Display_New //Both are sets
Use the calculated field Display_sheet in filter and select true
Use the required fields on the sheet to display data.
Let me know how it goes
Below trick will get you moving
Duplicate the data source (right click on the data source in 'Data' panel -> 'Duplicate') having above data
Drag AdviserName from the original data source in 'Rows' view
Click on the duplicated data source in 'Data' panel. There you'll find 'link symbol' next to every dimension (in grey color). Click on the 'link symbol' next to AdviserName and it'll turn 'red' (if it turns grey don't worry. Click again and it'll turn red). Be sure that only one 'link symbol' is red (which is next to AdviserName).
Create a calculated field filter_cond in this duplicated data source as
IF [Status]='Lapsed' and [Duration]<=3 and [Duration]>=1
then 'Y'
ELSE 'N'
END
Drag filter_cond in 'Filters' view and select 'Y'
Now drag PolicyNumber, Product & Status from the original datasource in 'Rows' view. You are done!
Voila!
The following gets me the results I want but I am trying to figure out if I could have done this with a join or includes instead.
#items = Item.find_by_sql("SELECT *
FROM items_with_metadata
FULL OUTER JOIN items ON items.id = items_with_metadata.item_id")
The result should be that I get all attributes from both tables and the attributes are null wherever the items_with_metadata did not match an item in the items table.
ALSO, I do not have any associations between the two tables, the id of some items just happens to be in both tables
So for example if I have
items table with
id | name | active
------------------
123 | a | 0
456 | b | 1
and items_with_metadata has
color | usable | location | item_id
-----------------------------------
red | yes | north | 123
the result of the query will be
id | name | active | color | usable | location | item_id
--------------------------------------------------------
123 | a | 0 | red | yes | north | 123
456 | b | 1 | | | |
I was hoping there was a way to do this using ActiveRecord's joins or includes or any other ActiveRecord method that is not find_by_sql
How about:
Item.joins('FULL OUTER JOIN items_with_metadata ON items.id = items_with_metadata.item_id')
EDIT:
You could also use:
#items = Item.includes(:items_with_metadata)
This will return only Item models, but will also load all relevant ItemWithMetadata models to the memory which will make them available via:
#items.first.items_with_metadata
The last statement won't cause a DB query, but load the item metadata from the memory.
Similar Question I found at mySQL query - show most popular item
but I need little changes, and for VB6 ADO.
I want to create a list of popular (most sold) items, no date range.
An example of table:
+------------+---------------------+-------+
| date | item | qty |
+------------+---------------------+-------+
| 2009-08-02 | Apple | 5 |
| 2009-08-03 | Pear | 2 |
| 2009-08-04 | Peach | 4 |
| 2009-08-05 | Apple | 1 |
| 2009-08-06 | Apple | 3 |
+------------+---------------------+-------+
Result I need:
+------------+------------+-------+
| sl| item | | total |
+------------+------------+-------+
| 1 | Apple | | 9 |
| 2 | Peach | | 4 |
| 3 | Pear | | 2 |
+------------+------------+-------+
Assuming you already have an ADODB.Connection:
Dim oRS as New ADODB.Recordset
dim sSql as string
sSql = "SELECT item, SUM(qty) FROM myTable GROUP BY item ORDER BY Item"
oRS.Open sSql, oDBConn, adOpenForwardOnly, adLockReadOnly
Do While Not oRS.EOF
'// Do something with the data'
oRS.MoveNext
Loop
oRS.Close