Sql query to add comparison columns dynamically - sql

My table looks like
Fields Jack Mike Bruce ... Tony
Salary 150 300 125 ... 150
CTC 100 100 250 ... 500
Here Jack is the base user and I need to compare the salary and CTC of Mike , Bruce ,Tony upto n columnof users in the table and add comparison rating columns such as the output looks like,
Fields Jack Mike Mike_rating Bruce Bruce_rating ... Tony
Salary 150 300 high 125 low ... 150
CTC 100 100 equal 250 high ... 500
Output Explanation
The user list grows dynamically and corresponding rating column needed to be added.
Jack Salary is 150 and Mike is 300 . So Mike_Rating column should have value as high else low else if two values are equal then equal
Any Help would be appreciated . Thank you

Create a trigger on table user. Trigger is used exactly for things like this. So, for example, if u create a trigger on table Users to be activated every time after insert, then u can get the inserted data, and using it you can make changes to your table. You would also probably want to create triggers for delete and update operations on User table. You can find details on how to create a trigger here

Related

Delete table column content

I have the following table:
car
score
description
Opel
30
43
Volvo
500
434
Kia
50
3
Toyota
4
4
Mazda
5000
4
How I can delete all the content of table column score without changing the table structure?
Expected result:
car
score
description
Opel
43
Volvo
434
Kia
3
Toyota
4
Mazda
4
As pointed out by Bergi, you have the option of setting all values in the column to NULL or 0, depending on what you need, or you can delete the entire column.
Solution 1:
UPDATE cars SET score = NULL;
or
UPDATE cars SET score = 0;
This will preserve the score column but set all the values to NULL or 0 respectively. Note that NULL and 0 are different things. NULL means the field is empty but 0 means the field has the numerical value 0.
If you don't need the score column anymore, you can delete it like this:
ALTER TABLE cars
DROP COLUMN score;
This will delete the column score and you will not be able to use it anymore.
I think the answer by gowner is ok.
However in case you have no permission to alter table structure, you cannot delete column.
And given the score field is not nullable,
you cannot update the field to null.
You must be careful that updating the score to 0 may not be ideal.
0 may have different meaning in your table. Maybe minimum score is 1 and 0 is not a possible value in the field. Or a consensus in your organization that -1 means "no value". They should be relfected in the default constraint or guidelines of your organization.
I would prefer to be safe
UPDATE cars SET score = DEFAULT;

Select Distinct using JET and no PKEY - Duplicate rows

There seems to be a lot of threads on this topic but few that work with Excel.
I have a simple table from which i want to select:
ideally all columns i.e. using * if possible so if a user adds new columns they do not need to edit SQL. (is this a pipe dream?) if so a solution specifying all the returned columns is OK.
only return rows where [name]&[date] (concatenated) is distinct
all other columns i don't care about which row is returned. first, last, limit 1... anything. they are a mix of all types.
this must not create a new table or delete rows, just selecting and joining
name date sales
andy 01/01/2010 100
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/02/2010 200
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 09/09/2010 300
dave 01/09/2010 300
Also code simplicity is prefered over speed. This is going to be left to run over night so nice looking but slow is fine... and excel doesn't have millions of rows!
Many thanks to everyone in advance.
UPDATE
I would expect the table to look like this:
name date sales
andy 01/01/2010 100
andy 05/01/2010 100
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 01/09/2010 300
or
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/....
I can select all the 'unique things with this:
SELECT MAX(joined)
FROM
(SELECT [Single$].[date] AS [date],
[Single$].[name] AS [name],
name & date AS [joined]
FROM [Single$]
)
GROUP BY joined
HAVING MAX(joined) IS NOT NULL
But i don't know how to somehow join this back to the original table keeping any single row where the join matches. And i don't know if a join is the right way about this? Thanks
Simply run an aggregate query grouped by [Name] and [Date]. For all other columns run an aggregate like MAX() or MIN() which should work on numeric and string values.
SELECT [Single$].[name] AS [name], [Single$].[date] AS [date],
MAX([Single$].[sales]) As [sales],
MAX(...)
FROM [Single$]
GROUP BY [Single$].[name] AS [name], [Single$].[date] AS [date]

Counting number of occurences of tuples in an m:n relationship

I'd like to know if there's an efficient way to count the number of occurences of a permutation of entities from one side of the m:n relationship. Hopefully, the next example will illustrate properly what I mean:
Let's imagine a base with people and events of some sort. People can organize multiple events and events can be organized by more than one person. What i'd like to count is whether a certain tuple of people have already organized an event or if it's their first time. My first idea to do this is to add an attribute to the m:n relationship
PeopleID | EventID | TimesOrganized
100 1 1
200 1 1
300 2 1
400 3 1
Now, there's an event no. 4 that's again organized by persons 200 and 100 (let's say they should be added in that order). The new table should look like:
PeopleID | EventID | TimesOrganized
100 1 2
200 1 2
300 2 1
400 3 1
200 4 2
100 4 2
Now, if I added an event organized by persons 200 and 300 it would look like this:
PeopleID | EventID | TimesOrganized
100 1 2
200 1 2
300 2 1
400 3 1
200 4 2
100 4 2
200 5 1
300 5 1
How would I go about keeping the third column updated properly and what are my options?
I should also add that this a part of the larger project we have for one of the classes and we'll be implementing an application that uses the database in some way, so I might as well move this to application logic if there's no easy way.
I wouldn't recommend tracking a TimesOrganized column as you suggest.
You can simple query it as needed using a COUNT(EventId)..GROUP BY PeopleID.
If you do feel you need to maintain the value somewhere it probably is better normalized to the (presumed) table People. Something like People.TimesOrganized. But then you have to increment it as you go instead of just recalculating as needed.
If you want to count how many many time someone have organized an event the problem is not m:n, but 1:m. Just count the event grouped by the people, that's it, you don't really need to have that column in the table, if it's not needed a lot of time.
That said I find you table a little confusing, there are detail and aggregation mixed, the third one downright wrong: the PeopleID 200 had organized 3 event and the 300 have 2 event.

I need to get the data from the multiple tables and I want to display that in the report form without repeating the primary key values

empID Hobbies Salary
1 Cricket 100
1 Walleyball 100
1 Golf 100
2 Cricket 200
2 Golf 200
I need to get the data from the multiple tables and I want to display that in the report form without repeating the primary key values.. in the above table it should not display empID for every hobby and I need to have the total of salary at the end of the report.
How to overcome such problem
If I understand what you are asking, this is not something you do in SQl, it is something you do in grouping in the report builder.
You are looking for something like this?:
empID Hobbies Salary
1 Cricket
Walleyball
Golf 300
2 Cricket
Golf 400
In five rows like that, or two rows like this:
empID Hobbies Salary
1 Cricket, Walleyball, Golf 300
2 Cricket, Golf 400
And which database system?
SELECT `empID`, SUM(`Salary`) AS `total_salary` FROM yourtable GROUP BY `empID`;
http://www.sql-tutorial.com/sql-group-by-sql-tutorial/
http://www.sql-tutorial.com/sql-aggregate-functions-sql-tutorial/

Sort Excel Grouped Rows

I have a spreadsheet that has information in groups. The header row contain company names and information and then the grouped rows beneath them contain names of people in the company.
Company Name | Number of Employees | Revenue |
Employee Name | Email | Phone
Is there anyway to sort by the number of employees and/or revenue and keep the grouped employee information below the company with the information?
Normally when I try it, it will sort the company information but keep the employee information in the order that it is entered.
If I understand your question correctly, I have a way you can accomplish what you want (don't know if there is a more efficient method).
Write code which will, for each company header row, copy the number of employess and revenue data into two of the chosen unused columns. The data needs to be copied into the columns for both the header company row and detail employee rows.
In the third column assign a sequence number. This is to keep data together and in order when sorting by employee/revenue.
Now you can sort by either the newly created number of employees and/or revenue columns (along with the sequence column to maintain ordering within company).
After the sort you can delete the extra copied data rows.
So if your data looked like this to start with...
A B C
Penetrode 200 750000
Micheal Bolton mbolton#pene.com 555-555-3333
Samir N samirn#pene.com
Initech 500 500000
Bill Lumbergh umumyeah#init.com 555-555-1212
Peter Gibbons pgibbons#init.com 555-555-2222
Your code would then copy the employee count and revenue data and sequencify the rows using three unused columns.
A B C D E F
Penetrode 200 750000 200 750000 1
Micheal Bolton mbolton#pene.com 555-555-3333 200 750000 2
Samir N samirn#pene.com 555-555-3334 200 750000 3
Initech 500 500000 500 500000 4
Bill Lumbergh umumyeah#init.com 555-555-1212 500 500000 5
Peter Gibbons pgibbons#init.com 555-555-2222 500 500000 6
Then you can code a sort on any of the column combos: (D,F), (E,F), (D,E,F), or (E,D,F)
Better late than never, I suppose, but I feel my LAselect plugin would have solved your problem. I created this plugin because I do much non-standard 'stuff' with my data and needed a tool to handle it. LAselect can produce your 'group' output too and you would not need hidden columns or anything. I mean, you would not need to change the screens you are used to to sort them in whatever way you wanted.