I have the following tables:
Sales Table:
+----+------------+
| ID | Date |
+----+------------+
| 1 | 11/20/2018 |
| 2 | 11/21/2018 |
+----+------------+
Items Table:
+----+------------+----------+-------+----------+
| ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----------+-------+----------+
| 1 | 1 | 5 | 100 | 500 |
| 2 | 1 | 3 | 50 | 150 |
| 3 | 2 | 5 | 60 | 300 |
+----+------------+----------+-------+----------+
Currently, I query for the Sale rows and then run another query for each to retrieve all of its Items after which, I initialize them as objects in my C# Program. My problem is that I frequently run into scenarios where I'm dealing with hundreds of sales at the same time. Because of this, I have to run hundreds of queries as well.
My question is, is there a more efficient way to query for all Sales and their Items or is this it? I thought of trying a query like
SELECT *
FROM items_table
WHERE FK_Sale_ID = '1' OR FK_Sale_ID = '2'
And then manually sorting which Item belonged to which sale for initialization but this query quickly got messy after dealing with more than a few sales. Any ideas?
You can use join clause to combine multiple tables data in your query:
SELECT *
FROM items_table
JOIN sales_table ON items_table.FK_Sale_ID = sales_table.ID
WHERE sales_table.Date = #somedate
With this query when #somedate = 11/20/2018 you basically will get this data in one go:
+----+------------+----+------------+----------+-------+----------+
| ID | Date | ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----+------------+----------+-------+----------+
| 1 | 11/20/2018 | 1 | 1 | 5 | 100 | 500 |
| 1 | 11/20/2018 | 2 | 1 | 3 | 50 | 150 |
+----+------------+----+------------+----------+-------+----------+
Use SELECT items_table.* to get all fields of items table only
I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue
I want to show data separately from income and expense table in MySQL using same query for both things but it show me when show income title date then same show expense title date. But I need to do if it find income table's 4th row and if find expense table's 2nd row, but it show me 4th row. Why?
SELECT income.date,
IF(income.income_sourch IS NULL, 'N/A', income.income_sourch )as 'Income Title',
income.amount as 'income',
IF(expense.expense_sourche IS NULL, 'N/A', expense.expense_sourche) as 'ExpenseTitle',
expense.amount as 'Expense'
from expense RIGHT JOIN income
ON expense.date=income.date
where expense.date='30-Aug-2016'
Why - because that's how joins work when you have only a date to join the 2 tables. To solve this you need to create a relationship in addition to the date bearing in mind that at any one time the number of records in income could be greater than, less than or equal to the number of records in expenses. If we generate a row number for each row in income and independently generate a row number for each row in expenses then we can join income and expenses on row number
for example
/*
CREATE TABLE INCOME (ID INT,DESCRIPTION VARCHAR(10),DT DATE,AMOUNT INT);
INSERT INTO INCOME VALUES
(1,'SALE','2016-08-30',100),
(2,'ROKY','2016-08-30',200),
(3,'KORIM','2016-08-30',300),
(4,'SALEVOUCHR','2016-08-30',400);
CREATE TABLE EXPENSES (ID INT,DESCRIPTION VARCHAR(10),DT DATE,AMOUNT INT);
TRUNCATE TABLE EXPENSES;
INSERT INTO EXPENSES VALUES
(1,'RENT','2016-08-30',100),
(2,'UTILITIES','2016-08-30',100);
/*
This code
SELECT S.DT,S.DESCRIPTION,S.AMOUNT, s.rn,T.DESCRIPTION,T.AMOUNT, t.rn1
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,INCOME I
) S
LEFT OUTER JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, EXPENSES E
) T ON T.RN1 = S.RN AND T.DT = S.DT
WHERE S.DT IS NOT NULL
results in
+------------+-------------+--------+------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | rn | DESCRIPTION | AMOUNT | rn1 |
+------------+-------------+--------+------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | NULL | NULL | NULL |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | NULL | NULL | NULL |
+------------+-------------+--------+------+-------------+--------+------+
BUT this is UNSAFE because it assumes that there are always more income than expense items
for example if we add 3 more expense rows
TRUNCATE TABLE EXPENSES;
INSERT INTO EXPENSES VALUES
(1,'RENT','2016-08-30',100),
(2,'UTILITIES','2016-08-30',100),
(3,'RATES','2016-08-30',100),(4,'SALARY','2016-08-30',100),(5,'INSURANCE','2016-08-30',100);
then the last expense item is lost resulting in
+------------+-------------+--------+------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | rn | DESCRIPTION | AMOUNT | rn1 |
+------------+-------------+--------+------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | RENT | 100 | 3 |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | UTILITIES | 100 | 4 |
+------------+-------------+--------+------+-------------+--------+------+
To cope with this we can simulate a full join like so
SELECT S.*,T.*
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,INCOME I
) S
LEFT JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, EXPENSES E
) T ON T.RN1 = S.RN AND T.DT = S.DT
UNION
SELECT S.*,T.*
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN3:=#RN3 + 1 RN3
FROM (SELECT #RN3:=0) RN3,INCOME I
) S
RIGHT JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN4:=#RN4 + 1 RN4
FROM (SELECT #RN4:=0) RN4, EXPENSES E
) T ON T.RN4 = S.RN3 AND T.DT = S.DT
result
+------------+-------------+--------+------+------------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | RN | DT | DESCRIPTION | AMOUNT | RN1 |
+------------+-------------+--------+------+------------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | 2016-08-30 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | 2016-08-30 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | 2016-08-30 | RATES | 100 | 3 |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | 2016-08-30 | SALARY | 100 | 4 |
| NULL | NULL | NULL | NULL | 2016-08-30 | INSURANCE | 100 | 5 |
+------------+-------------+--------+------+------------+-------------+--------+------+
you just need to add limits inside the query
if expense table limit 2
if other limit 4
See how to add limit to the query with if condition
[DONE] for those who need this.. I will edit this and put the answer below..
How to make LName and FName all CAPS using subquery?
I already make all LName and FName CAPS but not in subquery using this code
Select UPPER(LName) AS "Lastname", UPPER(FName) AS "Firstname" FROM Trainers;
OUTPUT:
+-----------+-----------+
| Lastname | Firstname |
+-----------+-----------+
| MONTANO | GERALD |
| ALEJANDRO | LEONARD |
| MONTEZ | GARY |
| GUILLERMO | JACKIE |
| SANCHEZ | VERNA |
| GABORNI | MICHELLE |
+-----------+-----------+
Table Trainers
+-----------+-----------+----------+-------+
| TrainerNo | LName | FName | Rate |
+-----------+-----------+----------+-------+
| 00804871 | Montano | Gerald | 200 |
| 00807461 | Alejandro | Leonard | 175 |
| 00807462 | Montez | Gary | 150 |
| 00823253 | Guillermo | Jackie | 200 |
| 00827320 | Sanchez | Verna | 125 |
| 00845907 | Gaborni | Michelle | 150 |
+-----------+-----------+----------+-------+
ANSWER:
Select UPPER(Lname) AS "Lastname", UPPER(FName) AS "Firstname" from Trainers Where Rate <= (Select Max(Rate) from Trainers);
IF you want to return the first name concatenated with the second name in upper case in a sub-query. Then you can do this.
Select * from Table Name, (Select UPPER(LName) + UPPER(FName) FROM Trainers) as Name
I have a problem with displaying and editing some data from database to ASP.Net webpage.
Data from database consist from few different tables:
Table Files, witch contains file_id and a file name
| file_id | file_name |
--------------------------
| GUID | VARCHAR |
file is saved in database as a collection of lines for one file_id,
Table Items,witch contains basically all the lines from all the files
| item_id | file_id | step | number |
---------------------------------------------------
| GUID | VARCHAR | BIGINT | BIGINT |
Table Wording, witch contains all distinctive wording
| wording_id | wording_eng | description |
-----------------------------------------
| GUID | VARCHAR | VARCHAR |
And M_N_table witch contains connection between items and wording
| item_id | wording_id |
----------------------------
| GUID | GUID |
my final display table should look like this and should be editable
| | file1 | file2 | file3 |.........| fileN | | |
| sortorder --------------------------------------------------------------------------| wording_eng | desription |
| | step | number | step | number | step | number |.........| step | number | | |
------------------------------------------------------------------------------------------------------------------
| 1 | 10 | 15 | | | | |.........| | | something1 | something2 |
------------------------------------------------------------------------------------------------------------------
| 2 | | | | | 25 | 15 |.........| 5 | 17 | something3 | something4 |
------------------------------------------------------------------------------------------------------------------
| 3 | 2 | 4 | 5 | 16 | 17 | 3 |.........| 1 | 15 | something5 | something6 |
So for every file from some selected list,witch means variable number of files, I need to read all the wordings and if a wording is connected to item in M_N_table and that item exist in selected files I have to write value of step and number to that files column as shown, if they don't exist it will be written blank value.
Problem I'm having is creating Table or DataList or GridView where I can have connection to item_id and wording_id when I'm editing data. I managed to create "pivot" datatable in C# in backcode, using all of these tables and created gridview with dynamic number of columns but problem is I don't have information about what item_id I'm actually editing it just goes back to changing value in datatable.
What is the best way to display this data? I tried making dynamic pivot table in SQL to display it directly with gridview but without aggregation it's impossible,so I had to do it with datatable's in C# back-code. And how can I make it so that when I edit data in displayed table I get the same changes in database?