I have a row that should be duplicated in certain conditions.
Example:
Case 1:
a b c d e
---------------
1 4 25 10 NULL
if e is Null, display a, b and c:
1 4 25
Case 2:
a b c d e
---------------
1 4 25 10 55
if e is not Null, duplicate the row
1 4 25 => column a,b,c
1 4 10 => column a,b,d
Use this query
Using union you can achieve this use case
select a,b,c from table
union all
select a,b,d from table where e is not null
If you don't want to scan the table twice, then you can use cross joinand some logic:
select a, b,
(case when n = 1 then c else d end)
from t cross join
(select 1 as n union all select 2) n
where n = 1 or
(n = 2 and e is not null);
Related
I need to write an SQL query that fetches something like this :
a
b
c
1
x
3
2
y
4
3
x
7
4
y
9
transforms into the following form:1
Here's my coding attempt:
SELECT CONCAT (a) AS a , CONCAT (b, -c) as m FROM viborka
and the corresponding output I'm getting:
a
m
1
x-3
2
y-4
3
x-7
4
y-9
I can't merge expressions with X into string 1 and expressions with Y into string 2.
How can I do it?
You can do:
select
case when row_number() over(partition by b order by c) = 1 then a end as a,
concat(b, '-', c) as m
from t
order by b, c
Result:
a m
----- ---
1 x-3
null x-7
2 y-4
null y-9
See running example at db<>fiddle.
MySQL Version: 5.7.36
I'm attempting to minimize the amount of queries I have to execute.
Right now, I'm executing a query similar to this:
SELECT
TABLE 1.column1 as "A",
TABLE 1.column2 as "B"
FROM
TABLE 1
WHERE
CONDITION
I can obtain the results I need from this query, however I would also like to obtain the count of what type of values show up in the same query.
For example, if the following query retrieves this table
A B
- -
1 a
1 b
1 c
2 d
3 e
4 f
4 g
I would also like to, for each row, obtain the count of all rows retrieved with its column "A" that matches its value.
Would it be more efficient to execute another query to get that result or can I modify my obtaining query to get this statistic?
Desired result:
A B C
- - -
1 a 3 # 3 rows with "1" in Column "A"
1 b 3
1 c 3
2 d 1
3 e 1
4 f 2
4 g 2
UPDATE:
The closest query I could find goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column1
Results in this:
A B C
- - -
1 a 3
2 d 1
3 e 1
4 f 2
However, it removes any other rows with the same value in column "A". Even if it has different values in column "B". I would like to have all rows present in my SQL query with its corresponding row count.
The next closest query I found goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column2
Results in this:
A B C
- - -
1 a 1
1 b 1
1 c 1
2 d 1
3 e 1
4 f 1
4 g 1
Which also isn't achieving the desired result.
You have to join with the subquery that gets the counts.
SELECT t1.column1 AS A, t1.column2 AS B, t2.count
FROM Table1 AS t1
JOIN (
SELECT column1 AS A, COUNT(*) AS count
FROM Table1
GROUP BY column1
) AS t2 ON t1.A = t2.A
SELECT
c,
COUNT(*) OVER (PARTITION BY c)
FROM t
ORDER BY c
I just want to merge the content of 2 tables and display it based on the ID.
Both the table has 3 entries.
Table a - Sampling order
Date Docname Products Quantity ID
1 A A 1 1
2 B B 2 1
3 C C 3 1
Table B - Representative locations
Date Area lat long ID
1 a 1 1 1
2 b 2 2 1
3 c 3 3 1
The output should generate like 3 rows with all the table A columns and B columns where ID = Specified ID
I need a output like this
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
But its generating 9 rows (3*3) and duplicating the numbers of rows present in both the tables.
Its generating
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
Combing number of rows in A * B - I just need only 3 rows with respect to ID.
Query -
$Report = DB::table('sampling_order')
->join('representativelocations','representativelocations.representativeid','=','sampling_order.representativeid')
->select('sampling_order.representativeid as representativeid',
'sampling_order.date as date',
'sampling_order.doctor_name as doctor_name',
'sampling_order.products as products',
'sampling_order.quantity as quantity',
'representativelocations.latitude as latitude',
'representativelocations.longitude as longitude',
'representativelocations.area as area')
->whereBetween('sampling_order.date', [$Datefrom, $Dateto])
->where('sampling_order.representativeid','=',$Representativeid)->get();
What I can see, all Id is '1' in two tables,
so it should be 3 * 3 = 9 rows in result.
I guess you may want this:
id product area-A area-B area-C
1____A____1____2_____3
1____B____2____3_____4
if so. you need to join three times.
SELECT * FROM A
LEFT JOIN (SELECT *, area AS area-A FROM B WHERE area = 'a' ) AS B ON B.id = A.id
LEFT JOIN (SELECT *, area AS area-B FROM B WHERE area = 'b' ) AS C ON C.id = A.id
LEFT JOIN (SELECT *, area AS area-C FROM B WHERE area = 'c' ) AS D ON D.id = A.id
Hope this is what you want.
Basing on the requirement, i think you should try joining on date not ID as below. This will return 3 rows as you are expecting.
select so.date,so.Docname,so.products,so.Quantity,rl.Area,rl.lat,rl.long from Sampling_order AS so
INNER JOIN Representative_locations as rl ON so.Date = rl.Date
WHERE so.ID = 1
Change table names and column names as needed.
Assume that I have a table call client.
In this table I have 3 fields (A, B, C)
I would like to group each rows when values of A = B = C
ex:
A B C otherRow
1 2 3 x
2 1 x
4 2 y
I would like to get the folowing
(A,B,C) otherRow Count
1 x 2
2 x 2
2 y 1
3 x 1
4 y 1
Your query is UNION, not JOIN:
SELECT
`A,B,C`,
otherRow,
COUNT(`A,B,C`) AS `Count`
FROM
(SELECT a AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT b AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT c AS `A,B,C`, otherRow FROM t) AS u
GROUP BY
`A,B,C`,
otherRow
HAVING
`A,B,C` IS NOT NULL
check this fiddle. I've added NULL-check since it's not obvious what are your "empty" values. If you'll remove it, you'll get zero-count NULL-rows.
I need to obtain all elements m_id of Table A where m_active is = N and that the corresponding elements in Table B have ALL v_active = N.
m_id is foreign key in Table B.
In the example below, what I am looking for is m_id=2 and and m_id=4 as both satisfy the condition of being m_active=N and have ALL v_active = N.
How do I go about that?
Thanks
Table A example:
m_id m_active
1 Y
2 N
3 Y
4 N
Table B example:
v_id m_id v_active
1 1 N
2 1 Y
3 1 N
4 2 N
5 2 N
6 2 N
7 3 N
8 3 Y
9 3 Y
10 4 N
Try this:
SELECT * FROM A
WHERE m_active='N'
AND NOT EXISTS (
SELECT * FROM B
WHERE B.m_id=A.m_id
AND B.v_active<>'N'
);
SELECT *
FROM a
WHERE m_active = 'N'
AND m_id NOT IN
(
SELECT m_id
FROM b
WHERE v_active <> 'N'
)
This will also select all entries from a which have no corresponding entries in b (and hence all 0 of 0 entries are inactive). This may or may not be what you want.