I am not a very experienced database designer and would like make this
table design better.
ID Title ParentID GroupID Price
1 single product 0 0 12.00 // single
2 main product 0 0 44.00 // parent
3 sub product 2 0 4.00 // child
4 product set A 0 A 49.00 // complete price (ignore part price)
5 set part A1 0 A 22.00
6 set part A2 0 A 6.00
7 set part A3 0 A 31.00
8 product set B 0 B 0 // sum price (22 + 6 + 31 = 59)
9 set part B1 0 B 22.00
10 set part B2 0 B 6.00
11 set part B3 0 B 31.00
So there are four different products in the basket (and count
basket products with sql is a problem ;)). Not very straight
SQL and I need a lot of logic to handle the result.
I know that I can realise parent/child products with GroupID
but parent/child products will be displayed different in the frontend.
I need the information, is it a set or a parent/child product...
Does anyone have an idea how to realize this better?
Thank you very much & best regards
I have a scenario where I need to display only non zero columns
id C1 C2 C3 C4
1 0 1 0 2
2 0 1 0 5
3 0 9 0 3
i want the output as below
id C2 C4
1 1 2
2 1 5
3 9 3
An SQL query always results in before-known columns. So you cannot do what you want to do in pure SQL. (And not being able to do something with SQL only is often a sign of a poor database design. Sometimes however it's that SQL is about how to get data, not how to display it.)
What you can do is get the columns' sums in one query, then build a new query dynamically (manually or within an app with Java, PHP or whatever programming language is used) that only selects the non-zero columns.
I'm having issues using the line chart control in SSRS. If I was in excel this would be easy but for some reason I can't wrap my head around how to do it in SSRS
I'm returning 5 rows of data from a database and need to chart 3 of the rows.
Name J F M A M J J A S
1 Requested 13 19 4 20 2 0 0 0 0
2 Completed 1 0 0 4 1 0 0 0 0
3 % Completed .7 0 0 0.2 0.5 0 0 0 0
4 Monthly Ba 12 19 4 16 1 0 0 0 0
5 YTD Backlog 12 31 35 51 52 52 52 52 52
The rows consist of the name and the value for that month.
I'm trying to display a chart for rows 1, 2, and 5 that would look like a normal line graph with each of the Months being on the horizontal column and the value being the data point.
However, when I start adding things to my chart I'm getting 36 different series, one for each Month for each series and nothing seems to be working right. It's also splitting into Group 1 and 2 at the bottom which makes no sense to me. I feel like I'm missing something simple, most likely a grouping of some kind.
EDIT:
I ended up taking Nathan's suggestion and added an unpivot on the table to rotate the data into an acceptable format for SSRS
Thanks
Can you alter the query that is returning the results for the dataset?
If so try changing it so that the dataset looks more like this:
monthnumber month Requested Completed YTD Backlog
1 J 13 1 12
2 F 19 0 31
3 M 4 0 35
4 A 20 4 51
etc.
You should be able to add "Requested", "Completed" and "YTD Backlog" as Values and "month" as a Category to produce the chart you want.
I added a "monthnumber" column, as you will probably want something like this so that you can set the category (month) sorting expression to this, otherwise they will appear in alphabetical order.
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.