how to insert in a loop? - sql

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

Related

Left join with left table values with select query

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

Divide Sequential Records

I have a table in MS Access like:
table
+-----+-----+-----+
| 1st | 2nd | 3rd |
+-----+-----+-----+
| A | 1 | 100 |
| A | 2 | 200 |
| A | 3 | 300 |
| B | 1 | 100 |
| B | 2 | 200 |
| B | 3 | 300 |
| C | 1 | 100 |
| C | 2 | 200 |
| C | 3 | 300 |
+-----+-----+-----+
Now I want to read the values from the 3rd column, do some sort of manipulation to it and store them in to another table like:
summary
+-----+---------+---------+
| 1st | 2nd | 3rd |
+-----+---------+---------+
| A | 100/200 | 200/300 |
| B | 100/200 | 200/300 |
| C | 100/200 | 200/300 |
+-----+---------+---------+
In another words, for summary.2nd this means:
select table.3rd FROM table where table.1st = A AND table.2nd = 1
divided by
select table.3rd FROM table where table.1st = A AND table.2nd = 3
Can someone give me a hint how this could be done?
Maybe VBA / ADO Recordset etc?
One method is conditional aggregation:
select [1st],
max(iif([2nd] = 1, [3rd], null)) / max(iif([2nd] = 2, [3rd], null)) as [2nd],
max(iif([2nd] = 2, [3rd], null)) / max(iif([2nd] = 3, [3rd], null)) as [3rd]
from t
group by [1st];
Try this SQL
INSERT INTO Summary
SELECT DISTINCT a.[1st],
a.[3rd] / b.[3rd] AS [2nd],
a.[3rd] / c.[3rd] AS [3rd]
FROM ((tbl AS a
INNER JOIN tbl AS b
ON a.[1st] = b.[1st])
INNER JOIN tbl AS c
ON a.[1st] = c.[1st] )
WHERE a.[2nd] = 1
AND b.[2nd] = 2
AND c.[2nd] = 3
Here's another alternative, using calculated join criteria:
select
t1.[1st],
t1.[3rd]/t2.[3rd] as [2nd],
t2.[3rd]/t3.[3rd] as [3rd]
from
(
[table] t1 inner join [table] t2
on t1.[1st] = t2.[1st] and t1.[2nd] = t2.[2nd]-1
)
inner join [table] t3
on t1.[1st] = t3.[1st] and t1.[2nd] = t3.[2nd]-2
Since the 2nd column values 1, 2 & 3 are not hard-coded, this is applicable to any three integers in the 2nd column whose values differ sequentially by one.
Change [table] to the name of your table.

Query returned with an extra column in sql -ms access

So I am wondering. I fell into an interesting suggestion from another developer. So i basically have two tables I join in a query and I want the resulting table from the query to have an extra column that comes from the table on from the joint.
Example:
#table A: contains rating of players, changes randomly at any date depending
#on drop of form from the players
PID| Rating | DateChange |
1 | 2 | 10-May-2014 |
1 | 4 | 20-May-2015 |
1 | 20 | 1-June-2015 |
2 | 4 | 1-April-2014|
3 | 4 | 5-April-2014|
2 | 3 | 3-May-2015 |
#Table B: contains match sheets. Every player has a different match sheet
#and plays different dates.
MsID | PID | MatchDate | Win |
1 | 2 | 10-May-2014 | No |
2 | 1 | 15-May-2015 | Yes |
3 | 3 | 10-Apr-2014 | No |
4 | 1 | 21-Apr-2015 | Yes |
5 | 1 | 3-June-2015 | Yes |
6 | 2 | 5-May-2015 | No |
#I am trying to achieve this by running the ms-access query: i want to get
#every players rating at the time the match was played not his current
#rating.
MsID | PID | MatchDate | Rating |
1 | 2 | 10-May-2014 | 4 |
2 | 1 | 15-May-2015 | 2 |
3 | 3 | 10-Apr-2014 | 4 |
4 | 1 | 21-Apr-2015 | 4 |
5 | 1 | 3-June-2015 | 20 |
6 | 2 | 5-May-2015 | 3 |
This is what I have tried below:
Select MsID, PID, MatchDate, A-table.rating as Rating from B-table
left Join A-table
on B-table.PID = A-table.PID
where B-table.MatchDate > A-table.Datechange;
any help is appreciated. The solution can be in Vba as long as it returns something like a view/table I can manipulate using other queries or report.
Think of this in terms of sets of data... you need a set that lists the MAX dateChange for each player's and match date.
Soo...
SELECT MAX(A.DateChange) MDC, A.PID, B.Matchdate
FROM B-table B
INNER Join A-table A
on B.PID = A.PID
and A.DateChange <= B.MatchDate
GROUP BY A.PID, B.Matchdate
Now we take this and join it back to what you've done to limit the results in table A and B to ONLY those with that date player and matchDate (my inline table C)
SELECT B.MsID, B.PID, B.MatchDate, A.rating as Rating
FROM [B-table] B
INNER JOIN [A-table] A
on B.PID = A.PID
INNER JOIN (
SELECT MAX(Y.DateChange) MDC, Y.PID, Z.Matchdate
FROM [B-table] Z
INNER Join [A-table] Y
on Z.PID = Y.PID
and Y.DateChange <= Z.MatchDate
GROUP BY Y.PID, Z.Matchdate) C
on C.mdc = A.DateChange
and A.PID = C.PId
and B.MatchDate = C.Matchdate
I didn't create a sample for this using your data so it's untested but I believe the logic is sound...
Now Tested! SQL Fiddle using SQL server though...
My results don't match yours exactly. I think you're expected results are wrong though for MSID 4 given rules defined.

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;

Sub-sub-selects and grouping: Get name column from the row containing the max value of a group

I have two tables: States, and Items.
States:
+----+------+-------+----------+
| id | name | state | priority |
+----+------+-------+----------+
| 1 | AA | 10 | 1 |
| 2 | AB | 10 | 2 |
| 3 | AC | 10 | 3 |
| 4 | BA | 20 | 1 |
| 5 | BB | 20 | 5 |
| 6 | BC | 20 | 10 |
| 7 | BD | 20 | 50 |
+----+------+-------+----------+
Items:
+----+--------+-------+
| id | item | state |
+----+--------+-------+
| 1 | Blue | 10 |
| 2 | Red | 20 |
| 3 | Green | 20 |
| 4 | Yellow | 10 |
| 5 | Brown | 10 |
+----+--------+-------+
The priority column is not used in the Items table, but complicates getting the data I need, as shown below.
What I want is a list of the rows in the Items table, replacing the state.id value in each row with the name of the highest priority state.
Results would look like this:
+----+--------+-------+
| id | item | state |
+----+--------+-------+
| 1 | Blue | AC |
| 2 | Red | BD |
| 3 | Green | BD |
| 4 | Yellow | AC |
| 5 | Brown | AC |
+----+--------+-------+
Here's the tiny monster I've come up with. Is this the best way, or can I be more efficient / less verbose? (Sub-sub-selects make my palms itch. :-P )
SELECT *
FROM
Items AS itm
INNER JOIN (SELECT sta.name, sta.state
FROM (SELECT state, MAX(priority) [highest]
FROM States
GROUP BY state) AS pri
INNER JOIN States AS sta
ON sta.state = pri.state
AND sta.priority = pri.highest) AS nam
ON item.state = name.state
Update: I'm using MS-SQL 2005 and MS-SQL 2008R2
You did not post your version of SQL-Server. Assuming you are on 2005 or later you can use the ROW_NUMBER() function together with a cross apply like this:
CREATE TABLE dbo.States(id INT, name NVARCHAR(25), state INT, priority INT);
INSERT INTO dbo.States
VALUES
( 1 ,'AA', 10 , 1 ),
( 2 ,'AB', 10 , 2 ),
( 3 ,'AC', 10 , 3 ),
( 4 ,'BA', 20 , 1 ),
( 5 ,'BB', 20 , 5 ),
( 6 ,'BC', 20 , 10 ),
( 7 ,'BD', 20 , 50 );
CREATE TABLE dbo.Items( id INT ,item NVARCHAR(25), state INT );
INSERT INTO dbo.Items
VALUES
( 1 ,'Blue', 10 ),
( 2 ,'Red', 20 ),
( 3 ,'Green', 20 ),
( 4 ,'Yellow', 10 ),
( 5 ,'Brown', 10 );
SELECT i.id,
i.item,
s.name,
s.priority
FROM dbo.Items i
CROSS APPLY (
SELECT *,ROW_NUMBER()OVER(ORDER BY priority DESC) rn FROM dbo.States si WHERE si.state = i.state
)s
WHERE s.rn = 1;
The cross apply works like a join but allows to reference columns on the left side in the right side as you can see in the where clause. The ROW_NUMBER() function numbers all rows in the states table that match the current state value in reverse priority order so that the row with the highest priority always gets the number 1. The final where clause is filtering out just those rows.
EDIT:
I just started a blog series about joins: A Join A Day
The Cross Apply will be topic of day 8 (12/8/2012).