Sort a View SQL Server 2014 - sql-server-2014

I am trying to find a way to sort the results in a view. I know the Order by command does not work. The reason for this is that we are using a BI tool 'Board' to drill through to the raw data. Unfortunately there is no way in the program to sort a drill through as I would a regular query since the Where clause is dynamic and the program always puts it at the end of the query.
Hence an Order by would appear prior to a Where which results in an error.
Any suggestions would be greatly appreciated.
Thanks,
Scott

So there's a couple of ways to achieve this, one is to make sure that the data the view is reading is already sorted, to do this, you make sure that the indexes on the table/s that the view is reading from are sorted the way you want, if this isn't possible, then you could also create a clustered index on the view itself and specify the sort order there.
If creating indexes is not possible, there is another way that I have found to work, but is not always ideal as it creates an extra column in your result set that doesn't mean anything, you can do this:
SELECT ROW_NUMBER() OVER ( ORDER BY [col1] ASC ) AS [sort_col] ,
[col1] ,
[col2] ,
[col3]
FROM [dbo].[table];
If you do this, you will end up with the extra column giving the row number, but the data will be sorted by [col1]. One thing to mention with this is that if you do put this into the view definition, you must still select that column in your query for the sorting to take place.

Related

How to I select a distinct count of a column on SSRS 2008?

Im using SSRS 2008. I am counting a column with the title "Good". When i run the information if the column comes back with a Y it means yes if an N it means no. I am counting the Y's of the column. Here is the catch sometimes the information comes back twice. That is suppose to happen but when it comes back with Y even though it is the same information i only want it to count 1 of those Y's. What is the correct formula i can use to do this? My current formula is =Count(Fields!Good.Value) What can i add on to this or change this to make it do this?
Your question doesnt make sense to me, I think you have probably left out the context of your distinct requirement.
Anyway, as it is asked, I would try:
=CountDistinct( Iif ( Fields!Good.Value = "Y" , Fields!Good.Value , Nothing ) )
This formula will count the Ys in the column:
=sum(Iif(Fields!Good.Value = "Y",1,0))
Note: It assumes that you have your data structured such that every row is counted. You'll need to set some kind of flag or something in the table if you want to exclude some of the Ys and modify the above formula to check for that flag.
Best practice would be to structure your data so it doesn't have any duplicate rows. Either a filter at the SSRS dataset, or at the datasource.

SQL Query on transformed table in SSIS

I have joined 5 tables and done transformation on these tables. Now I got a single table at the end. Now I want to perform sql query on this single table to filter records. But I don't know how to perform simple sql query on this table. I have attached a snap shot which shows the resulting table. How I get this resulting data set as the source? I want to populate my destination after filter out this data.
I am using SSIS 2008.
Click here to see the Table on which I want to perform a simple sql query
SELECT * FROM `first_table`
where `some_column` =
(
SELECT `*`
FROM second_table
WHERE
`some_column2`='something'
LIMIT 1
)
Try this code This will help. You can even use this to connect all those four tables with each other.
From the image you posted, it looks like you have a set of data in the dataflow you're trying to query against. You need to do one of two things at this point. Either you insert the data into a table in the database and use another data flow to query it, or you use use a conditional split (or multicast and conditional splits) to filter the rows down further from there.
Without more detail about what you're actually trying to accomplish, these are the recommendations I can determine.
You could send the rows into a record set destination, but you aren't able to query it like a regular table and you'd need some C#/VB skills to access it to do more than a FOR EACH loop.
Assuming your sql query that you want to run against the resulting table is simple, you can use a script component task. By simple, I mean, if it is of this nature:
SELECT * FROM T WHERE a = 'zz' and b = 'XX' etc.
However, if your query has self joins, then you would be better of dumping the outcome of joining those 5 tables in to a physical table, and go from there.
It appears that query is going to be real straight-forward; in that case using a script component would be helpful.
A separate question: It's advisable to do the sorting at the database level. You are using 5 sort tasks in your solution. Can you please elucidate the reason?

How could SUM(column) give incorrect results in MySQL?

I have the following simple MySQL query, called from PHP:
SELECT foo_id, SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
GROUP BY foo_id
This works fine, except for one $foo_id, which returns about 2.5 times greater than the sum of the number_of_guests field.
What could cause this behavior for only a certain value of $foo_id?
Is there a better way to do this?
Your query should work. The error is most likely in the other method you are using to verify the result.
Is there a better way to do this?
Yes. Since you are only fetching one group there's no need for your GROUP BY clause:
SELECT SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
The problem is that there was one row with a large value for number_of_guests. I didn't see in in browsing the data because there are a few hundred rows. It didn't show up when I copied and pasted from HTML page into Excel because that row was missing most of the other columns, and the HTML page has all the columns.
Thanks for all your help!

Report rows differ from SQL result

I am stumped.
I have a select statement that returns the data fine in the data pane, but in the resulting report, one row is our of sort order and falls about 25 rows down in the data where it shouldn't be.
How do I trouble shoot something like that?
Thanks!i
Somewhere in the report you must be sorting the dataset. In this case, the data will look fine in the data pane but will be out of the expected order in the report. Try adding a brand new table to the report and apply the dataset. It should match the data pane.
How do I trouble shoot something like that?
You start by posting the select statement here so we can help without having to hone our psychic debugging skills :-)
It's possible that you're not ordering on every column you think you are. It's also possible that SSRS re-orders the data even after you've retrieved it.
They'd be the first two places I'd look.

Nonstandard SSIS lookup

I have a situation where I am trying to lookup a value in one table based on values in another table, using a BETWEEN operator and not an = operator.
In one table, I have a value "EffectiveDate". I want to get a Weight number from another table, but the other table has two fields: "Inception" and "Termination". What I want to do is extract the Weight from that table for use where the EffectiveDate is between Inception and Termination.
SSIS doesn't seem to provide a way to do this. It's good at matching one column to another column, but doesn't seem to allow one to many-column comparison/operations.
Am I missing anything? Is this possible to do somehow?
Have a look on this URL as it could be a performance killer
http://blogs.msdn.com/mattm/archive/2008/11/25/lookup-pattern-range-lookups.aspx
A script component could be the direction forward
JUst put two conditions on it mydatefield >= some date and mydatefield <= some date
Take a look at:
http://www.julian-kuiters.id.au/article.php/ssis-lookup-with-range
http://sqlblog.com/blogs/jamie_thomson/archive/2009/11/28/debunking-kimball-effective-dates.aspx
(The second link is a controversial argument for how [not] to respresent type 2 SCDs).
If you have the time and patience, writting a script component is by far the way to go from a performance perspective.