I want to select clients only when all the lines in Credit are filled. From the table I want to see only the lines for Client 1, Client 3, Client 4 and Client 5.
I was trying this:
SELECT *
FROM table
WHERE EXISTS (SELECT client FROM table WHERE [CREDIT] = 'cred')
But not working...
Thanks!
Client
Credit
1
CRED
1
CRED
1
CRED
1
CRED
1
CRED
2
2
CRED
2
CRED
3
CRED
3
CRED
3
CRED
3
CRED
4
CRED
4
CRED
5
CRED
6
6
6
6
CRED
6
CRED
You need a self-(antisemi)join for this:
SELECT t.*
FROM table t
WHERE NOT EXISTS (SELECT 1
FROM table t2
WHERE t1.client = t2.client
AND t2.credit IS NULL)
SELECT client, count(credit) FROM t
group by client
having count(client) = sum(case when credit is null then 0 else 1 end)
Related
I have below table structure
To delete the record user will pass the ID, but we have similar text in Data column all the records should be deleted, for example in the above table if users pass 1, we need to delete 3 records (ID 1,2,3), please help for the single delete query.
Use a subquery:
SQL> select * From test;
ID DAT
---------- ---
1 abc
2 abc
3 abc
4 def
5 ijk
6 lmn
6 rows selected.
SQL> delete from test t
2 where t.data = (select t1.data
3 from test t1
4 where t1.id = &id
5 );
Enter value for id: 1
old 4: where t1.id = &id
new 4: where t1.id = 1
3 rows deleted.
SQL> select * from test;
ID DAT
---------- ---
4 def
5 ijk
6 lmn
SQL>
I have one parent table "Employee", the employee information is stored in three children tables and each children table has one children table. Consider the following tables:
Table: Employee (Level #1)
EmpId IsActive
__________________
1 1
2 1
3 1
4 0
5 0
6 1
Table: EmployeeEmail (Level #2)
EmpEmailId EmpId EmailId
______________________________
1 1 1
2 4 3
3 6 4
Table: EmailAddress (Level #3)
EmailId Email
____________________________
1 one#gmail.com
2 two#gmail.com
3 three#gmail.com
4 four#gmail.com
Table: EmployeePhone (Level #2)
EmpPhoneId EmpId PhoneId Type
____________________________________________
1 1 1 Mobile
2 2 2 Mobile
3 5 4 Fax
4 1 6 Fax
5 2 9 Home
Table: PhoneNumber (Level #3)
PhoneId PhoneNumber
_______________________
1 9912345671
2 9912345672
3 9912345673
4 9912345674
5 9912345675
6 9912345676
7 9912345677
8 9912345678
9 9912345679
Now I need to select the Active Employee Records (Full Information), if the employee has phone number then it should come otherwise it should be NULL, I need the same for Email too.
My expected output:
EmpId Email Home Mobile Fax
____________________________________________________________________
1 one#gmail.com NULL 9912345671 9912345676
(...)
This question is similar to my previous question How to effeciently SELECT Nested dependency Tables using JOIN in SQL Server
Kindly assist me how to fetch the multiple phone numbers in a single row?
This is a simple case style pivot that looks like it would suit your needs:
select
e.EmpId
, Email = max(em.Email)
, Home = max(case when ep.Type = 'Home' then pn.PhoneNumber else null end)
, Mobile = max(case when ep.Type = 'Mobile' then pn.PhoneNumber else null end)
, Fax = max(case when ep.Type = 'Fax' then pn.PhoneNumber else null end)
from Employee as e
left join EmployeeEmail as ee on e.EmpId = ee.EmpId
left join EmailAddress as ea on ee.EmailId = ea.EmailId
left join EmployeePhone as ep on e.EmpId = ep.EmpId
left join PhoneNumber as pn on ep.PhoneId = pn.PhoneId
where e.IsActive = 1
group by e.EmpId
I'm sure there is an easy way to do this but I've been struggling with this one...
Suppose I have an order table like so:
OrderId OrderStatus DriverId TripId
------- ----------- ------ ----
1 Available 5 2
2 Available 5 2
3 Available 5 2
4 Delivered 5 2
5 Delivered 5 3
6 Delivered 6 2
I want to group by each Driver and Trip with an extra column displaying the count of the OrderStatus when it is equal to 'Available'. So, for example
TotalOrderCountInTrip DriverId TripId AvailableOrdersCount
--------------------- -------- ------ --------------------
4 5 2 3
1 6 2 0
I've gotten this far but I can't figure out how to add the AvailableOrdersCount column:
select count(*) TotalOrderCountInTrip, dos.DriverId, dos.TripId
from DriverOrderSet dos (nolock)
group by
dos.DriverId,
dos.TripId
Add as a column:
sum(case when OrderStatus = 'Available' then 1 else 0 end)
I have two simple SELECT statements:
The first shows a list of Features.
SELECT * FROM Features
id name
-- ----
1 24 Hour Access
2 24 hour CCTV monitoring
3 Airport location
4 Break-Out Areas
5 Business Lounge
6 Business park location
snip..
and the second statement shows a list of feature information that has changed
SELECT
*
FROM
#SmartFeaturesToUpdate new_features
ORDER BY
new_features.centre_translation_id,
new_features.feature_id,
new_features.feature_selected
feature_id centre_translation_id feature_selected
---------- --------------------- ----------------
1 1 1
2 1 1
5 1 1
10 1 1
11 1 1
snip..
What I want to see is all of the features by centre translation.
Combining the tables gives me:
SELECT
*
FROM
#SmartFeaturesToUpdate new_features
LEFT JOIN Feature feature ON feature.id = new_features.feature_id
ORDER BY
new_features.centre_translation_id,
new_features.feature_id,
new_features.feature_selected
feature_id centre_translation_id feature_selected id name
---------- --------------------- ---------------- -- ----
1 1 1 1 24 Hour Access
2 1 1 2 24 hour CCTV monitoring
5 1 1 5 Business Lounge
10 1 1 10 Double Glazing
11 1 1 11 Elevator
snip..
The result above is missing feature id's 3 and 4, because they are not in the second list.
but the result I need is:
feature_id centre_translation_id feature_selected id name
---------- --------------------- ---------------- -- ----
1 1 1 1 24 Hour Access
2 1 1 2 24 hour CCTV monitoring
3 1 1 3 Airport Location
4 1 1 4 Break-Out Area
5 1 1 5 Business Lounge
snip..
How should I modify the third SELECT statement to acheive this and combine the results from both the features and feature information list?
As the comments alluded, I needed another table which linked Features to centre_translation_ids
First get all of the feature / centre_translation varients
SELECT
[centre_translation_id] = centre_translation.id,
feature.id,
feature.name
INTO #AllTheFeatures
FROM
CentreTranslation centre_translation
CROSS JOIN Feature feature
ORDER BY
centre_translation.id,
feature.id
Now we can simply perform the LEFT JOIN
SELECT
all_features.centre_translation_id,
all_features.id,
all_features.name,
smart_features.feature_selected
FROM
#AllTheFeatures all_features
LEFT JOIN #SmartFeaturesToUpdate smart_features ON smart_features.centre_translation_id = all_features.centre_translation_id AND
smart_features.feature_id = all_features.id
ORDER BY
all_features.centre_translation_id,
all_features.id
This gives the results:
centre_translation_id id name feature_selected
--------------------- -- ---- ----------------
1 1 24 Hour Access 1
1 2 24 hour CCTV monitoring 1
1 3 Airport location NULL
1 4 Break-Out Areas NULL
1 5 Business Lounge 1
Why don't you just put it in one query?
SELECT
centre_translation.id AS centre_translation_id,
feature.id,
feature.name,
smart_features.feature_selected
FROM
CentreTranslation centre_translation
CROSS JOIN Feature feature
LEFT JOIN #SmartFeaturesToUpdate smart_features
ON smart_features.centre_translation_id = all_features.centre_translation_id
AND smart_features.feature_id = all_features.id
ORDER BY
centre_translation.centre_translation_id,
feature.id
In my SQl server Sp.
`SELECT rating as [Rating],count(id) as [RatingCount]
FROM MMBPollResults
where mmb_id = #MMbid
GROUP BY rating
This SP returns the rating for each user.
i:e rating ratingcount
` 1 2
2 1
5 4
So this means that
2users have rated the transaction with 1star
1 user has rated the transaction with 2stars
4 users have rated the transaction with 5stars
This is how I need the output
rating ratingcount
` 1 2
2 1
3 0
4 0
5 4
Sorry, if this is a silly question
Thanks
Sun
You need a table with 1 to 5. This could be a number table or some other rating table.
Here I use a simple UNION to make a table with 1 to 5
SELECT
List.Rating,
count(MMB.*) as [RatingCount]
FROM
(
SELECT 1 AS Rating
UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
) List
LEFT JOIN
MMBPollResults MMB ON List.Rating = MMB.Rating AND MMB.mmb_id = #MMbid
GROUP BY
List.Rating
ORDER BY
List.Rating;