Please see below table structure and output required (struggling to right this)
Table: Employees
Structure
EmpID Manager Account Manager
1 Conrad Craig
2 Rob
3 Holly Luke
Output Required (If manager is not null and account manager is not null then Count is 1+1)
EmpdId Count
1 2
2 1
3 2
I'm struggling to write query which can give me above output
So we are checking of null for manager and account manager. And if they are not null then assign value 1 to them and add them up.
Regards
Hi you could do like this:
SELECT EmpId
CASE WHEN Manager IS NOT NULL AND Account Manager IS NULL THEN 1 ELSE 2 END AS Count
FROM TBL
Related
I admit the title question is convoluted; here is the situation.
I have two tables:
USERS Table
id
name
status
1
Monica
A
2
Ross
A
3
Phoebe
T
4
Chandler
A
5
Rachel
T
6
Joey
A
PERMISSIONS Table
user_id
permission_id
1
32
1
51
4
12
6
2
3
5
5
22
2
18
What I want is a way to delete all rows from the PERMISSIONS table where that user's STATUS is "T" but how would I do that?
I had tried this:
DELETE FROM permissions
WHERE user_id IN (
SELECT id FROM users
WHERE status = 'T'
);
However, SQL Server gives this error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
try apply join permissions and user and where status = 'T'
Example:
DELETE p
FROM permissions p
INNER JOIN users u
ON p.user_id=u.id
WHERE u.status = 'T'
I have a fact table and employee "tier" table, let's say.
So the fact table looks sorta like
employee_id call date
Mark 1 1-1-2017
Mark 2 1-2-2017
John 3 1-2-2017
Then there needs to be a data structure for 'tier level' - a slowly changing dimension table. I want to keep this simple -- I can change the structure of this table to whatever, but for now I've created it as such.
employee_id tier1_start ... tier2_start ... tier3_start
Mark 5-1-2016
John 6-1-2016 8-1-2016
Lucy 6-1-2016 10-1-2016
Two important notes. This table sort of operates under the assumption that a promotion will only occur once - aka no demotions and repromotions will occur. Also, it's possible one can jump from tier 1 to tier 3.
I was trying to come up with the best possible query for coming up with a 'tier' dimension (denormalization) for the fact table.
For instance, I want to see the Tier 1 metrics for February, or the Tier 2 metrics for February. Obviously the historically-changing tier dimension must be linked.
The clumsiest way I can think of doing this for now ... is simply joining the fact table on the tier table using employee_id.
Then, doing an even clumsier case statement:
case
when isnull(tier3_start,'0') < date then 'T3'
when isnull(tier2_start, '0') < date then 'T2'
when isnull(tier1_start, '0') < date then 'T1'
else 'other'
end as tier_level
Yes, as you can see this is very clumsy.
I'm thinking maybe I need to change the structure of this a bit.
You're probably better off splitting your tier table in two.
So have a Tier table like this:
TierID Tier
------------------
1 Tier 1
2 Tier 2
3 Tier 3
And an EmployeeTier table:
ID EmpID TierID TierDate
---------------------------------------
1 1 1 Jun 1, 2016
2 1 3 Oct 2, 2016
3 2 1 Jul 10, 2016
4 2 2 Nov 11, 2016
Now you can query the EmployeeTier table and filter on the TierID you're looking for.
This also gives you the ability to promote/demote multiple times. You simply filter by the employee and sort by date to find the current tier.
I am updating our in-house purchase system. While doing this, I had to move the service expiration date from the client table to the purchase table so each system could expire separately. I am trying to update the SQL statement to get systems that need to be billed, but am running into issues. Below is a simplified example of what I need:
Purchase Table
ID Product ClientID SystemID IsSystem ExpireDate
1 System1 12 1 1 9/22/2015
2 Feature1 12 1 0 NULL
3 Feature2 12 1 0 9/22/2016
4 System2 12 2 1 9/22/2016
5 Feature1 12 2 0 9/22/2015
6 Feature2 12 2 0 NULL
In the example above, I would need to get lines 1, 2 and 3. What I need to do is:
Look at each system to see if the ExpireDate is less than or equal to a given date (say today)
If it is, take all rows with a matching ClientID and SystemID reguardless of ExpireDate
Although line 5 would be expired, it would not get included because the system it is a part of has not expired.
This is getting inserted into a much larger SQL statement spanning several tables, but I should be able to extrapolate from an example with this table.
This is probably best accomplished using a common table expression that gets the expired client and systemIDs and then checks every row in your table against that list:
WITH expired AS
(
SELECT DISTINCT
ClientID,
SystemID
FROM purchase
WHERE
CONVERT(DATE, ExpireDate) <= CONVERT(DATE, GETDATE())
AND isSystem = 1
)
SELECT
*
FROM purchase p
WHERE
EXISTS (SELECT * FROM expired WHERE p.ClientId = ClientID AND p.SystemId = SystemId)
I have 2 tables one is accounts where users user stored his multiple accounts and other is reports table where we have stored the number of reports but there is no link between them.
table 1 (accounts)
accountid | contactId | accountname
121 12 personal
122 12 official
table 2 (Reports)
Reportid | ReportName
1 bankstatment
2 panreport
3 other report
Now I want this output
account | contactid| reportid | reportname | ReportId | ReportName | ReportId | ReportName
121 12 1 bankstatment 2 panreport 3 other report
122 12 1 bankstatment 2 panreport 3 other report
How I can make it in SQL Server query ? I am asking this because I am not able to identify which account has selected which report in mvc.net in grid form
Explanation
I am basically CROSS JOINing the accounts table with the PIVOT on the reports table.
This allows us to generate the desired output of all reports in a single row for each account.
Please try this query:
select
a.*,
1 as reportid,
[1] as reportname,
2 as reportid,
[2] as reportname,
3 as reportid,
[3] as reportname
from
accounts a
cross join
(
select [1],[2],[3]
from
(select * from Reports) s
pivot
(Max(ReportName) for Reportid in ([1],[2],[3]))p
)t
sql fiddle for demo : http://sqlfiddle.com/#!6/703c4/9
Simple answer: Not Possible
If there are 5 Bank Statements report in your report table, and 5 entries in your Accounts table, how would you know which report belongs to which account?
Also the last comment "I am not able to identify which account has selected which report in mvc.net in grid form" of your question is a different thing to what you have shown in your example. If you have fixed number of reports and you want a column for each of those report type, then yes, but if you want selected reports for each account, then you need to store that selection somewhere.
Is it possible to randomly select a record from the database excluding some records with particular status?
For eg,
For example, I have a table for storing employee details.
id employeename employeestatus
1 ab 1
2 cd 1
3 ef 2
4 gh 1
5 ij 1
What I want from the query is to fetch a single random record whose status is not 2. Is it possible to do so? The database I'm using is PostgreSQL 8.4.15.
TRY This
SELECT *
FROM employee
WHERE employeestatus != 2
ORDER BY RANDOM()
LIMIT 1
Try this other question on the same topic
Best way to select random rows PostgreSQL
It's tricker than you think (to do efficiently)