I have an internal table consisting the ff structure.
SO Customer Materialno Materialgroup2 Liters
001 James 1 GroupA 5
001 James 2 GroupA 10
001 James 3 GroupB 20
002 John 2 GroupA 10
002 John 3 GroupA 10
002 John 4 GroupB 20
002 John 5 GroupB 5
I need to add it by customer, billing doc, and by their material group2. Now the final output of the internal table should be like this.
SO Customer MaterialGroup2 Liters
001 James GroupA 15
001 James GroupB 20
002 John GroupA 20
002 John GroupB 25
rt_result = VALUE #( FOR GROUPS group OF ls_source IN it_source
GROUP BY ( so = ls_source-so
customer = ls_source-customer
material_group = ls_source-material_group )
( so = group-so
customer = group-customer
material_group = group-material_group
liters = REDUCE #( INIT lv_liters = 0
FOR ls_member IN GROUP group
NEXT lv_liters = lv_liters + ls_member-liters ) ) ).
REPORT ztest.
TYPES:
BEGIN OF ts_input,
so TYPE char3,
customer TYPE char5,
materialno TYPE i,
materialgroup TYPE char6,
liters TYPE i,
END OF ts_input,
BEGIN OF ts_output,
so TYPE char3,
customer TYPE char5,
materialgroup TYPE char6,
liters TYPE i,
END OF ts_output.
DATA:
lv_liters TYPE i,
gt_input TYPE STANDARD TABLE OF ts_input,
gs_input TYPE ts_input,
gt_temp TYPE STANDARD TABLE OF ts_output,
gs_temp TYPE ts_output,
gt_output TYPE STANDARD TABLE OF ts_output,
gs_output TYPE ts_output.
START-OF-SELECTION.
gs_input-so = '001'.
gs_input-customer = 'james'.
gs_input-materialno = 1.
gs_input-materialgroup = 'GroupA'.
gs_input-liters = 5.
APPEND gs_input TO gt_input.
gs_input-so = '001'.
gs_input-customer = 'james'.
gs_input-materialno = 2.
gs_input-materialgroup = 'GroupA'.
gs_input-liters = 10.
APPEND gs_input TO gt_input.
gs_input-so = '001'.
gs_input-customer = 'james'.
gs_input-materialno = 3.
gs_input-materialgroup = 'GroupB'.
gs_input-liters = 20.
APPEND gs_input TO gt_input.
gs_input-so = '002'.
gs_input-customer = 'john'.
gs_input-materialno = 2.
gs_input-materialgroup = 'GroupA'.
gs_input-liters = 10.
APPEND gs_input TO gt_input.
gs_input-so = '002'.
gs_input-customer = 'john'.
gs_input-materialno = 3.
gs_input-materialgroup = 'GroupA'.
gs_input-liters = 10.
APPEND gs_input TO gt_input.
gs_input-so = '002'.
gs_input-customer = 'john'.
gs_input-materialno = 4.
gs_input-materialgroup = 'GroupB'.
gs_input-liters = 20.
APPEND gs_input TO gt_input.
gs_input-so = '002'.
gs_input-customer = 'john'.
gs_input-materialno = 5.
gs_input-materialgroup = 'GroupB'.
gs_input-liters = 5.
APPEND gs_input TO gt_input.
" move to temp. table, skip MaterialNo
LOOP AT gt_input INTO gs_input.
gs_temp-so = gs_input-so.
gs_temp-customer = gs_input-customer.
gs_temp-materialgroup = gs_input-materialgroup.
gs_temp-liters = gs_input-liters.
APPEND gs_temp TO gt_temp.
ENDLOOP.
SORT gt_temp ASCENDING BY so customer materialgroup.
LOOP AT gt_temp INTO gs_temp.
AT NEW materialgroup.
CLEAR lv_liters.
ENDAT.
lv_liters = lv_liters + gs_temp-liters.
AT END OF materialgroup.
gs_output-so = gs_temp-so.
gs_output-customer = gs_temp-customer.
gs_output-materialgroup = gs_temp-materialgroup.
gs_output-liters = lv_liters.
APPEND gs_output TO gt_output.
ENDAT.
ENDLOOP.
Related
Below is simple code where I want to display location based on checkbox selection.
Eg: id p_pune is selected at seletion screen then after WRITE command my output should be as below
EMPID NAME LOCATION
1 A PUNE
Code:
TYPES: BEGIN OF ty_emp,
empid TYPE i,
name TYPE char5,
location TYPE char6,
END OF ty_emp.
DATA: wa_emp TYPE ty_emp,
it_emp TYPE TABLE OF ty_emp.
DATA: gd_ucomm TYPE sy-ucomm.
wa_emp-empid = 1.
wa_emp-name = 'A'.
wa_emp-location = 'Pune'.
append wa_emp to it_emp.
CLEAR wa_emp.
wa_emp-empid = 2.
wa_emp-name = 'B'.
wa_emp-location = 'Mumbai'.
append wa_emp to it_emp.
CLEAR wa_emp.
wa_emp-empid = 3.
wa_emp-name = 'C'.
wa_emp-location = 'Delhi'.
append wa_emp to it_emp.
CLEAR wa_emp.
wa_emp-empid = 4.
wa_emp-name = 'D'.
wa_emp-location = 'Noida'.
append wa_emp to it_emp.
CLEAR wa_emp.
PARAMETERS: p_pune AS CHECKBOX USER-COMMAND c1,
p_mumbai AS CHECKBOX USER-COMMAND c2,
p_delhi AS CHECKBOX USER-COMMAND c3,
p_noida AS CHECKBOX USER-COMMAND c4.
You can try this:
IF p_pune = abap_true.
READ TABLE it_emp INTO wa_emp INDEX '1'.
WRITE:/ wa_emp-empid, wa_emp-name, wa_emp-location.
ENDIF.
IF p_mumbai = abap_true.
READ TABLE it_emp INTO wa_emp INDEX '2'.
WRITE:/ wa_emp-empid, wa_emp-name, wa_emp-location.
ENDIF.
IF p_delhi = abap_true.
READ TABLE it_emp INTO wa_emp INDEX '3'.
WRITE:/ wa_emp-empid, wa_emp-name, wa_emp-location.
ENDIF.
IF p_noida = abap_true.
READ TABLE it_emp INTO wa_emp INDEX '4'.
WRITE:/ wa_emp-empid, wa_emp-name, wa_emp-location.
ENDIF.
When you check multiple checkboxes, it will display multiple locations.
If you want to display only one location, I suggest you use RADIO BUTTON instead of CHECKBOX.
You could convert the boolean values of the parameters to a location, using the COND statement. Then you can loop over the internal table with LOOP AT and use it's WHERE condition to filter out the location you need.
TYPES:
BEGIN OF employee,
empid TYPE i,
name TYPE char5,
location TYPE char6,
END OF employee,
employees TYPE STANDARD TABLE OF employee WITH EMPTY KEY.
DATA(employees) = VALUE employees(
( empid = 1 name = 'A' location = 'Pune' )
( empid = 2 name = 'B' location = 'Mumbai' )
( empid = 3 name = 'C' location = 'Dehli' )
( empid = 4 name = 'D' location = 'Noida' )
).
PARAMETERS: pune AS CHECKBOX,
mumbai AS CHECKBOX,
dehli AS CHECKBOX,
noida AS CHECKBOX.
START-OF-SELECTION.
DATA(wanted_location) = COND char6(
WHEN pune = abap_true THEN 'Pune'
WHEN mumbai = abap_true THEN 'Mumbai'
WHEN dehli = abap_true THEN 'Dehli'
WHEN noida = abap_true THEN 'Noida'
).
LOOP AT employees INTO DATA(employee)
WHERE location = wanted_location.
WRITE:
employee-empid,
employee-location,
employee-name.
NEW-LINE.
ENDLOOP.
I have a table like below:
caseid | ncode | code | test
1 1 ABC TRUE
1 2 DEF TRUE
2 1 ABC TRUE
3 1 DEF TRUE
3 2 HIJ FALSE
Where caseid represents an individual case. This table creates the relationship that each individual case can have multiple codes associated with it (ncode and code). test is just a variable that tracks a boolean value of interest.
I have specific requirements for my query:
I need all cases where code = ABC and ncode = 1 and test = TRUE. This criteria has the highest priority.
Of those cases from #1, I need to create an additional column called hasdef that is a boolean that indicates if that specific caseid has any other rows where code = DEF and test = TRUE. It should be TRUE if so, otherwise FALSE.
So from the above table, what should return is:
caseid | ncode | code | test | hasdef
1 1 ABC TRUE TRUE
2 1 ABC TRUE FALSE
caseid = 1 returns because code = ABC, ncode = 1, and test = TRUE. hasdef = TRUE because in the second row, caseid = 1, code = DEF and test = TRUE.
caseid = 2 returns because code = ABC, ncode = 1, and test = TRUE. hasdef = FALSE because there is no other row with caseid = 2 where code = DEF.
caseid = 3 does not return. Even though there is a row where code = DEF and test = TRUE, the first criteria (code = ABC and ncode = 1) is not first satisfied.
This is what I have so far, but I am not confident it is working as desired:
select tab1.*, tab2.code is not null as hasdef from
(select * from mytable
where code = 'ABC' and ncode = 1) as tab1
left join (
select caseid, any_value(code) code, any_value(test) test
from mytable
group by caseid
having code = 'DEF' and test is true
) as tab2
using(caseid)
order by caseid
Below is for BigQuery Standard SQL
#standardSQL
select * from (
select *,
0 < countif(code = 'DEF' and test is true) over(partition by caseid) as hasdef
from `project.dataset.table`
)
where code = 'ABC' and ncode = 1 and test is true
if to apply to sample data from your question - output is
Note: you can replace test is true with just test as in below
select * from (
select *,
0 < countif(code = 'DEF' and test) over(partition by caseid) as hasdef
from `project.dataset.table`
)
where code = 'ABC' and ncode = 1 and test
I have a table like this:
MAName feldtext
------------------
karl fieldtext1
karl fieldtext2
karl fieldtext1
karl fieldtext3
karl fieldtext4
karl fieldtext2
karl fieldtext5
karl fieldtext3
karl fieldtext3
susi fieldtext1
susi fieldtext4
john fieldtext2
john fieldtext5
john fieldtext5
and I need:
MAName fieldtext1 fieldtext2 fieldtext3 fieldtext4 fieldtext5 FehlerJeMA
karl 2 2 3 1 1 9
susi 1 0 0 1 0 2
john 0 1 0 0 2 3
The columns fieldtext can go from fieldtext1 to fieldtextn, it's dynamic, depending on query.
I was looking here for solutions and found, so my approach:
Dim dt2 As New DataTable
Dim nn As Integer = 0
Dim Zeile As DataRow
dt2.Columns.Add("MAName")
' fieldtext distinct
Dim query2 = (From dr In (From d In newTable2.AsEnumerable Select New With {.feldtext1 = d("feldtext")}) Select dr.feldtext1 Distinct)
For Each Feldtext In query2
dt2.Columns.Add(Feldtext)
Next
column = New DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = "FehlerJeMA"
dt2.Columns.Add(column)
' MAName distinct
Dim query3 = (From dr In (From d In newTable2.AsEnumerable Select New With {.MAName2 = d("MAName")}) Select dr.MAName2.ToString.ToLower Distinct)
For Each Mitarbeiter In query3
Zeile = dt2.NewRow()
Zeile(0) = Mitarbeiter.ToString.ToLower
MA2 = Mitarbeiter.ToString.ToLower
nn = 1
For Each colName2 In query2
Fehler2 = colName2
Dim AnzahlFehler As String = (From row In newTable2.Rows Select row Where row("MAName").ToString.ToLower = MA2 And row("feldtext") = Fehler2).Count
If AnzahlFehler = 0 Then
AnzahlFehler = ""
End If
Zeile(nn) = AnzahlFehler
nn += 1
If AnzahlFehler <> "" Then
FehlerJeMA += CInt(AnzahlFehler)
End If
Next
Zeile(nn) = FehlerJeMA
dt2.Rows.Add(Zeile)
Next
This works, but is very slow...
It could be the case that in my table has more than 10.000 rows...
So my question is: what is fastest approach to get the result?
Is it some kind of cross table with linq? Other approaches?
In C# you will be able to use the code, try to translate it for your problem:
var pivotData = data.GroupBy(x => new {x.MAName, x.feldtext}, (key, group) => new { MAName = key.Column1, feldtext = key.Column2, count = group.Count() });
SO, each item has an id, a setId, and a name.
Current data may only have one NAME field filled out in an entire set.
EX>
>ID = 1 >NAME = 'Bob' >SETID = 5
>ID = 2 >NAME = NULL >SETID = 5
>ID = 3 >NAME = NULL >SETID = 5
>ID = 4 >NAME = NULL >SETID = 5
I am looking for an SQL script that GET the one record that is not null in a "SET"
SET the remaining items in the "SET" for that "NAME" record.
The end result I am aiming for would look like this:
>ID = 1 >NAME = 'Bob' >SETID = 5
>ID = 2 >NAME = 'Bob' >SETID = 5
>ID = 3 >NAME = 'Bob' >SETID = 5
>ID = 4 >NAME = 'Bob' >SETID = 5
Assuming there is only one Name in a set that is not null, you could use a CTE and do something like:
;WITH NameHelper AS
(
SELECT
Name,
SetID
FROM MyTable
WHERE Name IS NOT NULL
GROUP BY Name, SetID
)
UPDATE MyTable SET Name = NameHelper.Name
FROM MyTable
INNER JOIN NameHelper ON MyTable.SetID = NameHelper.SetID
Please i have a project that i query the database using entity framework as my ORM. This is my query
Dim subjectSearch = From subSearch In DemoSchool.EssenceSubjectRegistrations Where subSearch.Session = drpSession.SelectedItem.Text _
AndAlso subSearch.Term = drpTerm.SelectedItem.Text AndAlso subSearch.RealClass.ClassSN = findClassSN.FirstOrDefault AndAlso _
subSearch.SubjectCode = drpSubject.SelectedValue _
Select New With {.SubjectRegSN = subSearch.SubjectRegSN,
.FirstName = subSearch.Student.FirstName,
.Surname = subSearch.Student.Surname,
.CA1 = subSearch.CA1,
.CA2 = subSearch.CA2,
.CA3 = subSearch.CA3,
.CA4 = subSearch.CA4,
.CA5 = subSearch.CA5}
Then i query my result so that i can be able to do some operation on it by doing this
Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _
.Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN,
.FirstName = jamie.FirstName,
.Surname = jamie.Surname,
.CA1 = jamie.CA1,
.CA2 = jamie.CA2,
.CA3 = jamie.CA3,
.CA4 = jamie.CA4,
.CA5 = jamie.CA5,
.MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5) / 5})
The result of the second query is bounded to the gridview which renders properly as it is suppose to rendered. My problem is that i want to create a virtual column called Rank on the gridview after the .MidTerm bounded column that will display the position of each person record in the search result.
NAME CA1 CA2 CA3 CA4 CA5 MIDTERM RANK
James 50 50 60 40 60 52 3
Essty 100 50 50 50 50 60 2
Markus 100 40 50 60 50 60 2
Code 100 100 100 100 50 90 1
Above is a format of the gridview columns. I want column Rank to be a virtual column that is calculated based on the Midterm score of the students.Code VB scored 90 so his rank so be 1 and so on.Please i don't know how to calculate this rank column and the code to get the highest and the smallest.Really i need help thanks
Two ways.
Use Unbound column and calculate/display the rank
Have a RANK field and pre-calculate it and bound it, something like:
Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _
.Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN,
.FirstName = jamie.FirstName,
.Surname = jamie.Surname,
.CA1 = jamie.CA1,
.CA2 = jamie.CA2,
.CA3 = jamie.CA3,
.CA4 = jamie.CA4,
.CA5 = jamie.CA5,
.MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5) / 5,
.RANK = -1}).ToList()
dim sorted = secSubjectSearch.Select(function(n) n.MidTerm).Distinct().OrderByDescending(function(n) n).ToList()
for each itm in secSubjectSearch
itm.RANK = sorted.IndexOf(itm.MidTerm) + 1
next
-- Not tested, wrote it with notepad...