I have two tables in SQL Server DB. I have a dropdownmenu on my c# page where staff should be able to choose the relevant plan and it should show all users who do not have a current plan setup
Table 1 contains all users
Table 2 only contains people who have a plan. They also can have different plan_id's in table 2 from 1-10.
I need a SQL join which returns all people who do not have a plan at all or the plan id does not match the one selected from the dropdownlist
E.G a user could exist on table 2 with 3 entries with plan_id's 2,3 and 4.
E.G If a user selects plan 7 from the downdownlist, the user would appear in the list as they currently do not have a plan matching the id selected.
Table 2 plans also have an expiry_date and "active" field.
The plan should also not be valid if the expiry date is in the past or active is "0"
Write a not in Query, Where user does not exist in Table 2 with that plan id.
Suppose
Table_1 -> Userid, Username
Table_2 -> UserId, PlanId
SELECT UserId, Username FROM Table_1
WHERE UserId NOT IN ( SELECT UserId FROM Table_2 WHERE PlanId = 1 )
This query will return all the users who are not linked with Plan 1.
Related
I am trying to create a query to help get a list of accounts from an existing database. I will have two lists of integers passed in through two Table Value Parameters(TVP) from C#. I then need to see if multiple columns have any of the values in the corresponding TVP tables. The TVP lists of integers are provided by different clients and may differ between clients. That is why they are TVP's to allow the values to be passed in as parameters.
The data structure cannot be changed, it is created based on data from another system. Comments about changing the data structure won't be helpful. To help I will talk about an example table that would help show what I need.
Looking at a table like the following:
Table Accounts
varchar(200) AccountId
int StatusId1
int StatusId2
int StatusId3
int StatusId4
int Identifier1
int Identifier2
int Identifier3
int Identifier4
int Identifier5
I know that I can do a sql statement like:
Select AccountId from Accounts where StatusId1 In (1,2,3)
I was able to learn that I can reverse the In command as well:
Select AccountId from Accounts where 1 In (StatusId1, StatusId2, StatusId3, StatusId4)
This only lets me check one value against each column. The problem is I need to mix the two while using the TVP for the list of integers.
The closest I have been able to create is the following:
--Load the TVP lists
SELECT * INTO #StatusCodes FROM #StatusId
SELECT * INTO #IdentityCodes FROM #IdentifierId
--Find the Accounts that have the chosen Ids
SELECT AccountId
FROM Accounts
WHERE StatusId1 IN( SELECT Id FROM #StatusCodes)
OR StatusId2 IN( SELECT Id FROM #StatusCodes)
OR StatusId3 IN( SELECT Id FROM #StatusCodes)
OR StatusId4 IN( SELECT Id FROM #StatusCodes)
OR Identifier1 IN (SELECT Id FROM #IdentityCodes)
OR Identifier2 IN (SELECT Id FROM #IdentityCodes)
OR Identifier3 IN (SELECT Id FROM #IdentityCodes)
OR Identifier4 IN (SELECT Id FROM #IdentityCodes)
OR Identifier5 IN (SELECT Id FROM #IdentityCodes)
This query worked in my prototype and I got back the list of accounts that had at least one of these ids. I see a lot of select statements and it doesn't look very good. I am not sure how well it performs either. I am wondering if there is a better way to do this?
This is for a system that creates a report based on conditions our clients make. Each client runs from a couple to 100 reports each night. That means this is run possibly hundreds of times each night. While it isn't a system running thousands of times per hour, it does process a lot of data. Some of the databases it will search will be big with lots of accounts to search.
One option uses exists:
select a.acountId
from accounts a
where
exists (
select 1
from #StatusCodes s
where s.id in (a.StatusId1, a.StatusId2, a.StatusId3, a.StatusId4)
)
or exists (
select 1
from #IdentityCodes i
where i.id in (a.Identifier1, a.Identifier2, a.Identifier3, a.Identifier4)
)
Guys. I need an advice from you.
I created project MVC 5 and there user is importing excel file to (sql table) through interface on page. To give other users information about statuses.
In excel file 3 column ( in my model same columns )
Id NoAccount Status
User which uploading every week gets an excel file and importing file(s) using interface on page.
In excel file same columns may contain same data or newer data (with different statuses each week)
For example on 1st week user importing excel file with following data
Id NoAccount Status
1 A12345 0
On 2nd week user importing excel file with the next data.
Id NoAccount Status
1 A12345 1
For that moment, i will have, in my sql table 2 rows
And in a page also will be 2 rows and for preventing to confuse user should see only one row with status 1 (or 2,3 if it was )
If you want to keep multiple rows per account in your database and you want to see the highest value of status for each account, you can get the data using SQL like this
select NoAccount, max(Status)
from table
group by NoAccount
You mention T-SQL. You can use GROUP BY or SELECT DISTINCT constructs depending on the other fields that you want to include. So, for example:
SELECT DISTINCT
Id, NoAccount, Status
FROM
MyTable
or
SELECT
Id, NoAccount, Status, COUNT(Id)
FROM
MyTable
GROUP BY
Id, NoAccount, Status
would give you the distinct values together with the count of duplicates.
How to show or retrieve multiple columns with distinct value from one table in sql query ?eg: in my database i have account id 13 and id 13 have balance of 8000 and remaining fields are different but id and balance is same in 9 records, so i want id and balance to display only once and for remaining fields it should display 9 records
Try like this;
SELECT DISTINCT c1,c2,c3 FROM table
Or you can use group by
SELECT c1,c2,c3 FROM table group by c1,c2,c3
I am new to stack and c#. Here I created a c# windows form application. I am using SQL server with login and student tables. I store login details(UN,PW) in login table and general details of student(fname,lname,etc..) in student table.
What I want to get is First name of the student using his login details.
ex- UN:- ravi, PW:- 123, fname:- Ravindu
But in my system, ALL the registered students are not users of the system and all the users are registered students
Should I link tables or any structured way to do this.
You can use an outer or left join to represent everything in one table and only the matched items in the other.
select
*
FROM
TABLEALL
LEFT JOIN
TABLESOME
ON
TABLEALL.ID = TABLESOME.ID
Yes, you need to link both tables via a ID. When all users are registered students, they should have data in Student table. If you login as Ravi as stated in your example, this user should be present in Student Table. You can create a loginID as Primary key in login table. This should be linked to Student table via a Foreign key loginID in Student table and you should be able to achieve what you are looking for.
select st.fname from login lin left join on student st lin.loginID =st.loginID where lin.loginID='ID of student';
Since you are working with student table, i assume that every student has unique index number, so you could add that unique Index column in student table and in Index column your login table, since all of your system users are students...
SELECT studentName FROM studentTable
WHERE Index IN
(
SELECT Index FROM loginTable
WHERE loginTable.userName = username
AND loginTable.password = password
)
I am querying my table with the following query;
SELECT DISTINCT [RoleId], [RoleName], [RoleDescription], [PermissionId]
FROM [Role]
ORDER BY [RoleName]
And I get the following results;
RoleId RoleName RoleDescription PermissionId
1 Admin CanEdit 1
2 Admin asdf;lkj 2
3 Admin al;dskfj;l 3
4 Admin fa;ldkfjas;d 4
17 SuperAdmin aslkdfja; 1
18 SuperAdmin asldkfa;f 2
15 Users alskdfj;alk 1
16 Users aslkdfja;sl 2
However, I want to get the unique values of RoleName i-e RoleName shouldn't be repeated which is already in my query results. I want to get the RoleName once.
How do I get unique RoleName?
Updated:
I want to populate the combobox with the following results;
Admin
SuperAdmin
Users
but not not repeating (which I get already using my query
Admin
Admin
Admin
Admin
SuperAdmin
SuperAdmin
Users
Users
If you want to have only Distinct RoleName then you can query the table like this.
SELECT DISTINCT [RoleName]
FROM [Role]
ORDER BY [RoleName]
The answer you are watching is little bit complicated as you are going. if you want such type of result then you might have to change your process to "add Roles". While you are inserting Roles from front end then there you have to check whether the rolename is being entering doesn't exists in database already. Then you can easily go the result you are seeing for with the following Query.
Select Distinct rolename,roleid from tblroles
You say you want RoleId as well as RoleName. Well, you have to tell the system which RoleId to pick:
SELECT MIN(RoleId) as RoleId, RoleName
FROM Role
GROUP BY RoleName
ORDER BY RoleName
In this case, I've chosen somewhat arbitrarily to use the lowest RoleId for each name. If there's a different rule you want to use, then you a) need to tell us what that rule is, and/or b) need to tell SQL Server what that rule is.