I have a table named TRNSPOINDT. TRNSPOINDT has three fields named as itemid, Qty, projectname. I need to group the items by itemid and display the sum of Qty with respect to the itemid. I have done this, the query is as follows,
SELECT ITMID ,SUM(QTY) AS QTY FROM TRNSPOINDT GROUP BY ITMID
The problem is, i should display the project name respective to the itemid. But while iam trying to display project name, i got a error.
I have tried this query,
SELECT ITMID ,SUM(QTY) AS QTY,PROJECTNAME FROM TRNSPOINDT GROUP BY ITMID
I got a error as,
Column 'TRNSPOINDT.PROJECTNAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know that, i got this error because there is more than one project name is exist for a single itemid. But i should display the project name near to the itemid. How to do it..
Thanks in advance,
Praveen.T
The reason for this error is that it doesn't make sense to include the project name in this query. For example suppose you had
itmid qty projectname
1 3 proj1
1 7 proj2
1 2 proj1
3 4 proj1
You want rows for
itmid qty
1 12
3 4
But how would you attach a projectname to the first result row?
You mention in your question that you know the reason, but it really doesn't have a solution, other than grouping by item and project together, or creating a string of comma-separated project names for the items (which isn't exactly "denormalized", but is maybe what you want).
Do you mean this:
SELECT ITMID, SUM(QTY) AS QTY, PROJECTNAME
FROM TRNSPOINDT
GROUP BY ITMID, PROJECTNAME
It would give
itmid qty projectname
1 5 proj1
1 7 proj2
3 4 proj1
It groups by all itemid-projectname combinations.
Two ways depending on what 'exactly' you are looking for. First add projectname column in group by.
SELECT ITMID ,SUM(QTY) AS QTY,PROJECTNAME FROM TRNSPOINDT GROUP BY ITMID, PROJECTNAME
ITMID Sum(QTY) PROJECTNAME
-----------------------------------
itm1 10 PROJ1
itm1 20 PROJ2
itm2 12 PROJ3
This will display projectname in result, but the sum will be calculated for Qty for ProjectNAME and not by ITMID as you say there are more than one PROJECTNAME for one ITMID.
If you dont want Sum to be calculated for PROJECTNAME but only by ITMID. Then you will have to use subquery.
SELECT B.ITMID , B.QTY, A.PROJECTNAME
FROM TRANSPOINTDT As A INNER JOIN (SELECT ITMID ,SUM(QTY) AS QTY,PROJECTNAME FROM TRNSPOINDT GROUP BY ITMID) as B
ON A.ITMID = B.ITMID
ITMID SUM(QTY) PROJECTNAME
------------------------------------
itm1 30 PROJ1
itm2 12 PROJ3
Though this does not make sense to add PROJECTNAME column as it will not represent the correct information.
Related
I`m storing a hierarchical product adjacency list and a separate table for all the sales on those products.
Currently, I`m trying to present to the user a "Sales Report" with the total amount/sum sold per each product, and at the parent level.
In the sales table, I do not have info on the sales per group, thus, I have info available only at the level of the child. From what I have read I need to use recursive CTE, and I tried creating some queries, without any success.
Example of my dataset:
F - folders
P - products
Products table:
id
name
parentid
1
F1
NULL
2
F2
1
3
P1
2
4
P2
2
Sales table:
id
quant
sum
3
3
90
4
2
100
What I need to obtain in the report:
id
name
parentid
quant
sum
1
F1
NULL
5
190
2
F2
1
5
190
3
P1
2
3
90
4
P2
2
2
100
Logically, I understand that I need to fetch each row, and recursively go through all its children in order to SUM the quant and the sum, however I have no clue how to write it.
I`d be thankful for any guidance on where I could read more about recursive CTE, or anything that can help my situation.
Cheers!
I'm new to tableau.
I have a data like this.
[department]
[employee]
And I want to get employee count under 30 age per department. like this..
[my goal]
I want to show all departments (if there is no employee)
To get result, I do these steps,
make relationship of dept - emp (one datasource)
make datasource filter (emp.age < 30)
make sheet and set row as dept_no, countd(emp_no)
But I could only get like this.
[my result]
How can I get my goal..!!??
Help me!!
It is simple hereafter. Use join (instead of relationship) on the side of dept table and Take dept_no column from dept_table instead of emp_table. To do this follow these steps
double click on first added table (say emp). A join window will open (thgis differs from relationship)
add dept table and use right join
right click age convert to dimension
right click again age and convert to continuous
create filter at most 29 on age and don't forget to check include null values in the table
If you want to add all values in view. create a calc field with calculation as
INT([Age] <30)
add sum of this field to view
edit the calculation to show all nulls as 0
ZN(INT([Age] <30))
How can I want print the same field twice with different value for each?
My Product and Shop tables are linked with product_id as the primary/foreign key.
Product
product_id
product_name
Shop
shop_id
product_id
quantity_product
When I use Expert Selection, I take all products on Shop_id 1 and Shop_id 2 (if Shop_id is in {1,2}...). Now I want to print 2 different quantity_product fields from Shop 1 and Shop 2 in the same line as my product_id. Something like:
product_name quantity_product(shop_id: 1) quantity_product(shop_id: 2)
1 10 20
I tried adding a new link in my database scheme with another Shop table, but it didn't work out.
You can set up a CrossTab in the summary section to handle any number of Shops. (So long as there's room on the report for all the shop columns)
If that doesn't work you can manually set up a summary for each Shop:
Keep in mind both of these solutions will need to be placed in a Footer section instead of the Details.
I'm trying to create a report which picks 2nd oldest order for every user and groups them by month/year. I know how to select the oldest order for each, but can't figure out how to get the second oldest.
The input data is a table which has every customers' order as a single row with the relevant columns being
order.id, order.user_id, order.date, product.name...
The ideal result I'm looking for is something like:
mon/year : number of second orders
12/2013 : 14
01/2014 : 2
Try this Code
select to_char(date_trunc('month',date),'MM/yyyy'),(select count(*) from order where date=min(p.date)) from order p
group by date_trunc('month',date)
Im working on an iPhone application, Using SQLite as the backend
In my application i have two tables, ProjectsTable, Project_Hours_Table in my DB.
Here in first table i have list of projects with their unique_id.
In second table i have project ID's (as foreign key) and the respective working hours.
Now my requirement is to get the project_ID's and the sum of that particular project working hours.
as follows
Project_ID Hours
1 1
2 0
3 5 (1+4)
4 11 (5+6)
5 2
6 13 (7+6)
7 3
can any one please provide suggestions to implement the query
thanks in advance
select Project_Id,
sum(Hours) as Hours
from Proj_HoursTable
group by Project_Id
Since both of the required fields are available in Proj_HoursTable, no need to join to ProjectTable. Adding the project name to the output would require a join to ProjectTable (assumes project id and project name are 1 to 1
select pjt.ProjName,
pht.Project_Id,
sum(pht.Hours) as Hours
from Proj_HoursTable pht join ProjectsTable pjt
on pjt.Proj_Id = pht.Project_Id
group by pht.Project_Id, pjt.ProjName
First You need to display Proj_Id for particular project (That you want to show total hour) and store it in variable.
And then after write query for this
select * from Project_Hours_Table where Proj_Id = Proj_Id
This query return all data of specific Proj_Id that you need.
and store it data to NSMutableDictionary, after you need to get value of Hour field by your dictionary key #"hour"
you return this value as NSSting and you need to convert it in intValue by
int hourCount = [StringHourValue intValue];
and count it it will give you total number of Hour