Multiple query results - database

I am currently running a query in MS Access where i am searching for matching fields, the only problem is is that it is returning multiple fields which are all identical, is there anyway of limiting the results if the are identical?

What you need is DISTINCT.
Try something like this.
SELECT DISTINCT
table.*
FROM table

Related

RowMapper returns different column Ordering

I am implementing a row mapper to format the results from a specific query. When I query my DB through PG admin, or just using a simple jdbctemplate.queryforlist the columns return in one order but when running using my RowMapper it returns in a completely different order that is in no way related to how i run the other queries (example query: select * from blah.mytable). Any ideas why a RowMapper would cause the columns to return in a different order?
Thanks,
Brian
The issue is that I was using a HashMap instead of a LinkedHasMap when I returned my results.
Thanks,
Brian

SQL query getting data from 2 different database objects

I am very much a beginner at more in depth SQL queries other then simple queries needed to more manage the server side then the database side so any help here is greatly appreciated.
I have a a database for a ticketing system where I need to take data from two different objects and combine the results into the results.
Looks something like this:
+Prof1
- Columns
*AssignedTech (tech assigned task)
*Matters (unique matter ID)
*Type (open, closed, development etc.)
+Matters
- Columns
*MatterNumber
What I am trying to do is get the Matter Number into the query like below but don't know other then perhaps dumping the results to Excel and filtering them from there to get that data into this query. Matters in the DBO corresponds to the unique Matter ID noted above. If i could even run the query below and then use those results to query against the Matters DBO to get the matter ID along with it.
select *
from AssignedTech
where Type like 'open%'
order by Matters
I Believe you want to do is a SQL Join! (I suppose that the Column Matters at Prof1 Table is a reference to the MatterNumber at Matters Table)...
So, to do the join, you must write a query like this:
SELECT A.*, B.MatterNumber
FROM Prof1 A, Matters B
WHERE A.Matters = B.MatterNumber AND A.Type like 'open%'
ORDER BY A.Matters;
Hope this help (also, that I understand you correctly)
Mureinik, I wish I could post a screenshot but hopefully this helps. The section of the DB Basically looks something like this
Example

MSSQL Query should return same as MYSQL

I've MYSQL Query and working fine
The QUERY IS:
select tst_type, count(tst_type) tot from tst_type where project='JupiQA';
The above Query returns single record If I'm adding GROUP BY tst_type in this query it returns multiple values.
The Query I tried in MSSQL without GROUP BY tst_type ,its showing Error
ERROR IS: Column 'tst_type.tst_type' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
After that added GROUP BY tst_type in MSSQL query,then working fine and returns multiple value.
But my requirement is it should return same as MYSQL without adding GROUP BY fn OR should return single value like MYSQL
MySQL allows this behavior, but I don't know of any other DBMS that lets it happen. If you want MySQL and MSSQL queries to behave the same, add the SQL Mode ONLY_FULL_GROUP_BY. MySQL will now throw the error properly.
Also check out this SO post:
Why does MySQL allow "group by" queries WITHOUT aggregate functions?
When doing a COUNT() as you're doing here, the GROUP BY is required to get the correct results. See the MySQL docs on the function
You can use a count(*) without any other fields to get a count of the total number of records. Otherwise, you must use GROUP BY.
IMHO your requirement for this query is backwards; aggregate functions require GROUP BY to behave properly/consistently when non-aggregate fields are included in the select. MSSQL is behaving properly, MySQL is not.

How can I stop MiniProfiler showing "duplicate" SQL query warnings parameters are different?

I'm using MiniProfiler to check what NPoco is doing with SQL Server but I've noticed it reports duplicate queries even when the SQL parameters have different values.
Eg: if I fetch a string from a database by ID, I might call:
SELECT * FROM PageContent WHERE ID=#ID
...twice on the same page, with two different IDs, but MiniProfiler reports this as a duplicate query even though the results will obviously be different each time.
Is there any way I can get MiniProfiler to consider the SQL parameter values so it doesn't think these queries are duplicated? I'm not sure if this problem is part of MiniProfiler or if it's a problem in how NPoco reports it's actions to MiniProfiler so I'll tag both.
I think that this is by design, and is in fact one of the reasons for the existence of the duplicate query detection.
If you are running that query twice on one page where the only difference is the param value, then you could also run it one time and include both param values in that query.
SELECT * FROM PageContent WHERE ID in (#ID1, #ID2)
So you are doing with two queries what you could do with one (you would have to of course filter on server side, but that is faster than two queries).
The duplicate query label is not for saying that you are running the absolute identical query more than once (though it would apply there as well). Rather it is highlighting an opportunity for optimizing your query approach and consolidating different queries into one (think about what an N+1 situation would look like).
If the default functionality doesn't meet your needs, you can always change it! The functionality that calculates duplicateTimings is located in UI/includes.js. You can provide your own version of this file that defines duplicates in a different way (perhaps by looking at parameter values in addition to the command text when detecting duplicates) by turning on CustomUITemplates inside MiniProfiler, and putting your own version of includes.js in there.

SQL server search

I'm going to perform a search in my SQL server DB (ASP.NET, VS2010,C#), user types a phrase and I should search this phrase in several fields, how is it possible? do we have functions such as CONTAINS() in SQL server? can I perform my search using normal queries or I should work in my queries using C# functions?
for instance I have 3 fields in my table which can contain user search phrase, is it OK to write following sql command? (for instance user search phrase is GAME)
select * from myTable where columnA='GAME' or columnB='GAME' or columnC='GAME
I have used AND between different conditions, but can I use OR? how can I search inside my table fields? if one of my fields contains the phrase GAME, how can I find it? columnA='GAME' finds only those fields that are exactly 'GAME', is it right?
I'm a bit confused about my search approach, please help me, thanks guys
OR works fine if you want at least one of the conditions to be true.
If you want to search inside your text strings you can use LIKE
select * from myTable where columnA like '%GAME%' or columnB like '%GAME%' or columnC like '%GAME%'
Note that % is the wildcard.
If you want to find everything that begins with 'GAME' you type LIKE 'GAME%', if you allow 'GAME' to be in the middle you need % in both ends.
You can use LIKE instead of equals and then it can contain wildcard characters, so your example could be:
select * from myTable where columnA LIKE '%GAME%' or columnB LIKE '%GAME%' or columnC LIKE '%GAME%'
Further information may be found in MSDN
This is going to do some pretty heavy lifting in terms of what the database has to do though - I would suggest you consider something like full text search as I think it would more likely be suited to your scenario and provide faster results (of course, if you never have many records to search LIKE would probably do fine). Information on this is also in MSDN
Don't use LIKE, as suggested by other answers. It won't work with indexes, and therefore will be slow to return and expensive to run. Instead, you have two options:
Option 1: Full-Text Indexes
do we have functions such as CONTAINS() in SQL server?
Yes! You can use the CONTAINS() function in sql server. You just have to set up a full-text index for each of the columns you need to search on.
Option 2: Lucene.Net
Lucene.Net is a popular client-side library for searching text data that integrates closely with Sql Server. You can use it to make implementing your search a little easier.

Resources