Hi I have two tables one is tab_data and another one is tbl_catg
I want a result like in the image. What is available in tbl_Catg should show as it is. What is not available should show as "All Other"
The below query is showing the wrong result.
select
Future_Product_Category,
sum(TYDOLLAR) ty
from [dbo].[tbl_DATA] a
inner join [tbl_CATG] b on
ISNULL(a.Future_Division,'NULLDIV')=ISNULL(b.Division,'NULLDIV')
and IS NULL(a.Future_Product_Category,'NULLCATG')=ISNULL(b.CatgName,'NULLCATG')
group by Future_Product_Category
union All
select
'All Other',
A.TYDOllar
from
(select
Future_Division,
Future_Product_Category,
sum(TYDOLLAR) TYDOllar
from [dbo].[tbl_DATA]
group by Future_Division, Future_Product_Category) a
,[tbl_CATG] b
where
a.Future_Division=b.Division
and (a.Future_Product_Category not in (select CatgName from [tbl_CATG] where a.Future_Division=b.Division ))
order by a.Future_Product_Category
Related
I have two separate queries: Employee and Manager, which work perfectly fine and as I want but I wish to combine all the COLUMNS from both queries. Is this possible? I obviously don't expect a full answer but would appreciate some insight into possible methods to achieve this.
Employee Query:
SELECT
shopTableRef.shopname AS "Shop Name:",
perTableRef.personname AS "Employee Name:",
COUNT(inStoreTableRef.payid) AS "Total Sales:",
SUM(payTableRef.amount) AS "Sales Value (£):"
FROM
fss_Person perTableRef
JOIN
fss_Employee empTableRef ON perTableRef.personid = empTableRef.empid
JOIN
fss_InstorePayment inStoreTableRef ON empTableRef.empid = inStoreTableRef.empid
JOIN
fss_Payment payTableRef ON payTableRef.payid = inStoreTableRef.payid
JOIN
fss_Shop shopTableRef ON payTableRef.shopid = shopTableRef.shopid
WHERE
empTableRef.roleid = 2
GROUP BY
perTableRef.personname
ORDER BY
COUNT(inStoreTableRef.payid) DESC
Manager Query:
SELECT
perTableRef.personname AS "Manager's Name:"
FROM
fss_Person perTableRef
JOIN
fss_Employee empTableRef ON perTableRef.personid = empTableRef.empid
JOIN
fss_Manager manTableRef ON empTableRef.empid = manTableRef.empid
WHERE
empTableRef.roleid = 1
GROUP BY
perTableRef.personname
I have tried the following: UNION, UNION ALL, INNER JOIN, JOIN and nested queries but none seem to work.
UNION or UNION ALL should work with a couple of adjustments.
Each query must return the same number/type of columns, so fill in your Manager query with NULL AS "Shop Name:", etc. for the missing columns.
Only one ORDER BY is allowed for the combined queries, so make sure that comes at the very end, and refer to the ordering column by its aliased name "Total Sales:"
I need to write query that joins several tables and I need distinct value from one table based on max count().
These are my tables names and columns:
bands:
db|name|
releases_artists:
release_db|band_db
releases_styles
release_db|style
Relations between tables are (needed for JOINs):
releases_artists.band_db = bands.db
releases_styles.release_db = releases_artists.release_db
And now the query that I need to write:
SELECT b.name, most_common_style
LEFT JOIN releases_artists ra ON ra.band_db = b.db
and here I need to find the most common style from all band releases
JOIN(
SELECT DISTINCT style WHERE releases_styles.release_db = ra.release_db ORDER BY COUNT() DESC LIMIT 1
)
FROM bands b
WHERE b.name LIKE 'something'
This is just a non working example of what I want to accomplish. It would be great if someone could help me build this query.
Thanks in advance.
EDIT 1
Each artist from table bands can have multiple records from releases_artists table based on band_db and each release can have multiple styles from releases_styles based on release_db
So if I search for b.name LIKE '%ray%' it returns something similar to:
`bands`:
o7te|Ray Wilson
9i84|Ray Parkey Jr.
`releases_artists` for Ray Wilson:
tv5c|o7te (for example album `Change`)
78wz|o7te (`The Next Best Thing`)
nz7c|o7te (`Propaganda Man`)
`releases_styles`
tv5c|Pop
tv5c|Rock
tv5c|Alternative Pop/Rock
----
78wz|Rock
78wz|Pop
78wz|Classic Rock
I need style name that repeats mostly from all artist releases as this artist main style.
Ok, this is a bit of a hack. But the only alternatives I could think of involve heaps of nested subqueries. So here goes:
SELECT name
, SUBSTRING_INDEX(GROUP_CONCAT(style ORDER BY release_count DESC SEPARATOR '|'), '|', 1) AS most_common_style
FROM (
SELECT b.db
, b.name
, rs.style
, COUNT(*) AS release_count
FROM bands b
JOIN releases_artists ra ON ra.band_db = b.db
JOIN releases_styles rs ON rs.release_db = ra.release_db
GROUP BY b.db, rs.style
) s
GROUP BY db;
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'm brand new to SQL and I know this should be easy, but I can't seem to find any reference on how to do specifically what I'm looking for. I've check the archives and I can't find a basic example.
Anyway, all I want is -
SELECT
(COUNT (i.productNumber WHERE i.type = 'type1') AS 'Type 1'),
(COUNT (i.productNumber WHERE i.type = 'type2') AS 'Type 2'),
FROM items AS i
WHERE i.dateadded BETWEEN '2015-03-02' and '2015-03-04'
The two count conditions are different, but both of those queries share that date condition. I've done two distinct select statements and put a UNION between them. That works. The only issue is all of the data appears in one column under the first alias in the statement. I would need each alias to be a new column. I also have to write the date condition in twice.
You could group them by type so you would get a different row for each of them :
SELECT i.type, COUNT(i.productNumber)
FROM items i
WHERE i.dateadded BETWEEN '2015-03-02' AND '2015-03-04'
GROUP BY i.type;
If you really want to have one row, then you could do
SELECT COUNT(b.productNumber) AS 'type1', COUNT(c.productNumber) AS 'type2'
FROM items i
LEFT JOIN items b on b.productNumber = i.productNumber
LEFT JOIN items c on c.productNumber = i.productNumber
WHERE i.dateadded BETWEEN '2015-03-02' AND '2015-03-04'
AND b.type = 'type1'
AND c.type = 'type2';
Hey guys I have a query and it works fine, but I want to add another table to the mix. The invite table I want to add has two fields: username and user_invite. Much like this site, I am using a point system to encourage diligent users. The current query which is displayed below adds the up votes and down votes based on the user in question: $creator. I want to count the number of entries for that same user from the invite table, and add 50 for each row it finds to the current output/sum of my query. Is this possible with one query, or do I need two?
"SELECT *,
SUM(IF(points_id = \"1\", 1,0))-SUM(IF(points_id = \"2\", 1,0)) AS 'total'
FROM points
LEFT JOIN post ON post.post_id=points.points_id
WHERE post.creator='$creator'"
This should work :
SELECT *,**SUM(IF(points_id = "1", 1,0))-SUM(IF(points_id = "2", 1,0))+(select count(*)*50
from inivite where username='$creator') AS 'total'**,
FROM points LEFT JOIN post ON post.post_id=points.points_id WHERE post.creator='$creator'"
Assuming that there might be no correspondence in invite table, I used outer join and coalesce:
SET #good='1', #bad='2', #creator='$creator';
SELECT *,
SUM(IF(points_id=#good, 1,0))-SUM(IF(points_id=#bad, 1,0))+COALESCE(inv_cnt, 0) * 50) AS total
FROM points
LEFT JOIN post
ON post.post_id=points.points_id
LEFT OUTER JOIN (SELECT username, COUNT(user_invite) as inv_cnt
FROM invite
GROUP BY username) invites
ON post.creator = invites.username
WHERE post.creator=#creator;
Designing this query with limited knowledge of the schema...
SELECT *,
SUM(IF(points_id = \"1\", 1,0))
-SUM(IF(points_id = \"2\", 1,0))
+ 50 * COUNT(invite.user_invite) AS 'total' <--
FROM points
LEFT JOIN post ON post.post_id=points.points_id <--
LEFT JOIN invite ON post.creator = invite.user_invite
WHERE post.creator='$creator'
The important thing here is the extra lines, which I've marked with "<--". One is for JOINing your two tables together, the other is to modify the argument of the SUM function.
Post back if this doesn't work.