I don't know where to start so I will just post an example what I need:
A B
5 0
10 0
15 0
20 0
25* 1*
30 0
35 1
40 1
45* 0
50 0
55 0
60 0
65 0
70 1
75 1
80 0
85 0
I need a query that will look for 1 in column B (first row found in 25 1),"entry point" from this point, go down and look for first column A, that is 20 more than a A in the start point - "exit point" (found row 45 0), return row index of "entry point" and "exit point" , in this case 25 1 and 45 0 rows.
Also, the tricky part might be, that I don't want row 35 1 or 40 1, it also contains 1 in B column, but I don't want rows in between entry and exit points, so the next applicable row will be row 70 1 as an entry point (75 1 also to be)
This is something I would be able to do in java loop without problem, but I am getting lower performance than I would like, so it's been suggested using db for this kind of operation should be faster. Is it possible to do such db query?
If i understand corectly that you need the first valid entry point and its first valid exit point, something like this should do the job
SELECT
(
SELECT
MIN(A)
FROM
table
WHERE
B = 1
) entry,
A exit,
FROM
table
WHERE
A + 20 >= (SELECT
MIN(A)
FROM
table
WHERE
B = 1)
AND
B = 0
LIMIT 1
I have following table and using SQL Server 2008
Tbl_TRNSACTION
RowNum Transaction Type InQty OutQty InPrice
1 IN 20 0 20
2 IN 50 0 40
3 OUT 0 10 -
4 IN 10 0 30
I expect output in following format
If TransType is IN,
then (InQty*InPrice) is added in CumulativeStockVal
and StockRate = (CumulativeStockVal / Balance).
If Transaction Type is OUT,
then (OutQty*previous(StockRate)) is subtracted from CumulativeStockVal.
RowNum TransType InQty OutQty Balance CumulatveStockVal StockRate
1 IN 20 0 20 400 20
2 IN 50 0 70 2400 34.285
3 OUT 0 10 60 2057.15 34.285
4 IN 10 0 70 2357.15 33.673
Please reply.Thanks.
Try using Window Offset Functions combining with AGGREGATE functions.
EDIT: I found you a tut where it is shown very clearly, watch from minute 15.
https://www.microsoftvirtualacademy.com/en-US/training-courses/querying-microsoft-sql-server-2012-databases-jump-start-8241?l=OZmttuJy_2304984382
i am new to the SSRS and Have data as below
RCnt Jobnumber NJCT JD pmt flag cnt1 cnt2
2 001008 0 PRESSURE SEAL PSI Y 0 0 2
2 001008 0 PRESSURE SEAL PSI Y 0 0 10
2 001008 0 PRESSURE SEAL PSI Y 0 0 1
1 001009 0 Single Sheet Duplex Legal PSI Y 0 0 1
1 001009 0 Single Sheet Duplex Legal PSI Y 0 0 4
6 001010 0 Single Sheet Duplex Legal PSI Y 0 0 6
5 001011 0 Single Sheet Duplex Legal PSI Y 0 0 5
5 001011 0 Single Sheet Duplex Legal PSI Y 0 0 3
13 001012 13 Single Sheet Duplex Legal PSI Y 0 13 0
13 001012 17 Single Sheet Duplex Legal PSI Y 0 17 0
13 001012 8 Single Sheet Duplex Legal PSI Y 0 8 0
And i want to display it as below ,
jobNumber NJCT JD cnt2 Rcnt
001008 0 PRESSURE SEAL 13 2
001009 0 Single Sheet Duplex Legal 5 1
001010 0 Single Sheet Duplex Legal 6 6
001011 0 Single Sheet Duplex Legal 8 5
001012 38 Single Sheet Duplex Legal 0 13
TOTAL 38 32 27
I know this grouping can be done at the server level in query but i don't have access to query and have to format this data as below in report using SSRS may be this is simple grouping i don't know when i googled about grouping it given me column level and row level grouping which left me very confused so any help related to this will be very helpful.Any links are also welcome to get grouping like this in SSRS report.
You have the following data in your Dataset:
Drag a table into the report and set its Dataset in the properties. Right click on the left of the table with the three lines and click Add Group -> Parent Group.
Group on the Jobnumber field, and check Include group header:
It will look like this:
Right click on the first column and choose Delete Columns.
Right click on the detail row and choose Delete Rows:
Click OK when prompted with Delete rows and associated groups?
Finally we need a table footer row - click on the bottom row and click Insert Row -> Outside Group - Below:
It should look like this:
Now add your data! Add new columns as required. When adding numeric data to these group and header fields, it will aggregate as a Sum by default:
Now the table is basically looking like the requirements; just format to taste:
Also, some links as requested:
Books Online, Adding Grouping and Totals (Reporting Services).
SQL Server Central, Tablix Tutorial.
These should be a good intro and are part of larger series of articles.
I'm new to SSRS (2008) and am trying to replicate an existing Access report. The report lists sales totals by month, and I've not had any issue resolving the basics into a tablix. However the original Access report then totals columns by quarter, 6 month and yearly values, and moreover applies incorporates subreports to compare these with previous year totals and targets. Schematically thus
Sale Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total
Customer 1 1 11 10 8 1 2 0 0 0 1 3 4 40
Customer 2 0 1 3 1 0 0 0 1 1 0 2 1 10
MonthlyTotals 1 12 13 9 1 2 0 1 1 1 5 5 50
Quarterly 26 12 2 11
6 Monthly 38 13
Yearly 51
Prev Yr Totals 2 10 10 5 5 0 0 0 0 0 0 10
Monthly Diff -1 2 3 4 -4 2 0 1 1 1 5 -5
Quarterly Diff 4 2 0 10
And so forth. Note that the parameters are set so that the report can start at any month to lists the columns (for different financial years)
I have everything working fine for the first 4 lines (sale, customer 1, customer 2 and monthly totals) in the above, but cannot see the best way of
Displaying the quarterly etc totals
Displaying the subreports to show the previous years and target values with the differences between them and the current values.
I have full access to the SQL Server and am comfortable with complex queries and stored procedures, so was inclined to generate the values in a table and display out that, but is there a better way? In particular handling the quarterly etc totals in SSRS would be advantageous.
I think it is not possible to caclulate the quarterly totals with the help of Tablix alone, unless your SQL Table has a separate column named "Quarter".
So to achieve your requirement, you have to write simple stored-procedure which returns the resultant table along with one more additional column named "Quarter" which contains the values from "Q1..Q4" which is calculated based on the sales month field.
Then you can add this new column (Quarter) as ColumnGroup on top of your existing group in the tablix.
Hence the whole idea is to create the result set with all possible columns for which you want to group.
I have a big table with 300,000 records. This table has a integer value called "velocity" and it`s value is from 0 to 100.
In the firsts records, the value is 0 and I want to remove. I want to remove from the query, the records where the velocity field repeats more than 10 times. For example:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20...
[-------remove this-----------].......................................................................[---------] <- do not remove this
Thanks
The easiest way to do this is with a loop.
You can write a stored procedure that iterates through the records, or you might do it outside of the database. I'd do it like that if this needs to be done once. If this is a continuous process, it's better to make sure that the extra data is just not inserted into the database in the first place.
Anyway, if you insist on doing this in pure SQL, without stored procedures with loops, you can use a query like this:
set #groupnum=0;
select
GroupNum,
count(*) as RecsInGroup
from
(
select
t1.id as Id,
t1.velocity as velocity1,
t2.velocity as velocity2,
if(t1.velocity<>t2.velocity,#groupnum:=#groupnum+1,#groupnum) as GroupNum
from
VelocityTable as t1
join
VelocityTable as t2
on
t1.id=t2.id-1
) as groups
group by
GroupNum
having RecsInGroup>10
What happens here?
Step 1
The inner query just selects all records in your table, but splits the data in sequential groups.
So, using your example, it does this:
velocity : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20
Groupnum : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 19 20 21 22
It does that by joining the table to itself, by linking subsequent records in the table. Every time the left and right velocity are different, the GroupNum is increased. Otherwise it's left unchanged.
Step 2
The result if the query is wrapped in an outer query, and grouped by GroupNum. Again, using your example it would result in this:
GroupNum,RecsInGroup
0,15 // !!
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
10,1
11,1
12,1
13,1
14,1
15,1
16,1
17,1
18,4 // !!
19,1
20,1
21,1
By Adding the having RecsInGroup>10 clause, the result becomes this:
GroupNum,RecsInGroup
0,15
Now, with this list of GroupNum's you can delete records.
Step 3
With the query above you have:
A list of all your records, with an added GroupNum column.
The list of GroupNum's that need to be removed.
Deleting the records should be easy at this point.
I'd just rip through the records sequentially, with a variable sized window that expands and contracts to comprehend identical values. Whenever the size is >= 10 when the value changes, delete the rows using the primary keys.
You can put BEGIN TRAN and COMMIT TRAN at the beginning and end of the DELETE statements to make things reasonably efficient.
thank you very much. I'm allmost there, but i tried it with a mySQL View as table source and it's not working (unkown table xxx). I can't use the whole table because it's have more than 19 millions records, I just need the record from a specific day, vehicle plate and city.