Merge Rows in Crystal Reports - asp.net

I have a crystal report, which I already developed in below format.
My new requirement is to add two new columns to the report to show “Total Men”
Total Men column should merge into one, if the Date, Department and Vendor is same.
And it should show the Total Men which is = No of men Day shift + No of Men Night shift

You can create a Formula Field e.g. ShiftTotal and edit it to insert following code:
WhilePrintingRecords;
numberVar ShiftSum;
if {table_name.Date_Field} = Next({table_name.Date_Field})
and {table_name.Department_Field} = Next({table_name.Department_Field})
and {table_name.Vendor_Field} = Next({table_name.Vendor_Field}) then
ShiftSum := ShiftSum + {table_name.Men_Field} + Next({table_name.Men_Field})
else
ShiftSum := 0;
Place ShiftTotal in the Details section next to Men_Field
Format ShiftTotal to enable Suppress if Zero in Custom Style of Number tab
Sort your report by Date, Department and Vendor fields.
Above solution will work if there are maximum two records for same Date, Department and Vendor. Otherwise, it would be better to use Cross-Tab format, which (IMO) is better for such summaries.

Related

Duration data type in TimeIntervalDto object

I am creating a database to store time entries that have been created in Clockify, I need to declare a data type for the duration field. A string is returned in the TimeIntervalDto and an example provided in the API documentation is "PT1M4S" or "PT1H30M15S". This is obviously a meaningful string if you know how to decode it.
The example given in the API documentation is:
"timeInterval": {
"duration": "PT1M4S", (Example: PT1H30M15S - 1 hour 30 minutes 15 seconds)
"end": "2018-06-12T14:01:41Z",
"start": "2018-06-12T14:00:37Z"
},
My questions are:
How to I translate duration to something meaningful; and
What is the maximum size I would need to cater for, assuming that I'm using varchar, or nvarchar as the data type?
You are working with the ISO 8601:2004(en) duration ISO format.
You have a pattern like the follow: "PnnYnnMnnDTnnHnnMnnS"
In detail:
The letter P represent a Period Format
Each letter represent a different value, for example if you want to indicate a period of two year, use 02Y.
For hours use the letter T.
So for your question:
If you can use C# languange before saving to database you can decode de pattern using something like this:
String pattern = "PT1H30M15S";
TimeSpan ts = System.Xml.XmlConvert.ToTimeSpan(pattern); //Use System.Xml because it works with ISO 8601:2004
You can look for further information on in the ISO webpage. https://www.iso.org/obp/ui/#iso:std:iso:8601:ed-3:v1:en
If you are using Postgres Database, you can create a new column based on end and start columns. So you don't need to create a new column just for the duration as it is already implicit with the other two columns.
If you want to get the durations you could do this query:
SELECT (t.end - t.start) as "duration", t.start, t.end from t
This will return you a new column "duration" in interval type.
If you really want to create a new column you could do a SQL trigger to fill a column "duration" of type interval using "start" and "end" columns every time a new entry is added to the table.

xpath returning empty text when web-scraping in r

I'm trying to scrape information from https://www.kff.org/interactive/subsidy-calculator. For instance, put state=California, zip=90001, income=20000, no coverage, 1 people, 1 adult, no children, age=21, no tobacco.
We get the following:
https://www.kff.org/interactive/subsidy-calculator/#state=ca&zip=94704&income-type=dollars&income=20000&employer-coverage=0&people=1&alternate-plan-family=individual&adult-count=1&adults%5B0%5D%5Bage%5D=21&adults%5B0%5D%5Btobacco%5D=0&child-count=0
I would like to get the numbers for "estimated financial help" and "your cost for a silver plan" (they are bolded-blue in the "Results" grey box, for some reason I can't upload the screenshot). When I use the xpath for the numbers, I get back empty string. This is not the case if I were to retrieve some other text (not in the grey box). I wonder what could be wrong with this. I have attached code below. Please forgive me if this is a stupid question since I'm very new to web-scraping. Thank you!
state = tolower('CA')
zip = 94704
income = 20000
people = 1
adult = 1
children = 0
url = paste0("https://www.kff.org/interactive/subsidy-calculator/#state=", state, "&zip=", zip, "&income-type=dollars&income=", income, "&employer-coverage=0&people=", people, "&alternate-plan-family=individual&adult-count=", adult, "&adults%5B0%5D%5Bage%5D=21&adults%5B0%5D%5Btobacco%5D=0&child-count=", children)
# This returns empty string
r = read_html(url) %>%
html_nodes(xpath ='//*[#id="subsidy-calculator-new"]/div[5]/div/div/dl/dd[1]/span') %>% html_text()
# This returns "Number of children (20 and younger) enrolling in Marketplace coverage", a line that's not in the grey box.
r = read_html(url) %>%
html_nodes(xpath = '//*[#id="subsidy-form"]/div[2]/div[3]/div[3]/p') %>%
html_text()
The values are generated through scripts that run on the page. Your current method won't allow for this hence your result. You are likely better off using a method which allows scripts to run such as RSelenium.
The form you complete #subsidy-form feeds values into a template in a script tag #results-template. The associated calculations are covered in this script https://www.kff.org/wp-content/themes/kaiser-foundation-2016/interactives/subsidy-calculator/2019/calculator.js?ver=1.7.7 where you will find the logic and the pre-set values such as poverty lines per year.
The simplest quick view is probably to inspect the javascript variables when the new SubsidyCalculator object is created to process the form i.e. js starting with var sc = new SubsidyCalculator. You could 'reverse engineer' those variables with your values plus the values returned from the json below which I think, but haven't confirmed, feed the 6 variables that begin with kff_sc, according to zipcode, into the calculator e.g. silver: kff_sc.silver . You get an idea of the ballpark figures given there are default values given at top of script.
Figures in relation to zipcode are retrieved from this: https://www.kff.org/wp-content/themes/kaiser-foundation-2016/interactives/subsidy-calculator/2019/json/zips/94.json where the last two numbers before .json are the first two numbers of zipcode. You can determine this from the input validation script: https://www.kff.org/wp-content/themes/kaiser-foundation-2016/interactives/subsidy-calculator/2019/shared.js?ver=1.7.7
var bucket = $( this ).val().substring( 0, 2 );
if ( kff_sc.buckets[bucket] ) return;
$.ajax( '/wp-content/themes/vip/kaiser-foundation-2016/interactives/subsidy-calculator/2019/json/zips/' + bucket + '.json',
The first two digits determine the bucket.
All in all you could likely implement your own calculator but you would be re-inventing the wheel. Seems easier to just automate the browser and then extract the resultant values.

How do I limit query results, when distinct isn't distinct?

I have a bill of material file that I am trying to reduce to only the unique parts, and related data for the line. The problem I'm running into is multiple instances of a part number due to variations in the formatting or language in the part name from the system/s that a third party pulls the data from.
pn123 part_name
pn123 Part-name
pn123 Part name
pn123 German name
All other fields I select are equal, how do I limit this in the where clause to just one instance of the above for all different part numbers? Is there an equivalent to MAX() in a text string?
I am working around the issue in excel, by deleting the dupes.
select distinct
adhoc.ats_esh.Customs_Entry_Num
[VIN]as [Qlickview VIN]
,[Build_Date]
,[BOM_Level]
,[9802].[Supplier]
,[Part_number]
,[Part_Name] *******THIS IS THE PROBLEM FIELD*******
,[Unit_Price]
,[Usage]
,[Extended_Price]
from
adhoc.IMFM_9802_EU_AP [9802] inner join ADHOC.ATS_ESL
ON [9802].VIN = ADHOC.ATS_ESL.Part_Num
inner join adhoc.ATS_ESH
ON ADHOC.ATS_ESH.Trans_SK = ADHOC.ATS_ESL.Trans_SK
where
adhoc.ats_esh.importer ='ACME_CO'
and adhoc.ATS_ESH.ENTRY_SUMMARY_DATE >= '2/01/2018'
And adhoc.ATS_ESH.ENTRY_SUMMARY_DATE < '3/01/2018'
AND adhoc.ats_esl.Supplier in('supplier1','supplier2','supplier3')
--and adhoc.ats_esl.Part_Num like '%ABC%'
--and [BOM_Level] = '1' --**** use MAX()
The right way to do this is to have a table with part name and number where part number is a unique key. This way you join on Part_number and get a unique Part_name. You can also use Max(Part_name) if you use Group by Part_number but this will require you to rework your query a little bit.

datatype in the table should be Date/time or short tgext?

I am desiging dataentryform in access . I made tables and the datatye of one field is Date/time but in property field in format I put mm.\yyyy to get month and year . how should I define input mask for it? I want to see --.----
when I enter data in form I can see calender beside this field but I have to choose day as well I dont want to choose day however the day that I chose doesnt save in table but I have to choose day aw well.
more explaination: I have 2 fields that should define as mm.yyyy .one field is (start of project example 01.2010) and the next field is end of project which also has the same format mm.yyyy (03.2015).
and later I want to get a query which shows duration of project ( end of project - start of project) . so I should also do calculation according to my data.
but I dont know what kind of data type should I define in the table for these two fields. at first I tried Date/Time . but I couldnt find suitable format. then I changed it to short text and I entered 00.0000 in input mask.
what is the best solution because I have to calculate duration of project according to these two fields later? (in queries)
There are many ways around this. One is simply to allow the user to type freely in an unbound textbox, then validate/correct before updating.
Another is to have two/three juxtaposed textboxes for year-month(-day), then build the date value with DateSerial(y, m, d).
in VBA, mm/yyyy alone can never form a date value, so must silently add a day somehow.
Input masks should be avoided as they are a prime hate object for users.
If you insist, you can get an idea how to handle this from my article:
Entering ISO formatted date with input mask and full validation in Microsoft Access
It assumes the ISO sequence because year-month determines the possible day values.
Addendum:
Though decimal months is not an exact measure due to the varying day counts of the months, you will get pretty close with this function:
Public Function DecimalMonths( _
ByVal Date1 As Date, _
ByVal Date2 As Date) _
As Double
Dim Part1 As Double
Dim Part2 As Double
Dim Months As Double
Dim Days1 As Integer
Dim Days2 As Integer
Days1 = Day(DateSerial(Year(Date1), Month(Date1) + 1, 0))
Days2 = Day(DateSerial(Year(Date2), Month(Date2) + 1, 0))
Months = DateDiff("m", Date1, Date2)
Part1 = (Day(Date1) - 1) / Days1
Part2 = (Day(Date2) - 1) / Days2
Months = Months - Part1 + Part2
DecimalMonths = Months
End Function
Please note, that the count from 2017-04-15 to 2017-06-01 is 1.53
while it would be 1.5 from 2017-04-16 to 2017-06-01.

Dynamically create categories for SQLite pivot/crosstab

I realise it is possible to create a crosstab within sqlite, but is it possible to dynamically determine the relevant categories/columns at runtime rather than hardcoding them?
Given the following example, it can get rather tedious ...
SELECT
shop_id,
sum(CASE WHEN product = 'Fiesta' THEN units END) as Fiesta,
sum(CASE WHEN product = 'Focus' THEN units END) as Focus,
sum(CASE WHEN product = 'Puma' THEN units END) as Puma,
sum(units) AS total
FROM sales
GROUP BY shop_id
I managed to do this in SQLServer in a stored proceedure before and wondered if there was anything equivalent.
No, you can't do that in SQLite, since analytic means in SQLite lack many usefull features available in heavier RDBMSes. Here is the corresponding ticket, but it is in the pending status.
Old question, but anyway: I faked something like this in Ruby on Rails. For my purposes it works well.
def self.yearstats(attr)
# Determine number of columns
minyear = self.select("strftime('%Y', min(recorded_at)) AS minyear").first.minyear.to_i
maxyear = self.select("strftime('%Y', max(recorded_at)) AS maxyear").first.maxyear.to_i
# Stitch SQL query. Use explicit same column name to not confuse Rails' typecasting.
select_str = ["strftime('#{minyear}/%m/%d %H:%m', recorded_at) AS recorded_at"]
(minyear..maxyear).to_a.each do |y|
# avg because the table might have multiple rows per day
select_str += ["avg(case when strftime('%Y', recorded_at)='#{y}' then #{attr} end) AS value#{y}"]
end
self.select(select_str.join(",")).group("strftime('%m/%d', recorded_at)").all.collect {|row|
[row.recorded_at.utc.to_i*1000] + (minyear..maxyear).to_a.collect { |y| row.send("value#{y}") }
}
end
Note that for the first column I used a (fake) date using a fixed first year so that my graphing routines get a "real" date, not just day/month display. This is a little dirty but it works for my purposes. (Improvements still welcome...)
Instead of getting minyear and maxyear, you might want to do a SELECT DISTINCT.. to get the unique list of categories and modify the code appropriately.

Resources