Select Columns only if greater than 1 - sql-server

I have about 70 total columns that will have either a 1 or a 0 in the column. I'm trying to only Select the columns that have more than 0. What's the best way to do that please? Thank you.

I'm not sure what you're trying to accomplish, but SQL really doesn't have a good mechanism for selecting columns based on their value. However, if you convert the columns to rows using PIVOT then you can use a basic WHERE clause to filter the rows.
https://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx

Related

Aggregating rows across in SSRS

Sorry for the really noob SSRS question here. I am having trouble aggregating across my rows for my totals. Whenever I select my 3 columns that I need to aggregate (Rent,Exp,Other) = Total - It seems to sum down the instead of across.
I want to group my Total so I do not have 6 Rows of this Person with 6 different totals. I would just like 1 Grand total of the sums. Can someone point out what I am doing wrong here?
I would love this to be one consistent total for each unique Unit#
Thank you!
It looks like you are grouping by Unit which would be correct but also by something else (Base Rent?).
I think you have an extra Row Group that you need to delete. Just delete the inside group, not the related rows and columns.
If that's not the issue, post a pic of your report in design view.

Comparing two rows in SQL Server

Scenario
A very large size query returns a lot of fields from multiple joined tables.
Some records seem to be duplicated.
You accomplish some checks, some grouping. You focus on a couple of records for further investigation.
Still, there are too much fields to check each value.
Question
Is there any built-in function that compares two records, returning TRUE if the records match, otherwise FALSE and the set of not matching fields?
The CHECKSUM function should help identify matching rows
SELECT CHECKSUM(*) FROM table
May be this is what you are looking for:
SELECT * FROM YourTable
GROUP BY <<ColumnList>>
HAVING COUNT(*) > 1
Just developing on the suggestion provide by Podiluska to find the records which are duplicates
SELECT CHECKSUM(*)
FROM YourTable
GROUP BY CHECKSUM(*)
HAVING COUNT(*) > 1
I would suggest that use the hashbytes function to compare rows.It is better than checksum.
What about creating a row_number and parttion by all the columns and then select all the rows which are having the rn as 2 and above? This is not slow method as well as it will give you perfect data and will give the full row's data which is being duplicated.I would go with this method instead of relying on all the hashing techniques..

SQL Server Select Query

I have to write a query to get the following data as result.
I have four columns in my database. ID is not null, all others can have null values.
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_PHONE
1 John Williams +123456789
2 Rodney +124568937
3 Jackson +124578963
4 Joyce Nancy
Now I have to write a query which returns the columns which are not null.
I do not want to specify the column name in my query.
I mean, I want to use SELECT * FROM TABLE WHERE - and add the filter, but I do not want to specify the column name after the WHERE clause.
This question may be foolish but correct me wherever necessary. I'm new to SQL and working on a project with c# and sql.
Why I do not want to use the column name because, I have more than 250 columns and 1500 rows. Now if I select any row, at least one column will have null value. I want to select the row, but the column which has null values for that particular row should not appear in the result.
Please advice. Thank you in advance.
Regards,
Vinay S
Every row returned from a SQL query must contain exactly the same columns as the other rows in the set. There is no way to select only those columns which do not return null unless all of the results in the set have the same null columns and you specify that in your select clause (not your where clause).
To Anders Abels's comment on your question, you could avoid a good deal of the query complexity by separating your data into tables which serve common purposes (called normalizing).
For example, you could put names in one table (Employee_ID, First_Name, Last_Name, Middle_Name, Title), places in another (Address_ID, Address_Name, Street, City, State), relationships in another, then tiny 2-4 column tables which link them all together. Structuring your data this way avoids duplication of individual facts, like, "who is John Williams's supervisor and how do I contact that person."
Your question reads:
I want to get all the columns that don't have a null value.
And at the same time:
But I don't want to specify column names in the WHERE clause.
These are conflicting goals. Your only option is to use the sys.tables and sys.columns DMVs to build a series of dynamic SQL statements. In the end, this is going to be more work that just writing one query by hand the first time.
You can do this with a dynamic PIVOT / UNPIVOT approach, assuming your version of SQL Server supports it (you'll need SQL Server 2005 or better), which would be based on the concepts found in these links:
Dynamic Pivot
PIVOT / UNPIVOT
Effectively, you'll select a row, transform your columns into rows in a pivot table, filter out the NULL entries, and then unpivot it back into a single row. It's going to be ugly and complex code, though.

insert values from one table into another table with different design in sql server

I'm back with another (possibly) silly question. sorry.
I have a pretty complicated query which joins 4 tables and computes the sum of a column based on the other two columns in two tables. the result returned is like this:
Image http://eternalvinay.iocleicester.com/blahblah.png
Now, I want the results to be like the right hand side of the image. the number rows per month/year might change though its 4 for now.
I am creating a temporary table as:
Declare #TmpTable (id int identity, AnsSum float, AnsMonth int, AnsYear int)
to store the values from image --> table1. However, I cant figure out how to convert those rows into the format required by table 2.
So, Any hints on this please?
Thanks so much..
ps: I tried to google and related questions here, no luck.
pss: I am not expecting the exact answer too, i am quite interested to learn new things so if you know where i can learn to do this, a push in the right direction, that would be great too!
You could use cross apply to get all the values in a comma delimted format in a single column. instead of "4" different columns. The problem is this "4" cannot be defined everytime. it may increase or decrease and it is not advisable to have this as columns.
SELECT DISTINCT AnaMonth, anayear, [DerivedColumn] FROM #TmpTable A
CROSS APPLY
(
SELECT AnaSum + ',' FROM #TmpTable B
WHERE A.AnaMonth = B.AnaMonth
AND A.AnaYear = B.AnaYear
FOR XML PATH('')
) AS C (DerivedColumn)
You will get [6.0000, 1.000, 8.0000, 5.0000] in one column for month 5 and year 2010 etc ... You could use this as a table to query for any particular month.
Hope this helps
So you have normalized data and you want to pivot the result set to create repeating groups.
You could use PIVOT but you'd need some other attribute in your base table to define the four columns.
I would recommend do not pivot this query in SQL. Just do the query against the database and get four rows per month/year. Then write code in your application to aggregate the results by month/year.

SSRS: repeat tablix left-most row group value on each row

I have a tablix in an SSRS 2008 report. It has two-level row groupings, and I'd like the value for the left-most grouping to continue to be displayed on each row. Eg, I get this:
group1 subgroup1 500.00
subgroup2 250.00
... but I'd prefer...
group1 subgroup1 500.00
group1 subgroup2 500.00
I can't seem to find the option for this. Is it a strange thing to want?
Thank you,
Bill
You can simply edit the upper reporting level, to be grouping on both groups at the same time, this will cause your desired behaviour.
I had to resort to this solution once, where "hide duplicates" didn't seem to achieve the desired result.
It's so simple...you have to right click on the cell in the DATA area (ex. where is displayed the value 500) and to click add COLUMN on the left. After this change the value in the new cell with the label field (subgorup1), also do this for the others columns that you want to add, but remember to do it from the DATA area. when you finish, delete the columns out of the data area, but keep the row groups. nice try!
Currently, what I've found to be a good way of determining whether or not the repeated values of a group would show up in a SSRS report, is to create the Groups and build your report based on the groupings. Secondly, take the groups fields and duplicate the column to the right hand side of the report. Making sure that you've included all the columns needed and totals are correct; delete the grouped columns. When you do this, you will get a popup asking if you want to delete the group or just the columns, chose columns - this will keep the format of the initial report and keep all of the total line as well.
If you select the row in question, there is a field called Hide Duplicates. Set this to nothing (null, space, etc) That should return the values you want.
I had this same issue and I solved it really easily.
It seems you have two row groups, by group and by subgroup.
What you have to do is to select the first column and group by group and by subgroup and remove the grouping by subgroup.
That should do the trick.
Put ROW_NUMBER() OVER (ORDER BY name ASC) AS Row# in each query as a serial number and hide the row in the SSRS Report.
See: https://learn.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver15

Resources