I have a table name as "invoice" which consists of column name "CId"
I have another table name as invoiceclient_details which consists of column name "CId"
So now my question is "what query should i write so that i will get the data of greater "CId" i.e. rows of data which consists of greater "CID"
I have tried like this
SELECT
invoiceclient_details.OrganizationName,
invoiceclient_details.InvoiceNo,
invoiceclient_details.InvoiceDate,
invoiceclient_details.DeliveryNote,
invoiceclient_details.TermsofPayment,
invoiceclient_details.EsugamNo,
invoiceclient_details.OrganizationName,
invoiceclient_details.BuyerOrderNo,
invoiceclient_details.BuyDate,
invoiceclient_details.DispatchDocumentNo,
invoiceclient_details.Dated,
invoiceclient_details.DispatchThrough,
invoiceclient_details.Destination,
invoiceclient_details.TermsofDelivery,
invoiceclient_details.BuyerTin,
invoice.id,
invoice.DescriptionOfGoods,
invoice.Quantity,
invoice.PerUnitPrice,
invoice.TotalPrice,
invoice.VAT14,
invoice.VAT5,
invoice.ServiceTax,
invoice.CST
FROM invoiceclient_details,invoice
WHERE MAX(invoiceclient_details.CId) = MAX(invoice.CId);
But it is showing an error like
"misusage of group function"
Use an INNER JOIN to join the tables on the CId.
SELECT *
FROM invoice i
INNER JOIN invoiceclient_details icd ON i.CId = icd.CId
Try this
select *
from invoice,invoiceclient_details
where
invoice.CId in (Select max(invoiceclient_details.CId) from invoiceclient_details)
and
invoiceclient_details.CId in (Select max(invoiceclient_details.CId) from invoiceclient_details)
Related
I have these three tables:
The table "Clienti" contains the customers.
The table "Corsi" contains all the available courses
The table "corsi Fatti" contains all the Courses each client has taken.
what I would need is a query that returns each client, and what courses he attended on what date.
For that I would like to have for example a table returned with these columns:
Clienti.Nome, corsi.row1.corso, corsi.row2.corso, corsi.row3.corso,corsi.rowN.corso.
and the content of the table should be:
clienti.Nome, corsifatti.data of the matching course in the corsi table if present.
so, first column is the client name, and then there is a column for each row of the "corsi" table, and if a client has partecipated on that course then the corsifatti.data should be in that column.
Can something like this be done with a Access or Mysql Query? I have tried with inner joins but the result was not what I need.
select
Clienti.nome, Clienti.Addresse, Clienti.CAP, Clienti.Tel,
Clienti.Ansprechpartner, Clienti.Mail, Clienti.Weiteres,
CorsiFatti.Data, Corsi.Corso, Corsi.Durata
from Clienti
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]
What you are asking is a simple inner join:
select Clienti.nome, Clienti.Addresse, Clienti.CAP,
Clienti.Tel, Clienti.Ansprechpartner, Clienti.Mail,
Clienti.Weiteres,
CorsiFatti.Data,
Corsi.Corso, Corsi.Durata
from Clienti
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]
order by Clienti.nome, Corsi.Corso;
I think you meant yours was lacking the order by only.
I wouldn't suggest using access, but if it is access anyway, then you need to have parenthseses around all those joins (peculiar I know, but it is access).
The pivot with variable columns count is complicated. I can advice simplest solution uses JSON aggregation:
SELECT
ClienteId,
Name,
JSON_ARRAYAGG(
JSON_OBJECT("Data", Data, "Corso", Corso)
) Corsi
FROM
CorsiFatti
JOIN Corsi ON Corsi.Id = CorsiFatti.CorsoId
JOIN Clienti ON Clienti.Id = CorsiFatti.clienteId
GROUP BY
ClienteId,
Name
;
Result is row for each client contains client's data at his courses as JSON string:
+===========+=========+========================================================================================+
| ClienteId | Name | Corsi |
+===========+=========+========================================================================================+
| 1 | Mr. Fix | [{"Data": "2021-01-01", "Corso": "Corso1"}, {"Data": "2021-02-01", "Corso": "Corso3"}] |
+-----------+---------+----------------------------------------------------------------------------------------+
Here you can find SQL fiddle
I have two tables:
Table 1:
employer(id_em, nom_em)
Table 2:
the second one is
travailler(id_em, id_depart, date_chnge)
a certain id_em can have multiple entries in the travailler table but I want only to get a table with the latest entry of id_em, so basically the one with the biggest date.
so the result of my query should be something like this :
(id_em, nom_em, id_depart, date_change)
but only one entry for every id_em, the one that has the latest date
I've tried this but it shows all of them, I don't know what's wrong
SELECT employe.nom_em,
travailler.id_em,
travailler.id_depart,
Max(travailler.date_chnge)
FROM employe
INNER JOIN travailler
ON employe.id_em = travailler.id_em
GROUP BY employe.id_em
Please help!
SELECT
e.nom_em,
t.id_em,
t.id_depart,
t.date_chnge
FROM
employe e
INNER JOIN
travailler t
ON e.id_em=t.id_em
WHERE
t.date_chnge = (select max(tr.date_chnge) from travailler tr where tr.id_em = e.id_em)
We have two tables in mysql database.Screenshots are attached below.
Given table ads_testTable
here is the screenshot of my dimesnionvalue_flattable
We have to run a query like the one below.
SELECT Quiz_Attempt.L1_Key dimID,
Quiz_Attempt.L1_Label CatVars,
COALESCE(**xyz**,0) AS series0
FROM DSQ_ADSSCHEMA.ADS_TestTable dataTable
RIGHT OUTER JOIN LS_CONFIG.DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY Quiz_Attempt.L1_Key, Quiz_Attempt.L1_Label;
My motive is to write a query in place of xyz so that I can get avg of obtainedMarks column in testtable according to the value of dimID I get.Each distinct Quiz_Attempt is a different test so If a Packet is repeating for a particular Quiz_Attempt in testTable, it should take only one value for that AttemptID.
I think you query could take the form of:
SELECT
L1_Key dimID,
L1_Label CatVars,
COALESCE('**xyz**',0) AS series0
FROM (
SELECT *
FROM (SELECT * FROM ADS_TestTable GROUP BY ADS_TestTable.Quiz_Attempt) dataTable
RIGHT OUTER JOIN DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY dataTable.Quiz_Attempt
) A GROUP BY dimID, CatVars;
The JOIN is done in an inner query, and grouped by Quiz_Attempt, so that you get a single row per attempt. This result is then used to compute what you need.
I have these 2 queries:
$sql = "SELECT *
FROM ultrait_wpl_properties
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id ";
$sql2 = "SELECT *
FROM ultrait_wpl_properties, ultrait_wpl_property_types
WHERE ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id";
For some odd reason when the IDs are output some are duplicated? By my reseaning these queries should get everything from the table in the first part and join the second table based on the WHERE condition.
<property><id>13</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>7</id></property>
This may be slightly unclear but for some reason I'm getting duplicate IDs, all i want really is to be able to access the property type which links to the ID in the second table.
I have tested both queries in phpMyAdmin and they yeild the desired result, however when I use the queries in my php script they return unexpected results.
You are getting one row for each row in table ultrait_wpl_properties. What else do you expect? If it is just one record per type, then you would have to re-write your query accordingly. You select * from both tables. But is it only the type ID you need? Then why join the tables at all?
Get all type IDs:
select id from ultrait_wpl_property_types;
Get all type IDs in table ultrait_wpl_properties:
select distinct property_type from ultrait_wpl_properties;
Get all type data for types in ultrait_wpl_properties:
select * from ultrait_wpl_property_types
where id in (select property_type from ultrait_wpl_properties);
You are getting a Cartesian result in the case the ultrait_wpl_property_types table has multiple records for a single property. Such as a property type could be Type A, Type B, Type C which might be descriptive "types". So a single property would be accounted for each entry.
You might just need to do SELECT DISTINCT, or GROUP BY ultrait_wpl_properties.id to make sure only one record per ID, but with generic "Select * ", I would first try with GROUP BY.
I have two select:
SELECT ID, ID_cat, modello
FROM tbArticoli
WHERE ID_cat=5
Example result in Json:
{"ID":"5","ID_cat":"5","modello":"Hawaii"},
{"ID":"6","ID_cat":"5","modello":"T-Shirt Righe"},
{"ID":"7","ID_cat":"5","modello":"Polo"},
{"ID":"8","ID_cat":"5","modello":"Fantasia"},
{"ID":"9","ID_cat":"5","modello":"Fiori"},
{"ID":"10","ID_cat":"5","modello":"Arcobaleno"},
{"ID":"11","ID_cat":"5","modello":"Oro"},
{"ID":"12","ID_cat":"5","modello":"Argento"},
{"ID":"13","ID_cat":"5","modello":"StelleStrisce"}
And another select:
SELECT IDModello,
FLOOR(AVG(voto)) AS votomedio
FROM tbCommenti
GROUP BY IDModello
with result:
{"IDModello":"5","votomedio":"7"},
{"IDModello":"6","votomedio":"7"},
{"IDModello":"7","votomedio":"8"},
{"IDModello":"8","votomedio":"6"}
I need a final result like this:
{"ID":"5","ID_cat":"5","modello":"Hawaii","votomedio":"7"},
{"ID":"6","ID_cat":"5","modello":"T-Shirt Righe","votomedio":"7"},
{"ID":"7","ID_cat":"5","modello":"Polo","votomedio":"8"},
{"ID":"8","ID_cat":"5","modello":"Fantasia","votomedio":"6"},
{"ID":"9","ID_cat":"5","modello":"Fiori","votomedio":"null"},
{"ID":"10","ID_cat":"5","modello":"Arcobaleno","votomedio":"null"},
{"ID":"11","ID_cat":"5","modello":"Oro","votomedio":"null"},
{"ID":"12","ID_cat":"5","modello":"Argento","votomedio":"null"},
{"ID":"13","ID_cat":"5","modello":"StelleStrisce","votomedio":"null"}
on tbArticoli.ID = tbCommenti.IDModello
Which is the best query?
Thank you.
Assuming you want to join on id = idmodello, you can do something like
SELECT *
FROM (SELECT ID, ID_cat, modello
FROM tbArticoli
WHERE ID_cat=5) AS tbA
LEFT JOIN (SELECT IDModello,
FLOOR(AVG(voto)) AS votomedio
FROM tbCommenti
GROUP BY IDModello) as tbC
ON tbA.ID = tbC.IDModello
You can specify subqueries as what you are selecting from, because MySQL selects from a set of tuples. The table name just specifies that you want all tuples from that table, whereas in the query above you are specifying the specific tuples that you want. The main thing to note in this query is that you must use the 'AS' keyword to specify a temp name for each set of tuples.