I was wondering if what is this called or is this even possible in MySql, or what should I do to achieve this. This is what Mysql Table Looks Like:
Desired Output:
you can use a query like this:
SELECT customerid
, CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
FROM myitems
GROUP BY SUBSTRING_INDEX(item, ',', -1);
Sample
MariaDB [bernd]> SELECT * FROM myitems;
+----+------------+---------------+
| id | customerid | item |
+----+------------+---------------+
| 1 | 15 | 7pcs, Car |
| 2 | 15 | 2pcs, Car |
| 3 | 15 | 3pcs, Engine |
| 4 | 15 | 2pcs, Engine |
| 5 | 15 | 5pcs, Exhaust |
+----+------------+---------------+
5 rows in set (0.07 sec)
MariaDB [bernd]> SELECT customerid
-> , CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
-> FROM myitems
-> GROUP BY SUBSTRING_INDEX(item, ',', -1);
+------------+---------------+
| customerid | item |
+------------+---------------+
| 15 | 9pcs, Car |
| 15 | 5pcs, Engine |
| 15 | 5pcs, Exhaust |
+------------+---------------+
3 rows in set, 5 warnings (0.02 sec)
MariaDB [bernd]>
Your table structures wont allow (anything near as easy). A better format of your table might be more like
id customerid item qty
1 15 Car 7
2 15 Car 2
3 15 Engine 3
4 15 Engine 2
5 15 Exhaust 5
Then, you could get what you are looking for easily such as
select
t.customerid,
t.item,
sum( t.qty )
from
YourTable t
group by
t.customerid,
t.item
This WOULD give your your
customerid item qty
15 Car 9
15 Engine 5
15 Exhaust 5
Having a properly formatted table vs trying to have a description requires parsing numeric values vs rest of description is never a good solution such as what you have here. Too many opportunities for bad answers.
Related
So basically I have a table like the following:
+----+-----------+---------+-------+-------+------------+
| ID | ProductID | ShopID | Amount| Price | AddedDate |
+----+-----------+---------+-------+-------+------------+
| 1 | 1 | 1 | 1000 | 220 | 2020-07-01 |
| 2 | 1 | 1 | 1000 | 230 | 2020-07-30 |
| 3 | 1 | 1 | 100 | 21 | 2020-07-10 |
| 4 | 1 | 1 | 100 | 22 | 2020-07-31 |
| 5 | 1 | 2 | 100 | 23 | 2020-07-10 |
| 6 | 1 | 2 | 100 | 20 | 2020-07-12 |
Just finding the lowest price for a product is pretty easy with
lowestUnitPrice = product.Sales.Min(s => s.Price / s.Amount)
same for the highest price highestUnitPrice = product.Sales.Max(s => s.Price / s.Amount)
However, as you can see, I'm keeping a price history. As such for determining the lowest latest unit price, I need to use only the latest entry for a given amount. In the table above, it should discard the row with ID 1 as the row with ID 2 has an identical amount, but a newer AddedDate.
I'm trying to find a solution with a single query, but I don't think it's possible. In SQL finding the lowest unit price would look something like this I think, given that table Sales here, are the sales for a specific product.
SELECT MIN(Price / Amount)
FROM productsales
WHERE (ProductID, AddedDate) IN
(SELECT ProductID, max(AddedDate)
FROM productsales
GROUP BY ProductID, ShopID, Amount
)
The expected output from the above table would be 0.20 (row ID 6) for the lowest unit price, 0.23 (row ID 2) for the highest unit price.
SQL demo here: http://sqlfiddle.com/#!9/be74da/2
So one way to do it is
var query = from p1 in products
where
(from p2 in products
group p2 by new { p2.ShopID, p2.Amount } into g
select (g.Max(p2 => p2.AddedDate))
).Contains(p1.AddedDate)
select (p1.Price / p1.Amount);
decimal lowestPrice = query.Min();
decimal highestPrice = query.Max();
Don't know if it can be done shorter or more efficiently?
Demo to be found here: https://dotnetfiddle.net/MRJzHs
I want list all SOURCE_ID if the source has destination, the query below not work probably !!
select SOURCE_ID,
LINK_TYPE,
DESTINATION_ID
from LINK_TABLE
where link_table.link_type=1
and link_table.destination_is_deleted=0
START WITH link_table.source_id='100'
CONNECT BY PRIOR link_table.source_id=link_table.destination_id
Sample data
SOURCE_ID | DESTINATION_ID | LINK_TYPE| DESTINATION_IS_DELETED|
----------|-----------------|----------|-----------------------|
100 | 1500 | 1 | 0 |
100 | 1200 | 1 | 0 |
100 | 1300 | 1 | 1 |
1500 | 600 | 1 | 0 |
1500 | 700 | 1 | 0 |
700 | 88 | 1 | 0 |
Assuming you want to walk the hierarchy from the root nodes to the leaves this is what you need:
select SOURCE_ID,
LINK_TYPE,
DESTINATION_ID
from LINK_TABLE
where link_table.link_type=1
and link_table.destination_is_deleted=0
START WITH link_table.source_id='100'
CONNECT BY PRIOR link_table.destination_id = link_table.source_id
/
It's just a matter of swapping the referenced columns in the CONNECT BY PRIOR clause. Unfortunately the Oracle syntax is not intuitive here: I've been using it for over twenty years and I still have to test a query to make sure I've got them the right way round :)
I changed the query structure to solve the issue.
select SOURCE_ID,DESTINATION_ID,link_table.source_class,link_table.linktype,link_table.destination_class
from LINK_TABLE
where (link_table.source_id in( select link_table.DESTINATION_ID from link_table where link_table.source_id='100'and link_table.linktype=1 and link_table.destination_isdeleted=0 ))
and (link_table.linktype=1) or (link_table.source_id='100')
I have a question on mind for a few long days.
Finally, I made a SQL query, which I want to show in MVC4 View.
I build following SQL query:
select distinct date, max(priority) from Timetables where date between '2013-12-01' and '2013-12-31' group by date
which returns me a collection of dates and max priorities of that dates. That is OK for me. I am totaly okay, when this query returns me a whole Timetables record.
But I need to pass result of this query to View engine of MVC4.
I had some tries, but I am nowhere near finding a result for that.
If you have some other possibilities, how to do that, I am eager to hear :)
Also, I am using an Entity Framework.
Thanks!
e:
The database looks like that:
id | doctor_id | nurse_id | date | start_time | end_time |time_for_pacient| priority |comments
-----------------------------------------------------------------------------------------
1 | 5 | 4 | 13-12-01| 07:00 | 11:30 | 00:20 | 1 |Normal
2 | 5 | 4 | 13-12-02| 07:00 | 11:30 | 00:20 | 1 |Normal
3 | 5 | 4 | 13-12-01| 08:00 | 10:30 | 00:20 | 2 |Shorten
4 | 5 | 4 | 13-12-02| 06:00 | 10:30 | 00:10 | 3 |Extra
the result I want to achieve in View:
id | doctor_id | nurse_id | date | start_time | end_time |time_for_pacient| priority |comments
-----------------------------------------------------------------------------------------------------
3 | 5 | 4 | 13-12-01| 08:00 | 10:30 | 00:20 | 2 |Shorten
4 | 5 | 4 | 13-12-02| 06:00 | 10:30 | 00:10 | 3 |Extra
I just want to show a single record for each day with highest priority.
With EntityFramework and other ORM tools, don't think of selecting specific fields or columns. Rather, you're selecting specific obejcts.
To translate your exact query to LINQ would actually end up returning a collection of anonymous objects which is rarely what you want.
In C#, it might look like this:
var date1 = new Date(2013, 12, 1);
var date2 = new Date(2013, 12, 31);
var timetables = DbContext.Timetables
.Where(x => x.Date.CompareTo(date1) >= 0 && x.Date.CompareTo(date2) <= 0)
.Distinct();
consider a database having
checkID record_number data Order_number
a 0 1 2
a 1 2 0
a 2 3 1
a 3 4 3
Find a query which fetches the records according to Order_number column i.e.procedure should return data :
having record _number 2 then
having record _number 0 then
having record _number 1 then
having record _number 3
Is there any join or other query for this?
SELECT *
FROM tablename
ORDER BY CASE WHEN record_number = 2 THEN 0 ELSE 1 END, record_number;
SQL Fiddle Demo
This will give you your data in the following order:
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
select *
from YourTable
order by
case record_number
when 2 then 1
when 0 then 2
when 1 then 3
when 3 then 4
end
You can use CASE WHEN on this situation.;
SELECT *
FROM tbl
ORDER BY CASE record_number
WHEN 2 THEN 1
WHEN 0 THEN 2
WHEN 1 THEN 3
WHEN 3 THEN 4
END
Here is SQL Fiddle DEMO.
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
The best solution would be to create additional table for sorting checks, like this:
record_number sort_seq
2 1
0 2
1 3
3 4
then join it with your table
select c.*
from check as c
join sortSeq as s on c.record_number=s.record_number
order by s.sort_seq
SELECT *
FROM tablename
ORDER BY Order_number
I'm using MySQL.
This is table name item_supplier
supplier_ID Item_ID Date Price QTY
1 1 2012-01-01 00:00:00 500.00 2
1 1 2012-01-03 00:00:00 450.00 10
2 1 2012-01-01 00:00:00 400.00 5
3 1 2012-05-01 00:00:00 500.00 1
I need a select query showing a table something like this.
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00(2) 450.00(10) null
2 400.00(5) null null
3 null null 500.00(1)
or, at least,
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00 450.00 null
2 400.00 null null
3 null null 500.00
I hope someone can help me on this or give me a hint.
If there aren't a finite number of dates that are known beforehand, then you can't do what you want in MySQL alone.
Your best bet is to get a table like:
+---------+------------+-------------+-------------+
| Item_ID | Date | supplier_ID | price |
+---------+------------+-------------+-------------+
| 1 | 2012-01-01 | 1 | 500.00 (2) |
| 1 | 2012-01-01 | 2 | 400.00 (5) |
| 1 | 2012-01-03 | 1 | 450.00 (10) |
| 1 | 2012-05-01 | 3 | 500.00 (1) |
| ... | ... | ... | ..... |
Which can be done with:
SELECT Item_ID,Date,supplier_ID,CONCAT(FORMAT(Price,2),' (',QTY,')') AS price
FROM item_supplier
ORDER BY Item_ID,Date,supplier_ID;
Then on the C# side, loop through the results and print your desired output.
Since the output is now sorted by Item_ID, Date, and then supplier_ID, it's simple to loop through the results and then output in the format you want.
First of My SQL does not support to Crosstab/Pivot Query. So you need to create Dynamic temp Table for Columns and then inset record into it.
Like, First you have to fetch all date in one cursor and then create temp table and insert columns based on date's Cursor . After Creating table create another cursor for inserting rows. and fetch every row and update temp table.I have also done this using this way....
If you have any query please contact.