i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;
Related
I have a table like this in MS Access 2019:
+-----------+------------+--------+----------+-------+
| BillingID | Date | RoomID | Electric | Water |
+-----------+------------+--------+----------+-------+
| 1 | 12/23/2018 | 4 | 1669 | 106 |
| 2 | 12/26/2018 | 1 | 5035 | 289 |
| 3 | 12/27/2018 | 6 | 0 | 0 |
| 4 | 12/31/2018 | 5 | 3158 | 223 |
| 5 | 1/6/2019 | 2 | 3823 | 194 |
| 6 | 1/15/2019 | 3 | 1772 | 125 |
| 7 | 1/23/2019 | 4 | 1796 | 117 |
| 8 | 1/26/2019 | 1 | 5231 | 299 |
| 9 | 1/27/2019 | 6 | 0 | 0 |
| 10 | 1/31/2019 | 5 | 3366 | 242 |
| 11 | 2/14/2019 | 2 | 3975 | 201 |
| 12 | 2/15/2019 | 3 | 1839 | 129 |
+-----------+------------+--------+----------+-------+
I could calculate the electricity and water usage with Index & Match in MS Excel. However, I've had a lot of trouble to achieve this with MS Access. The result I want is as below:
+-----------+------------+--------+----------+---------------+-------+------------+
| BillingID | Date | RoomID | Electric | ElectricUsage | Water | WaterUsage |
+-----------+------------+--------+----------+---------------+-------+------------+
| 1 | 12/23/2018 | 4 | 1669 | | 106 | |
| 2 | 12/26/2018 | 1 | 5035 | | 289 | |
| 3 | 12/27/2018 | 6 | 0 | | 0 | |
| 4 | 12/31/2018 | 5 | 3158 | | 223 | |
| 5 | 1/6/2019 | 2 | 3823 | | 194 | |
| 6 | 1/15/2019 | 3 | 1772 | | 125 | |
| 7 | 1/23/2019 | 4 | 1796 | 127 | 117 | 11 |
| 8 | 1/26/2019 | 1 | 5231 | 196 | 299 | 10 |
| 9 | 1/27/2019 | 6 | 0 | | 0 | |
| 10 | 1/31/2019 | 5 | 3366 | 208 | 242 | 19 |
| 11 | 2/14/2019 | 2 | 3975 | 152 | 201 | 7 |
| 12 | 2/15/2019 | 3 | 1839 | 67 | 129 | 4 |
+-----------+------------+--------+----------+---------------+-------+------------+
For example, for RoomID = 4, the ElectricUsage is the difference between the Electric in BillingID #7 and BillingID #1 and so on.
I've tried some answer like this or this but Access ran into errors when using those solutions in SQL view (Syntax error in FROM clause).
Thanks.
You can use a couple of sub-queries to return the Electric/Water for each room on the previous date:
SELECT
B.BillingID, B.BillingDate, B.RoomID, B.Electric,
B.Electric-(SELECT TOP 1 E.Electric FROM tblBilling AS E WHERE B.RoomID=E.RoomID AND E.BillingDate<B.BillingDate ORDER BY E.BillingDate DESC) AS ElectricUsage,
B.Water,
B.Water-(SELECT TOP 1 W.Water FROM tblBilling AS W WHERE B.RoomID=W.RoomID AND W.BillingDate<B.BillingDate ORDER BY W.BillingDate DESC) AS WaterUsage
FROM tblBilling AS B
Note that I've renamed your Date field to be BillingDate, as Date is a reserved word in Access, and will cause you problems in the future.
Regards,
Greetings I am trying to sum up expense value for each transaction.
Association table.
**Assoc table schema**
| PK_id | FK_transaction | FK_Expense |
|-------|----------------|------------|
| 1 | 1 | 85 |
| 2 | 2 | 81 |
| 3 | 3 | 77 |
| 4 | 4 | 83 |
| 5 | 5 | 84 |
| 6 | 6 | 105 |
| 7 | 7 | 104 |
| 8 | 8 | 71 |
| 9 | 8 | 88 |
| 10 | 8 | 90 |
Transaction table
**Transaction table schema**
| PK_id | type | value | confirmed_value |
|-------|------|-------|--------------------|
| 1 | 1 | 3.2 | 0 |
| 2 | 1 | 23.2 | 0 |
| 3 | 1 | 33.2 | 0 |
| 4 | 1 | 43.2 | 11.00 |
| 5 | 1 | 53.2 | 0 |
| 6 | 1 | 63.2 | 0 |
| 7 | 1 | 73.2 | 0 |
| 8 | 1 | 83.2 | 66.00 |
| 9 | 1 | 93.2 | 0 |
| 10 | 1 | 133.2 | 77.00 |
| 11 | 1 | 123.2 | 0 |
Expences Table
| PK_id | value |
|-------|-------|
| 85 | 3.2 |
| 81 | 23.2 |
| 77 | 33.2 |
| 83 | 43.2 |
| 84 | 53.2 |
| 105 | 63.2 |
| 104 | 73.2 |
| 71 | 83.2 |
| 88 | 93.2 |
| 90 | 133.2 |
Result ::
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 83.2 | 66 |
| 8 | 93.2 | 66 |
| 8 | 133.2 | 66 |
Desired - before calculations ( just to give u the idea )
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 309.6 | 66 |
Full desired result
count matching values COUNT(confirmed_value = value )
count entries that not match COUNT(confirmed_value != value )
/\ Calculate value of confirmed values
?? sum(transactions.confirmed_value)
and calculate value itself
Should be a row result ( example )
=======================================================================================================================
MATCHED | NOT_MATCHED | SUM(of values) | COUNT(of confirmed equal to value ) | COUNT(of confirmed ! equal to value )
=======================================================================================================================
1 | 10 | 3003.22 | 3 | 10
=======================================================================================================================
SQL Fiddle : http://sqlfiddle.com/#!9/2ab102/9
Thanks for any tips
Try this query -
SELECT transactions.PK_id,
SUM(expenses.value),
transactions.confirmed_value
FROM `assoc`
JOIN `expenses` ON `assoc`.`FK_Expense` = `expenses`.`PK_id`
JOIN `transactions` ON `assoc`.`FK_transaction` = `transactions`.`PK_id`
GROUP BY transactions.PK_id,
transactions.confirmed_value
Fiddle Demo - http://sqlfiddle.com/#!9/2ab102/14
by followed Mysql query to dynamically convert rows to columns on the basis of two columns,
I have 4 table to convert dinamiacally rows into columns as RESULT
TABLE A
| id | stages |
-----+----------
| 1 | Stage A |
| 2 | Stage B |
| 3 | Stage C |
TABLE B
| id_b | code | lessons |
-------+------+--------------
| 10 | FIS | Physics |
| 11 | MAT | Mathematics |
| 12 | KIM | Chemistry |
| 13 | BIO | Biology |
TABLE C
|id_c| students |
-----+-------------
| 20 | student 20 |
| 21 | student 21 |
| 22 | student 22 |
| 23 | student 23 |
| 24 | student 24 |
| 25 | student 25 |
| 26 | student 26 |
| 27 | student 27 |
TABLE D
| id_d | id_a | id_b | id_c | value_m | value_n |
-------+------+------+------+---------+----------
| 201 | 1 | 11 | 20 | 71 | 73 |
| 203 | 1 | 13 | 20 | 80 | 87 |
| 204 | 2 | 10 | 21 | 72 | 75 |
| 206 | 2 | 13 | 21 | 76 | 78 |
| 208 | 2 | 12 | 22 | 72 | 78 |
| 209 | 2 | 13 | 22 | 76 | 80 |
| 210 | 3 | 11 | 23 | 73 | 71 |
| 211 | 3 | 12 | 23 | 71 | 77 |
| 212 | 3 | 13 | 23 | 78 | 81 |
| 213 | 3 | 10 | 24 | 70 | 81 |
| 214 | 3 | 11 | 25 | 71 | 82 |
| 215 | 3 | 12 | 26 | 74 | 83 |
| 215 | 3 | 13 | 27 | 78 | 80 |
RESULT
| students | FIS_1 | MAT_1 | KIM_1 | BIO_1 | FIS_2 | MAT_2 | KIM_2 | BIO_2 | FIS_3 | MAT_3 | KIM_3 | BIO_3 |
-------------+-------+-------+-------+-------+--------+------+-------+-------+-------+-------+-------+--------
| student 20 | | 71,73 | | 80,87 | | | | | | | | |
| student 21 | | | | | 72,75 | 76,78 | | | | | | |
| student 22 | | | | | | | 72,78 | 76,80 | | | | |
| student 23 | | | | | | | | | | 73,71 | 71,77 | 78,81 |
| student 24 | | | | | | | | | 70,81 | | | |
| student 25 | | | | | | | | | | 71,82 | | |
| student 26 | | | | | | | | | | | 74,83 | |
| student 27 | | | | | | | | | | | | 78,80 |
Where FIS_1, MAT_1,... FIS_2, MAT_2.., FIS_3, MAT_3.. is CONCAT from TABLE B kode and TABLE A id,
and value MAT_1, BIO_1... is CONCAT from TABLE D value_m and value_n
It's Posible? How can I do that with GROUP_CONCAT mysql query? I've tried it but still not successful..
Thanks for your advance..
I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27
So this is what i have
Table : Bill
+------+------------+-----------------+---------------------+
| id | patient_id | bill_number | confirmed_date |
+------+------------+-----------------+---------------------+
| 14 | 32 | 4657 | 2012-07-06 04:11:05 |
| 15 | 33 | 4567 | 2012-07-07 05:11:05 |
| 16 | 34 | 4568 | 2012-07-08 06:11:05 |
| 17 | 35 | 7445 | 2012-08-08 07:11:05 |
+------+------------+-----------------+---------------------+
Table: Claim
+------+---------+------------+-------+--------------+---------------------+
| id | bill_id | patient_id | level | claim_format | confirmed_date |
+------+---------+------------+-------+--------------+---------------------+
| 10 | 14 | 32 | 1 | 1500 | 2012-08-10 10:57:17 |
| 11 | 14 | 32 | 1 | UB04 | 2012-08-10 11:01:42 |
| 12 | 15 | 33 | 1 | 1500 | 2012-09-10 13:57:17 |
| 13 | 15 | 33 | 1 | UB04 | 2012-09-10 12:01:42 |
| 14 | 16 | 34 | 1 | 1500 | 2012-10-10 12:57:17 |
| 15 | 16 | 34 | 1 | UB04 | 2012-10-10 13:01:42 |
| 16 | 17 | 35 | 1 | 1500 | 0012-11-10 15:57:17 |
| 17 | 17 | 35 | 1 | UB04 | 2012-11-10 14:01:42 |
+------+---------+------------+-------+--------------+---------------------+
I want to update the confirmed_date column of bill table with the confirmed_date of claim table after comparing the greater of the two dates for each bill_id(bill_id and patient_id in claims are foreign keys to id and patient_id in bill)
Did i make myself clear enough?
UPDATE Bill b
SET b.confirmed_date = ( SELECT MAX(confirmed_date) FROM Claim c WHERE b.id = c.bill_id)