I have two tables: CB and Obj
CB
CBID CharaID Key Part
1 1 1
1 2 1
1 3 0
1 4 0
1 5 1
Obj
ObjID CharaID CharaValue
1 1 TestOne
2 2 TestTwo
3 3 TestThree
4 4 TestFour
5 5 TestFive
And I have two parameters that are send through c#.
#CBID and #CharaValue.
I need to join these two tables through CharaID column.
In table CB there is column Key part. When the value is 1 I need to check IF there is a same value that is send trough the two parameters with the values in the CharaValue column.
Example:
#CBID and #CharaValue send values for CharaID = 1 are 1 and TestOne and for CharaID = 2 are 2 and TestTwo. They both have Key Part 1 and I need to check if there is allready duplicate string values in Obj table, CharaValue column, bouth at the same time.
Can someone help me with the stored procedure to do this ?
I created some part but I need the rest...
#CBID int,
#CharaValue nvarchar(50)
SELECT a.CharaID, b.CharaValue
FROM CB AS A,
INNER JOIN Obj AS B ON a.CharaID = b.CharaID
WHERE "Key Part" = 1...
Related
I have a table include "ID" and "Values", and wanted to know how many times does value "A" jumped into another values like below
ID
Values
1
A
1
A
1
A
1
B
1
A
1
B
1
B
1
C
1
C
1
C
1
A
2
A
2
A
2
B
2
A
2
B
2
C
2
B
Expected Result:
ID
Values
Desired Output
1
A
0
1
A
0
1
A
1
1
B
0
1
A
1
1
B
0
1
B
0
1
C
0
1
C
0
1
C
0
1
A
0
2
A
0
2
A
1
2
B
0
2
A
1
2
B
0
2
C
0
2
B
0
The final table should be like this:
ID
Number of Transitions
1
2
2
2
You just need LEAD() to look at the next value:
select id, values, lead(value) over(partition by id) next_value
from table
Then you can compare next_value with values, and apply an iff(value='A' and next_value!='A', 1, 0).
Then just SUM() or COUNT() and GROUP BY.
You could also treat this as a regexp problem where you want to count how many times a given pattern occurs for each id. The missing piece in your question is -what column dictates the order in which the values appear for each id? You'll need that for either of the solutions
select id, regexp_count(listagg(val,',') within group (order by ordering_col), 'A,[^A]')
from t
group by id;
I would like to find the intersection of two columns in two matrix (see example below). So to find the position where A and B intersect -- in this case in position 3 and 5.
My solution so far, was to combine the two columns to one column and use intersect function on one column afterwards with a string. Is there a more elegant solution?
A = [1,1;1,3;1,4;2,1;2,5;3,1]
A =
1 1
1 3
1 4
2 1
2 5
3 1
B = [2,5;1,4]
B =
2 5
1 4
You can avoid combining the columns. When using intersect you can use the rows option.
A = [1,1;1,3;1,4;2,1;2,5;3,1]
B = [2,5;1,4]
[C,ia,ib] = intersect(B,A,'rows');
>>ib
3
5
Additionally, if you do not want the intersection result to be ordered you can use the stable option.
[C,ia,ib] = intersect(B,A,'rows','stable');
>>ib
5
3
Given a set of results in table 1
Col 1 Col 2 Col 3 Result
A B C 1
A B D 2
A B D 3
A B E 4
A B E 5
A B F 6
and a set of conditions in table 2
Col 1 Col 2 Col 3
A B C
A B D
A B E
how do I return a table of 'Result Sets' (or grouped results) in T-SQL to identify all the possible combinations of results from table 1 where all conditions in table 2 are met?
Result Set Result
1 1
1 2
1 4
2 1
2 2
2 5
3 1
3 3
3 4
4 1
4 3
4 5
EDIT: To clarify, the 'Result Set' value in the output table would be generated in the T-SQL to identify each set of results.
I have a sample table like the one below
table1
chktid ToUser
1 3
2 6
3 3
4 1
chkid is the parentId and sometimes this parent has childs and each of them, has sub-childs and so on
I have written a function to return all the childs and sub-childs of the particular chktid. Calling my function dbo.hierychyreturnfn(chktid) with different chktid as input parameter, I am getting the following results
For chktid = 1 and so select * from dbo.hierychyreturnfn(1), the output is:
chkchildid
10
11
12
13
For chktid = 2 and so select * from dbo.hierychyreturnfn(2), the output is:
chkchildid
14
15
16
For chktid = 3 and so select * from dbo.hierychyreturnfn(3), the output is:
chkchildid
19
For chktid = 4 and so select * from dbo.hierychyreturnfn(4), it returns no rows since chkid=4 has no childs.
My question is: Is there any way to join the function and table 1 and return all results?
My expected out put is:
chkid ToUser chkchildid
1 3 10
1 3 11
1 3 12
1 3 13
2 6 14
2 6 15
2 6 16
3 3 19
I am trying to use cross apply for getting this
SELECT a.chkid
,a.ToUser
,b.chkchildid
FROM table1 a
CROSS APPLY dbo.hierychyreturnfn(1) b // need to join table1 and pass each chkid
But I don't know how to pass each chkid to hierychyreturnfn(). I am stuck here. I have also tried code below also but it gives an error
"chkid" is not a recognized table hints option. If it is intended as a
parameter to a table-valued function or to the CHANGETABLE function,
ensure that your database compatibility mode is set to 90.
SELECT a.chkid
,a.ToUser
,b.chkchildid
FROM table1 a
CROSS APPLY dbo.hierychyreturnfn(chkid) b
Please help me to solve this issue.
I am trying to condense a table which contains multiple rows per event to a smaller table which contains counts of key sub-events within each event. Events are defined based on unique combinations across columns.
As a specific example, say I have the following data involving customer visits to various stores on different dates with different items purchased:
cust date store item_type
a 1 Main St 1
a 1 Main St 2
a 1 Main St 2
a 1 Main St 2
b 1 Main St 1
b 1 Main St 2
b 1 Main St 2
c 1 Main St 1
d 2 Elm St 1
d 2 Elm St 3
e 2 Main St 1
e 2 Main St 1
a 3 Main St 1
a 3 Main St 2
I would like to restructure the data to a table that contains a single line per customer visit on a given day, with appropriate counts. I am trying to understand how to use SQLite to condense this to:
Index cust date store n_items item1 item2 item3 item4
1 a 1 Main St 4 1 3 0 0
2 b 1 Main St 3 1 2 0 0
3 c 1 Main St 1 1 0 0 0
4 d 2 Elm St 2 1 0 1 0
5 e 2 Main St 2 2 0 0 0
6 a 3 Main St 2 1 1 0 0
I can do this in excel for this trivial example (begin with sumproduct( cutomer * date) as suggested here, followed by cumulative sum on this column to generate Index, then countif and countifs to generate desired counts).
Excel is poorly suited to doing this for thousands of rows, so I am looking for a solution using SQLite.
Sadly, my SQLite kung-fu is weak.
I think this is the closest I have found, but I am having trouble understanding exactly how to adapt it.
When I tried a more basic approach to begin by generating a unique index:
CREATE UNIQUE INDEX ui ON t(cust, date);
I get:
Error: indexed columns are not unique
I would greatly appreciate any help with where to start. Many thanks in advance!
To create one result record for each unique combination of column values, use GROUP BY.
The number of records in the group is available with COUNT.
To count specific item types, use a boolean expression like item_type=x, which returns 0 or 1, and sum this over all records in the group:
SELECT cust,
date,
store,
COUNT(*) AS n_items,
SUM(item_type = 1) AS item1,
SUM(item_type = 2) AS item2,
SUM(item_type = 3) AS item3,
SUM(item_type = 4) AS item4
FROM t
GROUP BY cust,
date,
store