Left join with left table values with select query - sql

I want a query where I can show the left table value with in select query.
Basically Left column have column A having same value but column B having different values so group_concat the column b value in the left table.
In the below query I am getting title of each in a separate row but I want it in same row against the commerce_order_item.order_id because commerce_order_item.order_id containe same value so want to group_concat(commerce_order_item.title)
SELECT commerce_order.order_id, commerce_order.mail, commerce_order.total_price__number, commerce_order.changed, commerce_order_item.title FROM commerce_order LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id WHERE cart = 1 AND commerce_order.changed BETWEEN $startdate AND $endates
Below is the query to group_concat
SELECT order_id, GROUP_CONCAT(title) FROM commerce_order_item GROUP BY order_id;
Resultant query
SELECT commerce_order.order_id, commerce_order.mail, commerce_order.total_price__number, commerce_order.changed, commerce_order_item.title FROM commerce_order LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id WHERE cart = 1 AND commerce_order.changed BETWEEN 1640998861 AND 1641258061 AND commerce_order_item.title = (SELECT GROUP_CONCAT(commerce_order_item.title) FROM commerce_order_item GROUP BY commerce_order_item.order_id)
Here are sample table:
commerce_order
commerce_order tabe as below
+--------+---------------+--------------+-----------+
|order_id| mail| | Total Price | changed |
+--------+---------------+------+-------+-----------+
| 1 |abc#gmail.com | 1000 |1641276265 |
| 2 |abc1#gmail.com | 5000 |1641276266 |
| 3 |abc2#gmail.com | 100 |1641276267 |
| 4 |abc3#gmail.com | 1001 |1641276268 |
| 5 |abc4#gmail.com | 10000 |1641276269 |
commerce_order_item table as below
+--------+-------+
|order_id| title |
+--------+-------+
| 1 | abc |
| 1 | xyz |
| 1 | def |
| 2 | ghi |
| 2 | lmn |
Result should be:
Order Id | Mail | total Price | Time(timestamp)| title
1 abc#gmail.com 1000 1641276265 abc,xyz,def
2 abc1#gmail1.com 5000 1641276266 ghi,lmn

Does this not give you what you want?
SELECT commerce_order.order_id
, commerce_order.mail
, commerce_order.total_price__number
, commerce_order.changed
, GROUP_CONCAT(commerce_order_item.title)
FROM commerce_order
LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id
WHERE cart = 1
AND commerce_order.changed BETWEEN 1640998861 AND 1641258061
GROUP BY commerce_order.order_id
, commerce_order.mail
, commerce_order.total_price__number
, commerce_order.changed

Related

Postgres SQL query to get the first row of distinct id

channels table
id | name
------------
1 | ABC
2 | XYZ
3 | MNO
4 | ASD
user_channels table
user_id | channel_id
----------------------
555 | 1
666 | 1
777 | 1
555 | 2
888 | 2
999 | 3
555 | 3
user_chats table
id | created_at | channel_id | content
---------------------------------------
2 | time 1 | 1 | Hello
3 | time 2 | 1 | Hi
4 | time 3 | 2 | Good day
5 | time 4 | 2 | Morning
I have these 3 tables in postgres SQL,
I want to write a sql query to get user_channels by user_id and it's latest message only (time 1 is oldest message) from user_chats table. How can I do that?
For example, for user_id = 555, the query should return
channel_id | content | created_at
---------------------------------------
1 | Hi | time 2
2 | Morning | time 4
3 | Null | Null
Use distinct on:
select distinct on (a.channel_id) a.*
from user_chats a
inner join user_channels l on l.channel_id = a.channel_id
where l.user_id = 555
order by a.channel_id, a.createt_at desc
If you want this for all users at once:
select distinct on (l.user_id, a.channel_id) l.user_id, a.*
from user_chats a
inner join user_channels l on l.channel_id = a.channel_id
order by l.user_id, a.channel_id, a.createt_at desc
You can use distinct on:
select distinct on (c.channel_id) c.channel_id, uc.content, uc.created_at
from user_channels c left join
user_chats uc
on uc.channel_id = c.channel_id
where c.user_id = ?
order by c.idchannel_id, uc.created_at desc;

how to insert in a loop?

i have two queries.
1st gets data from different tables in database
result for 1st query looks some thing like this. query1 can return 1 row, 2 rows or 10 rows
acount_ID | product_ID | Color | QTY | Size | Prize |
2 | 2 | blk | 1 | md | 100 |
2 | 1 | red | 2 | md | 50 |
2 | 9 | yllw | 1 | sm | 10 |
2 | 5 | wht | 5 | lg | 10 |
SELECT
DISTINCT b.[Account_ID], bi.[Product_ID],bi.[QTY], bi.[Color],
bi.[Size], p.[Price]
FROM [BAG_TB] b
LEFT JOIN [BAG_ITEM_TB] bi
on bi.[Bag_ID] = b.[Bag_ID]
LEFT JOIN [PRODUCT_TB] p
on p.[Product_ID] = bi.[Product_ID]
WHERE Account_ID = 2;
2nd query is inset query. in this query, I want to get all result from query1 and insert them in [order_Detail_TB].
INSERT INTO [ORDER_Detail_TB]
(Product_ID, QTY, Color, Size, Product_Prize, Total_Prize)
VALUES
(#Product_ID, #QTY, #Color, #Size, #Product_Prize, #Total_Prize);
INSERT INTO [ORDER_Detail_TB] (Product_ID, QTY, Color, Size,
Product_Prize, Total_Prize)
SELECT DISTINCT bi.[Product_ID],bi.[QTY], bi.[Color],
bi.[Size], p.[Price] FROM [BAG_TB] b LEFT JOIN [BAG_ITEM_TB] bi on
bi.[Bag_ID] = b.[Bag_ID] LEFT JOIN [PRODUCT_TB] p on p.[Product_ID] =
bi.[Product_ID] WHERE Account_ID = 2;
`
Update Dublicate: https://stackoverflow.com/a/4101761/971839

SQL Sum Columns

I want to get the following output:
Main table:
Email | Group | id
a#gmail.com | Y | 1
a#gmail.com | Y | 2
b#gmail.com | N | 3
c#gmail.com | N | 4
Join Table:
Email | Value
a#gmail.com | 10
b#gmail.com | 20
c#gmail.com | 30
Desired result (only take the a#gmail.com value once, despite appearing in the first table twice):
Group | Email Count | Sum
Y | 1 | 10
N | 2 | 50
Here is the sqlfiddle I've been playing around with:
http://sqlfiddle.com/#!9/c2a24d/8
You were close in your SQLFiddle. You just needed to join on a distinct select.
SELECT
e.Unsub as Unsub,
count(e.email) as EmailCount,
sum(c.sum) as EmailSum
FROM CountTable c
JOIN (select distinct email, Unsub from EmailsTable) e on c.email = e.email
GROUP BY e.unsub
SQLFiddle
First remove the duplicates, and then do the calculations
SQL DEMO
SELECT filter.`Unsub`, COUNT(*), SUM(`sum`)
FROM (
SELECT DISTINCT `Unsub`, `email`
FROM EmailsTable ) as filter
JOIN CountTable
ON filter.`email` = CountTable.`email`
GROUP BY filter.`Unsub`
OUTPUT
| Unsub | COUNT(*) | SUM(`sum`) |
|-------|----------|------------|
| N | 2 | 50 |
| Y | 1 | 10 |

Select query INNER JOIN issue

I have tow tables Requisitions and RequisitionDetails
Requisitions table
+---------------+-----------------+
| RequisitionID | RequisitionDate |
+---------------+-----------------+
| 1 | 2016-08-17 |
| 2 | 2016-08-18 |
| 3 | 2016-08-19 |
+---------------+-----------------+
RequisitionDetails table
+---------------------+---------------+--------+----------+------------------+
| RequisitionDetailID | RequisitionID | ItemID | Quantity | ReceivedQuantity |
+---------------------+---------------+--------+----------+------------------+
| 1 | 1 | 1 | 2 | 1 |
| 2 | 1 | 2 | 3 | 2 |
| 3 | 2 | 3 | 4 | 3 |
+---------------------+---------------+--------+----------+------------------+
I am trying to get Requisition data where Quantity is not equal to ReceivedQuantity.
i have tried the below query but its record with RequisitionID 1 twice.
How can i make the query returns the Requisition data without repeating the requisition data based on items that have Quantity is not equal to ReceivedQuantity.
SELECT
dbo.Requisitions.RequisitionID,
dbo.Requisitions.RequisitionDate
FROM dbo.Requisitions
INNER JOIN dbo.RequisitionDetails
ON dbo.Requisitions.RequisitionID = dbo.RequisitionDetails.RequisitionID
where dbo.RequisitionDetails.Quantity != dbo.RequisitionDetails.ReceivedQuantity
It's returning twice because of the two rows with RequistionID = 1 in the RequistionDetails table. Since the rows returned are exact duplicates you can simply add the DISTINCT keyword to your select to see one of them:
SELECT DISTINCT
dbo.Requisitions.RequisitionID,
dbo.Requisitions.RequisitionDate
FROM dbo.Requisitions
INNER JOIN dbo.RequisitionDetails
ON dbo.Requisitions.RequisitionID = dbo.RequisitionDetails.RequisitionID
where dbo.RequisitionDetails.Quantity!=
dbo.RequisitionDetails.ReceivedQuantity
You should also use some aliases to clean up your query:
SELECT DISTINCT
R.RequisitionID,
R.RequisitionDate
FROM dbo.Requisitions R
INNER JOIN dbo.RequisitionDetails RD ON R.RequisitionID = RD.RequisitionID
WHERE RD.Quantity != RD.ReceivedQuantity
You also can use exists for your case
select
* from requistions rq where exists(
select 1 from RequisitionDetails rd where rd.RequisitionID=rq.RequisitionID
and rd.Quantity!=rd.ReceivedQuantity)
As you don't need columns from the 2nd table you can also switch to EXISTS to avoid DISTINCT:
SELECT req.*
FROM dbo.Requisitions as req
WHERE EXISTS
( SELECT * FROM dbo.RequisitionDetails as req_det
WHERE req.RequisitionID = req_det.RequisitionID
AND Quantity <> ReceivedQuantity
)
Or IN:
SELECT req.*
FROM dbo.Requisitions
WHERE RequisitionID IN
( SELECT RequisitionID
FROM dbo.RequisitionDetails
WHERE Quantity <> ReceivedQuantity
)

Oracle ordering by several same meaning columns

I have to make sortable table like this:
Sortable table:
building_id | building_age | title |
-------------------------------------------------
1 | 100 | New york buil |
2 | 50 | House 1 |
3 | 50 | House 10 |
From these tables:
Building Table:
building_id | building_age | building_type_1_FK | building_type_2_FK
---------------------------------------------------------
1 | 100 | null | 1
2 | 50 | 1 | null
3 | 50 | 2 | null
building_type_1:
type_id | title | diff1 |
-------------------------------------------------
1 | New york buil| blablabla |
building_type_2:
building_id | title |
----------------------------
1 | House 1 |
2 | House 10 |
3 | House 500 |
While joining these tables I get several title columns where one of them is not null. Is there any way to sort by title and select top 10 results without fetching all the data and then sorting in the app?
p.s.. I know that in general this architecture is not good, but I can't change it.
Yes. You want to do a left outer join to the two tables, and then bring the results together:
select b.building_id, b.building_age, coalesce(bt1.title, bt2.title) as title
from building b left outer join
building_type_1 bt1
on b.building_type_1_FK = bt1.type_id left outer join
building_type_2 bt2
on b.building_type_2_FK = bt2.building_id;
To get the top 10 results in Oracle:
select *
from (select b.building_id, b.building_age, coalesce(bt1.title, bt2.title) as title
from building b left outer join
building_type_1 bt1
on b.building_type_1_FK = bt1.type_id left outer join
building_type_2 bt2
on b.building_type_2_FK = bt2.building_id
order by title
) b
where rownum <= 10;