I have a SQL server table that has a list of Usernames and a list of points ranging from 1 to 10,000.
I want to display the number of points in my VB.NET program from the username given. For example, if I type in "john" in the first box the program will give me a message box saying whatever amount of points "john" has. I'm not really sure about SQL queries so please help me out here.
This is the Table Structure:
Usernames Points
-----------------------------
John 20
Kate 40
Dan 309
Smith 4958
Depending on the structure of the table, a suitable query is one of these:
select sum(points) as points
from usernames
where name='username';
or
select points
from usernames
where name='username';
or
select count(*) as points
from usernames
where name='username';
Related
I have a quick question regarding writing a SQL query to obtain a complete entry from two or more entries where the data is missing in different columns.
This is the example, suppose I have this table:
Client Id | Name | Email
1234 | John | (null)
1244 | (null) | john#example.com
Would it be possible to write a query that would return the following?
Client Id | Name | Email
1234 | John | john#example.com
I am finding this particularly hard because these are 2 entires in the same table.
I apologize if this is trivial, I am still studying SQL and learning, but I wasn't able to come up with a solution for this and I although I've tried looking online I couldn't phrase the question in the proper way, I suppose and I couldn't really find the answer I was after.
Many thanks in advance for the help!
Yes, but actually no.
It is possible to write a query that works with your example data.
But just under the assumption that the first part of the mail is always equal to the name.
SELECT clients.id,clients.name,bclients.email FROM clients
JOIN clients bclients ON upper(clients.name) = upper(substring(bclients.email from 0 for position('#' in bclients.email)));
db<>fiddle
Explanation:
We join the table onto itself, to get the information into one row.
For this we first search for the position of the '#' in the email, get the substring from the start (0) of the string for the amount of characters until we hit the # (result of positon).
To avoid case-problems the name and substring are cast to uppercase for comparsion.
(lowercase would work the same)
The design is flawed
How can a client have multiple ids and different kind of information about the same user at the same time?
I think you want to split the table between clients and users, so that a user can have multiple clients.
I recommend that you read information about database normalization as this provides you with necessary knowledge for successfull database design.
Can I do SQL-style query on an in-memory dataset (or cellarray, or structure, etc) in MATLAB?
Why I ask is, sometimes, I don't want to talk to the database for 1000 times when I want to do different operations on each of the 1000 rows of data. Instead, I'd rather read all 1000 from the database and operate on them in MATLAB.
For example, I have read the following out of from the database:
age first_name last_name income
30 Mike Smith 45
17 David Oxgon 17
22 Osama Lumbermaster 3
Now I want to find out the full names of the people that are under the age of 25. I know how to do it, but is there any syntax as clean and intuitive as SQL like this?
SELECT first_name + ' ' + last_name AS name FROM people WHERE age < income
In the docs page Access Data in a Table (see the example Index Using a Logical Expression) it shows that your examples could be achieved as follows:
MyTable({'first_name','last_name'}, MyTable.age < MyTable.income)
These docs don't specifically explain how to merge the name and surname into one variable but I'm sure it's easy. Give it a try and let us know if you get it.
I have an RDLC report, I have a column chart whose X axis is shows PerformedBy person names,
Y axis shows the count of exams grouped by each PerformedBy person.
The data in the dataset contains case sensitive data.
For example, there are two PerformedBy names like 'john' and 'JOHN'.
john has Number of exams as 1 where as JOHN has 2. Currently in the chart it displays only JOHN.
The data for both 'john' and 'JOHN' are combined and shown under 'JOHN' as 3.
I want to display john with Number of exams as 1 and JOHN with Number of exams as 2.
How to handle this at RDLC level?
I have seen in some of the dicussions that I can select Data Options and then choose casesensitivity property.
But I am not seeing this option in Dataset Properties dialog.It shows only the option General.
I am using Visual studio 2010 , .NET 4.0 and SQL Server 2008 Express.
Another option I was thinking to add a Unique identifier field for the Performed By person in the dataset. In the chart
how do I group by Identifier but then display Performed by Name in X axis?
Note that I saw both records are coming in my output collection from sql query.
One way is to convert the string to ASCII code, and then put the number together
For instance 'john' => '106111104110' and John=> '74111104110'. And then group on that value
This link will explain how to convert a string to an ASCII values.
http://p2p.wrox.com/beginning-vb-6/56056-converting-set-string-its-ascii-value.html
I have a tricky problem that I wouldn't mind a bit of help on, I've made some progress using queries that I've here and elsewhere, but am getting seriously stumped now.
I have a mailing list that has numerous near duplications that I'm trying to combine into one meaningful row, taking data such as this.
Title Forename Surname Address1 Postcode Phone Age Income Ownership Gas
Mrs D Andrews 122 Somewhere BH10 123456 66-70 Homeowner
Ms Diane Andrews 122 Somewhere BH10 123456 £25-40 EDF
and making one row along the lines of
Title Forename Surname Address1 Postcode Phone Age Income Ownership Gas
Mrs Diane Andrews 122 Somewhere BH10 123456 66-70 £25-40 Homeowner EDF
I have over 127 million records, most duplicated with a similar pattern, but no clear logic as was proven when I added an identity field. I also have over 90 columns to consider, so it's a bit of work!
There isn't a clear pattern to the data, so I'm thinking I may have a huge case statement to try to climb over.
Using the following code I can get a decent start on only returning the full name, but with the pattern of data - trying to compare the fields across rows is as follows.
SELECT c1.*
FROM
Mailing c1
JOIN
Mailingc2 ON c1.Telephone1 = c2.Telephone1 AND c1.surname = c2.surname
WHERE
len(c1.Forename) > len(c2.Forename)
AND c2.over_18 <> ''
AND c1.Telephone1 = '123456'
Has anyone got any pointers as to how I should progress please? I'm open to discussion and ideas...
I'm using SQL 2005 and apologies in advance if the tagging is all over the place!
Cheers,
Jon
Would it work by assuming that all persons with the same surname and phone number (Do all persons have a phone?) were the same person?
INSERT INTO newtable <fieldnames>
SELECT lastname,phone,max(field3),max(field4)....
FROM oldtable
GROUP BY lastname,phone
But that would collapse John Smith and Jack Smith living together into one person.
Perhaps you should consider outsourcing it to a data-entry sweatshop somewhere, adter you have preprocessed the data. :-)
And/or be prepared to take the flack for mistaken bundling.
Perhaps adding something like "To improve our green footprint, we have merged x listings on your adress together. If you would like separate mailings, please contact us"
Following join is supposed to retrieve user info along with their messages for users with a certain status:
SELECT * FROM user, message WHERE message.user_id=user.id AND user.status=1
The problem is that all rows about a certain user in the result set contain redundant columns that repeat the same data about that user (those fields retrieved from user table), only fields from the message table contain non-redundant information. Something like this:
user.id username email message.id subject
1 jane jane#gmail.com 120 Notification
1 jane jane#gmail.com 122 Re:Hello
1 jane jane#gmail.com 125 Quotation
2 john john#yahoo.com 127 Hi jane
2 john john#yahoo.com 128 Fix thiss
2 john john#yahoo.com 129 Ok
3 jim jim#msn.com 140 Re:Re:Quotation
As you can see many data are redundant and we do not want to first find the users and then go about their messages in loop like structures or something like that. Loops that cause micro-queries should be avoided at all costs.
I am not concerned about the output of my program, that is well handled in the UI. I think perhaps the network traffic produced by returning the result of this query could be substantially reduced if somehow I can manage to eliminate the repetition of user data in all rows pertaining to that user.
There are several things you should know.
The first is that the default SQL JOIN construct is essentially a set cross product, restricted by the WHERE clause. This means it's multiplicative - you get duplicate results out which you then prune down. You also have to be careful in the presence of NULL fields.
The second is that there is a 'DISTINCT' keyword. When you prefix a column in the selection with this, you'll get at most one instance of a certain value for that column in the results. So, as per your query, 'SELECT DISTINCT user.id FROM' will eliminate the redundancies on the server side.
The third is that the correct way to solve this problem is likely not using the * operator. I suggest:
SELECT user.id,username,email,subject FROM message m,user WHERE m.user_id=user.id AND user.status=1
This uses the simple, easy-to-understand implicit-join syntax and should be valid SQL on whatever server. I can vouch for it working with MySQL, at least. It also aliases the 'message' table to 'm' as shorthand.
As you surmise, this will reduce the traffic from the SQL server to your database.
edit: if you want to eliminate the "redundant" email information, you can't - you must make two distinct queries. SQL results are tables and must be rectangular, with all known values filled. There's no 'ditto' entry.
edit 2: You only have to make two queries. For instance:
SELECT subject FROM message WHERE message.id IN (SELECT user.id FROM user WHERE status=1)
This is one query that contains a nested query, so it's really making two database hits. But it doesn't have any programmatic loops.
In the straight sql query there is not if you keep them as a single query. If you are programmatically printing this out, then you would order by user id and only reprint that information if the user id changes.
In the SQL standard, you would use NATURAL JOIN; this joins on common column names and only preserves one copy of those common names.
In practice, you carefully list the columns you want, rather than resorting to the '*' shorthand notation.
Assuming you can use stored procedure, you could write one to run the above query and then use a cursor to store nulls for 'redundant information' to get something like
user.id username email message.id subject
1 jane jane#gmail.com 120 Notification
null null null 122 Re:Hello
null null null 125 Quotation
2 john john#yahoo.com 127 Hi jane
null null null 128 Fix thiss
null null null 129 Ok
3 jim jim#msn.com 140 Re:Re:Quotation
and then return this resultset in a temporary table. but while this may reduce network traffic, it will add a processing overhead
Another way is to run 2 queries, one to get the user information, and the other to get the message information with just the linked user id and then do the "join" using application server side code. something like
SELECT DISTINCT user.* FROM user, message WHERE message.user_id=user.id AND user.status=1
and
SELECT user.id, message.* FROM user, message WHERE message.user_id=user.id AND user.status=1
which will result in 2 trips to the database, instead of 1, which might eventually be slower, even if the network traffic is reduced.
And another way is to bunch these 2 into a single resultset with something like
SELECT user.* FROM user, message WHERE message.user_id=user.id AND user.status=1
UNION ALL
SELECT user.id, message.* FROM user, message WHERE message.user_id=user.id AND user.status=1
to get something like
user.id username/message.id email/subject
1 jane jane#gmail.com
2 john john#yahoo.com
3 jim jim#msn.com
1 120 Notification
1 122 Re:Hello
1 125 Quotation
2 127 Hi jane
2 128 Fix thiss
2 129 Ok
3 140 Re:Re:Quotation
and then use application server logic to separate it out. reduced network traffic but more application server load / marginally more database server load.
But the saved network traffic is rarely worth the added complexity.