Totalling values with the same order name - sql-order-by

I have table in mysql with columns "name" and "views". I want sum views from serial whose name is in "name" column and display list of serials with views by echo from the most popular.

I think you are looking for this query:
SELECT name, SUM(views) as total_views
FROM serial
GROUP BY name
ORDER BY total_views DESC

Related

How to retrieve workflow attribute values from workflow table?

I have a situation where in I need to take the values from table column which has data based on one of the column in same table.
There are two column values like that which is required to compare with another table.
Scenario:
Column 1 query:
SELECT text_value
FROM WF_ITEM_ATTRIBUTE_VALUES
WHERE name LIKE 'ORDER_ID' --AND number_value IS NOT NULL
AND Item_type LIKE 'ABC'
this query returns 14 unique records
Column 2 query:
SELECT number_value
FROM WF_ITEM_ATTRIBUTE_VALUES
WHERE name LIKE 'Source_ID' --AND number_value IS NOT NULL
AND Item_type LIKE 'ABC'
this also returns 14 records
and order_id of column 1 query is associated with source_id of column 2 query using this two column values i want to compare 14 records combined order_id, source_id with another table column i.e. Sales_tbl
columns sal_order_id, sal_source_id
Sample Data from WF_ITEM_ATTRIBUTE_VALUES:
Note: same data in the sales_tbl table but order_id is sal_order_id and sal_source_id
Order_id
204994 205000 205348 198517 198176 196856 204225 205348 203510 206528 196886 198971 194076 197940
Source_id
92262138 92261783 92262005 92262615 92374992 92375051 92374948 92375000 92375011 92336793 92374960 92691360 92695445 92695880
Desired O/p based on comparison:
Please help me in writing the query

How to get distinct single column value while fetching data by joining two tables

$data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get(array('establishments.*'));
this is controller condition.
I have two tables, in table1 i am matching id with table2 and then fetching data from table1, and in table2 i have multiple values of same id in table 1. i want to get data of table1 values only one time, but as i am hvg multiple data of same id in table2 , data is repeating multiple times, can anyone please tell me how to get data only one time wheater table2 having single value or multiple value of same id... thank you
you can do it by selecting the field name you desired
instead get all field from table establishments
$data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get(array('establishments.*'));
you can select specific field from table establishments like
$data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get('establishments.fieldName');
or you can also do
$data['establishments2'] = `Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->select('establishments.fieldName')->get();`

How to use a query result or column value to define a select column name?

How can I use a column value as a column name. I've tried this:
SELECT TableX.(
SELECT OdTable.columnamecell
from OdTable
where 1 =1
AND OdTable.KeyValue = TableX.SomeValue
) as MyValue
,TableX.OtherValue as OtherValue
, TableX.SomeValue
from TableX
WHERE 1 = 1
Or to say it another way: Can I use a table column value as a column name for another query or sub-query?
To clarify: The table: OdTable has a column with values that are the column name in another table.
No, and Yes. You can't do this with "standard" SQL; all table and column names must be known, as literals, when the query is compiled; they can't be provided at runtime. What you want is called "dynamic SQL"; sometimes it is the only solution to a problem, but most of the time it is used when it is not necessary. It has several disadvantages (security risk, performance penalty, difficulty to maintain, ...)

Compare two columns from two different tables

TableA: Personal_info (Name,DOB,Fname,add,......)
TableB: Company_Info (Name,DOB,join_dt,Qual,sal....)
I want to compare these two tables in such a way that it displays different DOB with same name (Date of birth).
thanks.
You should join the two tables on the Name field and not equal DOB:
SELECT Personal_Info.DOB, Company_Info.DOB, Name
FROM Personal_Info JOIN Company_Info ON Personal_Info.Name = Company_Info.Name
AND Personal_Info.DOB != Company_Info.DOB

SSAS - Creating named calculation from different tables

I am having troubles when i want to create a named calculation from two different tables.
I have the table "CallesDim" with an id(PK) and a description and the table "UbicacionesDim" with an id (PK), another id (FK to "CallesDim") and a description:
--
CallesDim
id PK
Descripcion VARCHAR
--
UbicacionesDim
id PK
CalleId FK to id from CallesDIM
Altura INT
--
I want to concatenate "Descripcion" from "CallesDim" with Altura from "UbicacionesDim".
I try doing this:
CallesDim.Descripcion + ' ' + CONVERT(VARCHAR,UbicacionesDim.Altura)
but i am having the following error:
the multi-part identifier "CallesDim.Descripcion" could not be bound
Any ideas?
Thanks!
In a named calculation you can only access columns from the table that it is defined on.
Which record of the other table should it take in case it would accept columns from other tables? How should it join? All this cannot be configured.
If you need to join two (or more) tables, you can define a named query that can contain joins and access as many tables as you like. A named query can contain everything that you can state in a single select statement.

Resources