SQL Where for multiple condition - sql

I am trying to filter a data in SQL 2008 using where to create purchase order report. I want to say if Item Price < $2 and we sold 1 sale at least within a month then show me all items their quantity on had are less than 12 AND if Item Price < $10 and we sold 1 sale at least within three months then show me all items their quantity on had are less than 4 AND if Item Price < $50 and we sold 1 sale at least within six months then show me all items their quantity on had are less than 2 AND if Item Price < $100 and we sold 1 sale at least within nine months then show me all items their quantity on had are less than 1
Is any way I can do that. Please advise with syntax. The price filed is PREC_1, Quantity filed is QTY_ON_HND, Sale Date is SALD_DAT and sold quantity is QTY_SOLD
I will appreciate your help
Here is the query I made
`SELECT
'-- Select from IM_IN'
enter code hereM.ITEM_NO,
enter code hereM.DESCR,
enter code hereN.QTY_ON_HND,
enter code hereCASE
enter code hereWHEN N.QTY_ON_HND<0
enter code hereTHEN 0
enter code hereELSE N.QTY_ON_HND
enter code hereEND AS QTY_HND,
enter code hereN.LST_COST,
enter code hereN.LST_RECV_DAT,
enter code here--Select from IM_ITEM
enter code hereM.CATEG_COD,
enter code hereM.ATTR_COD_1,
enter code hereM.ITEM_VEND_NO,
enter code hereM.ALT_1_UNIT,
enter code hereM.ALT_1_NUMER,
enter code hereM.PRC_1,
enter code hereM.LST_COST,
enter code here--Select from PS_TKT_HIST_LIN
enter code heresum (S.QTY_SOLD) AS QTY_SOLD,
enter code herecount (S.BUS_DAT) AS SALS_DATS
enter code hereFROM
enter code heredbo.IM_INV N
enter code hereINNER JOIN dbo.IM_ITEM M
enter code hereON
enter code hereN.ITEM_NO = M.ITEM_NO
enter code hereINNER JOIN
enter code heredbo.PS_TKT_HIST_LIN S
enter code hereON
enter code hereN.ITEM_NO = S.ITEM_NO
enter code hereWHERE S.BUS_DAT>getdate()-270 AND N.QTY_ON_HND <4 AND M.PRC_1 <2
enter code hereGroup by
enter code hereM.ITEM_NO,
enter code hereM.DESCR,
enter code hereM.ITEM_VEND_NO,
enter code hereM.CATEG_COD,
enter code hereM.ATTR_COD_1,
enter code hereN.QTY_ON_HND,
enter code hereN.LST_COST,
enter code hereN.LST_RECV_DAT,
enter code hereN.LST_SAL_DAT,
enter code hereM.ALT_1_UNIT,
enter code hereM.ALT_1_NUMER,
enter code hereM.PRC_1,
enter code hereM.LST_COST,
enter code hereOrder by M.ITEM_NO`

You are going to need to combine 2 types of filtering.
"Item Price < $2" -> This needs a WHERE Clause
EG
WHERE [item price] < 2
" we sold 1 sale at least within a month"
This is an Aggregate type filter so you need to use HAVING
HAVING COUNT(*) > 1
Plus a Grouping clause
GROUP BY DATEPART(mm, bus_dt)
As you have a lot of criteria - I'd suggest you break it down and start at the top. Then work your way down once you have the first set of filters working ...
It might be beneficial to try and tabulate all the different criteria into a simple EXCEL table.
EG
Add a row for every OR and a column for every AND condition.
Bon Chance!

Related

How can i restrict the textbox value to stay same in the access database

My goal is to create an access database form with ordering multiple item at a time. But for this phase my end goal is to find the total cost by multiplying price per unit*quantity. Through my working out i got stucked by the problem of on change event. Basically, whenever i change a the id in the combo box it changes in all the textbox that i programmed.
Here is the scenario:
In this case the price per unit stays 1000 for all of the stock ID, whereas, when i have stock id of 2 i want the price to be 0 and so on.
This is how i programmed the get price per unit value
Private Sub StockIDCombo_Change()
Me.PricePerUnit.Value = Me.StockIDCombo.Column(3)
End Sub
Note that the form is continuous.
Have an additional field in the table to hold the calculation, and bind your textbox to this field.
Is your PricePerUnit control unbound? It should be bounded to some table field as other fields (StockID, Quantity).

Sum Calculation for Tax

Can you please help me on Crystal Report, where I'm trying to create a formula.
Case:
I have 2 tables, Table1 has some columns (like ItemName, ItemPrice, TaxType, TaxRate).
Table2 has Items sold with columns (like ItemName, Qty, ItemPrice, ItemTotal).
I have also linked the column in crystal report for ItemName, so that they can fetch related data.
I am looking for a formula in crystal report that can sum up tax rate * item total where tax type= VAT and TaxType= GST
My formula structure will be like:
VATSum ({table1.taxrate}*{table2.totalamount}) where table1.taxtype= 'VAT'
GSTSum ({table1.taxrate}*{table2.totalamount}) where table1.taxtype= 'GST'
Please guide.
I would really appreciate any body's efforts to solve this problem.
In Group Expert you will want to begin by creating a Group on the {table1.taxtype} field. This will ensure all of your VAT and GST items sold are grouped together. If you have other taxtype values than VAT and GST you will need to decide how to handle those taxtypes. You use Select Expert to filter them out if they are not needed for the report, or do nothing and they will all appear within additional groups on the report by their taxtype values.
You will need to then create a formula field that can be used to calculate the tax for each item sold. The name of this formula will be referenced by other formula fields in the report, so I will call this formula field itemTax in this example. The formula for this field will be:
{table1.taxrate}*{table2.totalamount};
Place the itemTax formula field into the details section of the report.
From here you have 3 options on how to calculate the sum of itemTax. You may insert a Summary Field, a Running Total Field, or another Formula Field. Any of them will work for you, but I will continue with the Formula Field option.
Create another formula field and name it totalTaxByGroup. The formula for this field will be:
Sum({#itemTax}, {table1.taxtype});
Then place this formula field in the Group Footer section of the taxtype group.

Excel VBA - Find first instance of value after another value

The example is created to help illustrate my problem.
I have a table of values the following table in Excel:
A B C D
Total Inventory £55,000 Customer 3
Work In Progress £20,000
Completed Goods £35,000
Total Inventory £60,000 Customer 4
Work In Progress £30,000
Completed Goods £30,000
Total Inventory £20,000 Customer 2
Work In Progress £5,000
Completed Goods £15,000
Total Inventory £2000 Customer 1
Work In Progress £1500
Completed Goods £500
When the table is generated the customers in column D (and the corresponding information in the other columns) appear in a random order. I'd like to extract the "Total Inventory" figure for Customer 4. As such I am wooking for a way to search for "Customer 4" in Column D and then extract the first "Total Inventory" figure which occurs in Coloumn A after that.
I know how to do a VLookup using VBA and extract the figure from Column B but I don't known how to make sure that I extract the correct "Total Inventory" figure when the order of the customers changes each time the table is generated.
Any suggestions would be appreciated!
Thanks
Sub Macro1()
Dim cust As String
cust = "Customer 3" ' it can be any other varible I just used this to check the data
formu = "index(b:b,match(""" & cust & "Total Inventory"",c:c&a:a,0),1)" ' this one will run to lookups and make sure that correct line with 2 lookup values
Result = Evaluate(formu)
MsgBox (Result) ' again I entered this just to see that this is working
End Sub
Text inline. Tested and validated.

get previous total from filtered amount in crystal report

I have created the following report in Crystal Report using VB.Net 2012. The OPENING BALANCE & NET TOTAL rows are not in my report but my question is there.
TR_ID | TR_DATE |DETAILS | EXPENSE | INCOME | BALANCE
OPENING BALANCE 0.00
1005 08/24/2015 CASH RTN 0.00 10,000.00 10,000.00
1013 08/25/2015 PURCHASE 3,500.00 0.00 6,500.00
1014 08/25/2015 DEPOSIT 5,000.00 0.00 1,500.00
1013 08/27/2015 SALES 0.00 4,000.00 5,500.00
NET TOTAL 5,500.00
I filtered the specific records within the date range of 08/25/2015 and 08/26/2015 & it works well. My question is how I get the previous balance (10000) of the specific date (08/25/2015) in "OPENING BALANCE" row and the total balance in "NET TOTAL" row?
As per the given datas above, the report shows the 4 records well excluding the rows of OPENING BALANCE & NET TOTAL. I want to add the OPENING BALANCE row to show the opening balance of every vendors' accounts when I open the report. And if I view the records of a particular customer within a date range, then OPENING BALANCE row should show the previous balance until the start date of the given date criteria. Ex: If I select records from 08/25/2015 to 08/26/2015 as per above datas, the report will show the two particular records (2nd & 3rd). But I want to show the previous balance 10,000 or the total amount which is above to the star date (08/25/2015) should be shown in the OPENING BALANCE row. And the sub total amount of this vendor's
should be shown in the last NET TOTAL row. So, pls help me, how the formulas will come & how to do.
Thank you
I expect your great help
Thank you
It´s not too much clear to me, but you may try to get the values directly from the report using a variable to do it.
With the report opened, you may entry in the CrystalReport option and select "Selection Expert - Saved Data".
You must create a formula using the "Formula Field" at left panel, and build it using the top 3 windows:
The first top window shows the fields of the table your report handles;
The middle top window show some built-in formulas
SO, you must
Go to formula field and create a new one;
Select the field at the top-left window you want to get (the Openning)
Drag-drop it into the main window (below the 3 top selections) and save it as a new variable
Add this variable into your report. This way, you may get this field for calculations in the same report.
Is it you need to do?
Sorry if I cannot understand exactly what you need.
Hugs!
UPDATE
You may have TOTAL creating a formula of SUM within report.
Click on the field EXPENSE (which appear in 3 rows) and select in menu: CRYSTAL REPORTS - INSERT - SUMMARY. It will create a field that must be positioned at the bottom (in the new Group will be created).
Do the same with INCOME field.
Create a FORMULA FIELD at the bottom to calculate the difference between both SUMARIES. You can name each of these summary fields (like IncSum and ExpSum) and put in that third field: IncSum-ExpSum. You may also put them as invisible.
Your final balance should be:
OldBalance (the parameter I point next lines)
+
Movement (the field that reflects IncSum-ExpSum)
To get the last/old Balance, you may have to get it from code as pass it as a PARAMETER. To do this, create a parameter in te report: see in the FIELD EXPLORER you have a choice of PARAMETER FIELD. AFter create it, use this code:
Dim MyCrystalReportt As New ReportDocument()
MyCrystalReportt.Load(Application.StartupPath & "\ReportSample.rpt\")
MyCrystalReportt.SetDatabaseLogon(UserID, Password, DataSource, InitialCatalog)
Dim myLogonInfo As New TableLogOnInfo()
Dim myTable As Table
For Each myTable In MyCrystalReportt.Database.Tables
myLogonInfo = myTable.LogOnInfo
myLogonInfo.ConnectionInfo.ServerName = DataSource
myLogonInfo.ConnectionInfo.DatabaseName = InitialCatalog
myLogonInfo.ConnectionInfo.UserID = UserID
myLogonInfo.ConnectionInfo.Password = Password
myTable.ApplyLogOnInfo(myLogonInfo)
Next
MyCrystalReportt.SetParameterValue("Data1", LastBalance.Value.ToString)
ReportsForm.CrystalReportViewer1.Reportsource = MyCrystalReport
ReportsForm.show
Notes:
You may have a form (ReportsForm) with a Crystal Control within (CrystalReportViewer1).
Datasource is the Server Name or IP
InitialCatalog is the Database (not the table!)
The ParameterValue MUST be a string - is numeric is required, convert it to number inside the Report. Use a temp_field to get it (see formulas of Crystal and you will find some to make this task).
I hope it can help you.
Good luck.

profile loss statement report

i want to make crystal report of profit and loss statement
i have a column of "vr_type" and there are 4 types in it "sale invoice", purchase invoice" now i want that put a 4 formula s where i select that "sum (vr_detail.debit) where vr_detail.vr_type} sale invoice it make Sum of al these types so kindly tell me what formula i should use to get the result
in simply i want sum of sale,sum of purchase,sum of expense
thanks
Crystal Reports doesn't have a sumif() function. You will need to create a formula field for each type of field that you want to summarize, then insert a Summarized field on the formula field.
For example, you could create a field named 'sales invoice'. The text of the formula would be:
if {vr_detail.vr_type}="sales invoice" then
{vr_detail.debit}
else
0
You would then select this field, then Insert | Summary...
You'll need to repeat this for each field.
** edit **
If you simply want subtotals by vr_type, follow these steps:
Insert a group on the {vr_detail.vr_type} field. This will add a group-header (GH) and group-footer (GF) section to the report.
Insert a summarized field on the {vr_detail.debit} and/or {vr_detail.credit} field.