SQL Server Table Display Uncertainty - sql-server

In my database in the datatable I found some uncertainty while viewing the table. Rows are not sorted according to the id. I saw some random behavior. I mean the entire table should be there according to the Id. But it is not.
The snap of the datatable is
Its not showing the entry as per the order it should be. I am selecting the top 11000 rows order by Id.
My query is
SELECT TOP 11000 [Id]
,[Date]
,[S11]
,[S12]
,[S13]
,[S14]
,[S15]
,[S21]
,[S22]
,[S23]
,[S24]
,[S25]
FROM [MCAS].[dbo].[MCASMonitoring_Rev1] ORDER By Id
Why it is showing like this ?

Related

Power Query to Filter a SQL view based on an Excel column list

Is there a way to filter a SQL view based on a list of values in an excel table column using Power Query?
I have a SQL view that returns a large set of data (millions of records or properties). Users want to filter that based on an excel table column of property IDs. I know I can just do a merge join based on the property ID between the view and the excel column in power query. But it looks like the merge brings in the millions of records first then filtered it in the join. Which takes a long time. Users want to change the list of propertyIDs on the fly on a daily basis and run the query.
Essentially, I wanted to create in Excel power query
what is in SQL
SELECT * FROM SQLViewName
WHERE PropertyID IN (Select Column from ExcelTable)
You should be able to do this with a List.Contains function.
If my ExcelTable is
ID
---
436
437
438
439
then adding a filter like this should do the trick:
Table.SelectRows(SQLViewName, each List.Contains(ExcelTable[ID], [PropertyID]))
When I tried this and did View Native Query on the last applied step, it folded the Excel table into a WHERE clause with the ExcelTable values as literals like this:
select [_].[PropertyID],
[_].[OtherColumns]
from [dbo].[SQLViewName] as [_]
where [_].[PropertyID] in (436, 437, 438, 439)
This allowed me to load a multi-million-row table in just a couple seconds.

Update row_number value in view joined to different table

I created a view in SQL Server that includes a row_number() function. The table referenced in the view contains every record in my database and enumerates records based on duplicate instances of a composite ID. For example:
row_number() over (
partition by
composite_id
order by
sample_value) as rownum
The issue is that whenever I join this view against another table (or filter rows based on a WHERE clause), the row number nevertheless always returns the value that would be returned for the full table referenced in the view. Instead, I'd like the row number to update depending on the records that are ultimately returned in the eventual result set.
For example:
select *
from my_created_view a
where a.sample_value in ('a','b','c')
or
select *
from my_created_view a
inner join subset_of_data b on a.sample_value = b.sample_value
...where either query above would result in a smaller number of records than are contained in the full original table and the resulting set of composite_id would sometimes contain only one instance. In cases where the result set contains only one instance of composite_id, I'd like that row to receive a value of 1.
Is this possible? Or does row numbering within a view create a row number that's tied only to the query within the created view?
Thanks in advance for any light you can shed here!

SQL SERVER - Retrieve Last Entered Data

I've searched for long time for getting last entered data in a table. But I got same answer.
SELECT TOP 1 CustomerName FROM Customers
ORDER BY CustomerID DESC;
My scenario is, how to get last data if that Customers table is having CustomerName column only? No other columns such as ID or createdDate I entered four names in following order.
James
Arun
Suresh
Bryen
Now I want to select last entered CustomerName, i.e., Bryen. How can I get it..?
If the table is not properly designed (IDENTITY, TIMESTAMP, identifier generated using SEQUENCE etc.), INSERT order is not kept by SQL Server. So, "last" record is meaningless without some criteria to use for ordering.
One possible workaround is if, by chance, records in this table are linked to some other table records (FKs, 1:1 or 1:n connection) and that table has a timestamp or something similar and you can deduct insertion order.
More details about "ordering without criteria" can be found here and here.
; with cte_new as (
select *,row_number() over(order by(select 1000)) as new from tablename
)
select * from cte_new where new=4

How to show data in column in SSRS

I'm using SSRS for my reporting, my reporting solution is in Visual Studio 2008 Business Intelligence Development Studio.
I have a report in which the data should be displayed in this format.
I have added a Column Group in my table which is having the values of Customer Name and details, the data is coming fine in the vertical format i.e column after column.
My Issue :
There should be only three columns in each row, after three records the next row should begin and again not more than three records should be displayed as shown in the image above.
My attempts :
I tried to add a row group and in that gave the expression
= Ceiling(Fields!Row_Count.Value/3)
here Row_Count is a field which is coming from my query which holds the serial number of the records.
My SQl Query
SELECT Row_Number() over(order by table_ID) AS Row_Count, Field_1,Field_2 from MyTable
In my Column group i have Customer Name and in my Row Group i have other details of the customer. The data is getting populated column wise but the issue is its not breaking the current row after three records.
Below is my table of report.
You were on the right track. Say you have data like this:
I have created a tablix like this:
The Row Group expression is:
=Ceiling(Fields!Row_Count.Value / 3)
This works together with the Column Group expression to split over three columns:
=(Fields!Row_Count.Value - 1) Mod 3
The other thing to note compared to your tablix is that CustomerName is not in a table header row, but rather there are two row header rows, one for CustomerName and one for Details.
This is looking OK to me, obviously you can format to taste:

How do I Iterate through a small set of records and retrieve records that match criteria set in each record

I have a table tblCriteria that contains a small (<20) set of records. Each record has a field of criteria.
I want SQL to move through these records when requested tblFilterRun, filter the main table tblRecords (~5000 records) and then insert some key fields from the matching records into another table tblFilterResults.
tblCriteria (CriteriaID, CriteriaText)
tblFilterRun (FilterRunID, FilterRunDate)
tblFilterResults (FilterResultsID, FilterRunID, RecordID, Ref, CustomerID, SupplierID
tblRecords (RecordID, CustomerID, SupplierID...)
Previously I would have created something in Access to iterate through each tblCriteria record, but I would like a purely server solution. I've heard cursors mentioned (usually at the same time as a profanity), what are my options?
It's not really clear what you need to do with the records in tblCriteria, but can you created a UDF that would do the work of processing one record? Then you can call it on every record using one query like
SELECT *
FROM tblCriteria
CROSS APPLY dbo.udf_yourFunction(parameter1, parameter2, etc)

Resources