How to fetch data using where clause in SQL [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
There is no error, but the results show the values excluding the row that meets both conditions. The result shows 3, in fact, it should be 4 excluding data that has 22 for age and UK for the country. Any idea why the result is 3, not 4?
SELECT COUNT(*) FROM CUSTOMERS WHERE CUSTOMERS.AGE = '22' OR CUSTOMERS.COUNTRY = 'UK';

You have two customers at the age of 22 and 2 residing in the UK.
This is actually a correct result here - how on earth do you come to the conclusion that you have four people meeting that criteria? If more than one point matches for a database entry, it will only count as one for the result, since it's still the same database entry.

Related

SQL Query for the player and vsplayer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Team Table
Player (column)
A
B
C
Expected Output: with Two columns
Player vsPlayer
A B
B C
C A
How to write a sql query to get the exact output as Expected output mentioned above.
Thanks in advance
The normal way to get combination (pairs) is to take all permutations, but where column 1 is less than column 2.
SELECT
l.player,
r.player
FROM
player l
INNER JOIN
player r
ON l.player < r.player
Demo : https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=9ce7af3d0afe89c6434cc2800dfbd2ef

case when with count [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a quick sql data as below,
Customerid,Type
1,Adult
1,Adult
2,Adult
3,Adult
4,Teenager
4,Adult
I want the query that lists those customer no.s that do not have any other Type associated with them. For eg. 1 as only Adult associated with, same with 2. But 3 and 4 have multiple Types associated with them.
I am trying to get an output as below.
Customerid,Type
1,Adult
2,Adult
3,null
How should we tackle this.
You seem to want:
select customerid
, (case when min(type) = max(type) then min(type) end) as type
from table t
group by customerid;

SQL Server 2000: how to replace a entry with only the last 2 characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a table in a SQL Server 2000 database with a column with several entries like 01-03, 04-05. The first 2 digits are the room number and the last two digits are the bed number.
I want to replace the 01-05, 03-07 etc. with only the bed number. So only the last two charcters have to be in the column.
How do I accomplish this?
Sample data is:
In the column [Bed] with values:
22-01
08-01
09-03
01-16
Result has to be:
01
01
03
16
RIGHT should solve your problem:
SELECT RIGHT(RTRIM(Bed), 2) FROM tbl

SQL: Generating a field based on a field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table (for the sake of the exercise let's call it Persons)
I am trying to create a query that will generate the CrDRN number.
The CrDRN column should contain the DRN number of the last record that has a VoucherType = 80
In this instance the CrDRN column would have the values 1 1 1 5 5
Any help would be greatly appreciated.
The CrDRN column should contain the DRN number of the last record that
has a VoucherType = 80
Another way of saying that would be to get the max value of DRN where DRN is less than or equal to the current DRN and where VoucherType is 80.
You can formulate just that in a subquery that returns CrDRN.
select T1.DRN,
T1.VoucherType,
(
select max(T2.DRN)
from T as T2
where T2.DRN <= T1.DRN and
T2.VoucherType = 80
) as CrDRN
from T as T1

Django get all rows with same value in column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how can I filter in Django to get a data row one times if the same value in on column?
columns:
a | b
x | y
a | y
y | s
Want one data row set with y (b) and one data row set with s (b).
If not clear what I mean I edit with SQL script...
EDIT: SELECT DISTINCT b FROM table;
Sorry to be blind...
http://docs.djangoproject.com/en/dev/ref/models/querysets/
Have a nice day.